我正在以天蓝色功能注册自定义遥测处理器-但是在启动期间,永远不会解雇客户处理器。我的代码如下:
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中可以正常工作。有解决方法吗?还是我错过了什么?
答案 0 :(得分:1)
在ITelemetry Processor
中使用azure function
时,请尝试以下指南:
首先,在您的Azure函数项目Microsoft.Azure.WebJobs.Logging.ApplicationInsights中安装nuget软件包的最新版本3.0.13
。
然后编写您的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;
});
}
}
}
}
然后将此天蓝色功能发布到天蓝色门户->完成发布后,在天蓝色门户中->您的天蓝色功能->监视选项卡,添加您的应用程序见解。
最后,将blob上传到blob存储,然后导航到应用程序见解,您可以看到该属性已添加到遥测数据中。截图如下: