这可以通过 ITelemetry初始化程序来实现,您可以将自定义维度添加到指定的遥测中(仅用于请求)。
1。安装以下nuget软件包:
Microsoft.ApplicationInsights,version 2.11.0
Microsoft.NET.Sdk.Functions,version 1.0.29
2。以下是我的测试代码。
[assembly: WebJobsStartup(typeof(FunctionApp54.MyStartup))]
namespace FunctionApp54
{
internal class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
//use telemetry is RequestTelemetry to make sure only add to request
if (telemetry != null && telemetry is RequestTelemetry && !telemetry.Context.GlobalProperties.ContainsKey("testpro"))
{
telemetry.Context.GlobalProperties.Add("testpro","testvalue");
}
}
}
public class MyStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer,MyTelemetryInitializer>();
}
}
public static class Function2
{
[FunctionName("Function2")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function,"get","post",Route = null)] HttpRequest req,ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello,{name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
,
是的,这是可能的。
选项1:在AsyncLocal的帮助下使用遥测初始化程序:
public class CustomTelemetryInitializer : ITelemetryInitializer
{
public static AsyncLocal<string> MyValue = new AsyncLocal<string>();
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties propertyItem)
{
propertyItem.Properties["myProp"] = MyValue.Value;
}
}
}
您可以在函数中设置AsyncLocal的值,如下所示:
[FunctionName("Function")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function,Route = null)]
HttpRequest req,ILogger log)
{
var name = req.Query["name"];
CustomTelemetryInitializer.MyValue.Value = name;
log.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult($"Hello,{name}");
}
运行该功能时,您可以在门户中查看信息:
您可以在this repo
中找到完整的代码
现在,解决您的评论。是的,您需要额外的东西。 ITelemetryInitializer
实例需要使用依赖注入进行注册。如documentation所述,在Startup
类中完成此操作:
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(FunctionApp.Startup))]
namespace FunctionApp
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer,CustomTelemetryInitializer>();
builder.Services.AddLogging();
}
}
}
注册后,Application Insights SDK将使用CustomTelemetryInitializer
。
选项2
另一个选项不涉及任何TelemetryInitializer,但是只能将属性添加到由Azure Function App Insights集成添加的生成的RequestTelemetry中。这是通过利用当前TelemetryRequest
存储在HttpContext
中的事实来完成的:
[FunctionName("Function")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function,ILogger log)
{
var name = req.Query["name"];
CustomTelemetryInitializer.MyValue.Value = name;
log.LogInformation("C# HTTP trigger function processed a request.");
var telemetryItem = req.HttpContext.Features.Get<RequestTelemetry>();
telemetryItem.Properties["SetInFunc"] = name;
return new OkObjectResult($"Hello,{name}");
}
这也将显示在门户中:
仅将上下文添加到请求是否存在问题?它可能会,但是请注意,您可以查询所有相关的遥测并知道所涉及的上下文,例如:
union (traces),(requests),(dependencies),(customEvents),(exceptions)
| extend itemType = iif(itemType == 'availabilityResult',itemType,iif(itemType == 'customEvent',iif(itemType == 'dependency',iif(itemType == 'pageView',iif(itemType == 'request',iif(itemType == 'trace',iif(itemType == 'exception',"")))))))
| extend prop = customDimensions.SetInFunc
| where ((itemType == 'trace' or (itemType == 'request' or (itemType == 'pageView' or (itemType == 'customEvent' or (itemType == 'exception' or (itemType == 'dependency' or itemType == 'availabilityResult')))))))
| top 101 by timestamp desc
将显示:
来自同一调用的所有遥测将具有相同的operation_Id
。
本文链接:https://www.f2er.com/3160282.html