1

我有一个 Azure 函数(.NET core 2.0),它在 ADO 存储库中的每个 PR 上运行。我想将 PR-ID 作为元数据添加到 Azure 函数记录的每个跟踪。(我正在使用tracesAzure 应用程序洞察实例中的表查看日志)

Azure 函数通过以下方式记录跟踪:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, ILogger log, ExecutionContext context)
{
  logger.LogInformation("Trace Message");
}

如何向每个跟踪添加额外的元数据?

4

2 回答 2

3

是的,这是可能的。

选项 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, "get", "post", 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}");
        }

运行该功能时,您可以在门户中看到信息:

在此处输入图像描述

你可以在这个 repo找到整个代码

现在,解决您的意见。是的,你需要一些额外的东西。ITelemetryInitializer需要使用依赖注入注册实例。这是在文档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, "get", "post", Route = null)]
            HttpRequest req,
            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',itemType,iif(itemType == 'dependency',itemType,iif(itemType == 'pageView',itemType,iif(itemType == 'request',itemType,iif(itemType == 'trace',itemType,iif(itemType == 'exception',itemType,"")))))))
| 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.

于 2019-11-08T12:06:44.167 回答
1

这可以使用ITelemetry Initializer来实现,您可以将自定义维度添加到指定的遥测数据中,例如仅用于请求。

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");
        }
    }
}

在此处输入图像描述

于 2019-11-06T07:56:49.543 回答