gpt4 book ai didi

azure - 将 Application Insights 遥测处理器添加到 Azure Functions

转载 作者:行者123 更新时间:2023-12-02 06:05:49 28 4
gpt4 key购买 nike

我正在 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 中运行良好。有解决方法吗?或者我错过了什么?

最佳答案

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

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

然后编写您的 azure 函数。在我的测试中,我创建了一个 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 存储,然后导航到应用程序见解,您可以看到该属性已添加到遥测数据中。截图如下:

enter image description here

关于azure - 将 Application Insights 遥测处理器添加到 Azure Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296488/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com