【问题标题】:Adding an Application Insights Telemetry Processor to an Azure Function将 Application Insights 遥测处理器添加到 Azure 函数
【发布时间】:2019-10-09 03:03:53
【问题描述】:

我在 Azure 函数中注册了一个自定义遥测处理器 - 但是,在启动期间,客户处理器永远不会被触发。我的代码如下所示:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        // ...
        builder.Services.AddHttpContextAccessor();
        builder.Services.AddApplicationInsightsTelemetryProcessor<CustomTelemetryProcessor>();
        // ...
    }
}

public class CustomTelemetryProcessor : ITelemetryProcessor
{
    private ITelemetryProcessor _next;
    private IHttpContextAccessor _httpContextAccessor;

    public CustomTelemetryProcessor(ITelemetryProcessor next, IHttpContextAccessor httpContextAccessor)
    {
        // never gets to here
        _next = next;
        _httpContextAccessor = httpContextAccessor;
    }

    public void Process(ITelemetry item)
    {
        // never gets here
        if (item is OperationTelemetry operationTelemetry)
        {
            // ...
            operationTelemetry.Properties.Add("MyCustomProperty", "MyCustomValue");
            // ...
        }

        // Send the item to the next TelemetryProcessor
        _next.Process(item);
    }
}

这种方法在 Web API 中运行良好。有解决方法吗?还是我错过了什么?

【问题讨论】:

  • 您好,您可以按照下面的答案解决您的问题吗?如果您仍有任何问题,请随时告诉我:)。
  • 是的,我让它工作了,我喜欢你在 Configure 中所做的事情!非常感谢

标签: azure .net-core azure-functions azure-application-insights


【解决方案1】:

azure function 中使用ITelemetry Processor 时,请尝试以下指南:

首先,在你的 azure 函数项目中安装最新版本的 3.0.13 nuget 包:Microsoft.Azure.WebJobs.Logging.ApplicationInsights

然后编写你的天蓝色函数。在我的测试中,我创建了一个 blob 触发器 azure 函数 v2。出于测试目的,我只是在跟踪遥测中添加了一个自定义属性(您可以修改代码以满足您的需要)。代码如下:

    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.ApplicationInsights.Extensibility;
    using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using System.IO;
    using System.Linq;

    [assembly: WebJobsStartup(typeof(FunctionApp16.MyStartup))]
    namespace FunctionApp16
    {
        public static class Function1
        {
            [FunctionName("Function1")]
            public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
            {
                log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            }
        }

        internal class CustomTelemetryProcessor : ITelemetryProcessor
        {
            private ITelemetryProcessor _next;
            private IHttpContextAccessor _httpContextAccessor;

            public CustomTelemetryProcessor(ITelemetryProcessor next, IHttpContextAccessor httpContextAccessor)
            {
                _next = next;
                _httpContextAccessor = httpContextAccessor;         
            }

            public void Process(ITelemetry item)
            {
                //for testing purpose, I just add custom property to trace telemetry, you can modify the code as per your need.
                if (item is TraceTelemetry traceTelemetry)
                {
                    // use _httpContextAccessor here...        
                    traceTelemetry.Properties.Add("MyCustomProperty555", "MyCustomValue555");               
                }

                // Send the item to the next TelemetryProcessor
                _next.Process(item);
            }
        }

        public class MyStartup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                builder.Services.AddHttpContextAccessor();

                var configDescriptor = builder.Services.SingleOrDefault(tc => tc.ServiceType == typeof(TelemetryConfiguration));
                if (configDescriptor?.ImplementationFactory != null)
                {
                    var implFactory = configDescriptor.ImplementationFactory;
                    builder.Services.Remove(configDescriptor);
                    builder.Services.AddSingleton(provider =>
                    {
                        if (implFactory.Invoke(provider) is TelemetryConfiguration config)
                        {
                            var newConfig = TelemetryConfiguration.Active;
                            newConfig.ApplicationIdProvider = config.ApplicationIdProvider;
                            newConfig.InstrumentationKey = config.InstrumentationKey;
                            newConfig.TelemetryProcessorChainBuilder.Use(next => new CustomTelemetryProcessor(next, provider.GetRequiredService<IHttpContextAccessor>()));
                            foreach (var processor in config.TelemetryProcessors)
                            {
                                newConfig.TelemetryProcessorChainBuilder.Use(next => processor);
                            }
                            var quickPulseProcessor = config.TelemetryProcessors.OfType<QuickPulseTelemetryProcessor>().FirstOrDefault();
                            if (quickPulseProcessor != null)
                            {
                                var quickPulseModule = new QuickPulseTelemetryModule();
                                quickPulseModule.RegisterTelemetryProcessor(quickPulseProcessor);
                                newConfig.TelemetryProcessorChainBuilder.Use(next => quickPulseProcessor);
                            }
                            newConfig.TelemetryProcessorChainBuilder.Build();
                            newConfig.TelemetryProcessors.OfType<ITelemetryModule>().ToList().ForEach(module => module.Initialize(newConfig));
                            return newConfig;
                        }
                        return null;
                    });
                }
            }
        }
    }

然后将此 azure 函数发布到 azure 门户 -> 发布完成后,在 azure 门户中 -> 您的 azure 函数 -> 监控选项卡,添加您的应用程序见解。

最后,将 blob 上传到 blob 存储,然后导航到应用程序洞察,您可以看到该属性已添加到遥测数据中。截图如下:

【讨论】:

  • 这不再适用于 Microsoft.NET.Sdk.Functions 3.0.3。 configDescriptor 解析为 null,即 builder.Services 没有 TelemetryConfiguration 类型的元素。
  • @BobbyKoteski,您可能缺少 APPINSIGHTS_INSTRUMENTATIONKEY 环境变量。
猜你喜欢
  • 1970-01-01
  • 2021-01-14
  • 2020-01-08
  • 2018-12-05
  • 2014-09-28
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多