我有以下遥测处理器:
public class ExcludeInProcInvokeTelemetryProcessor(ITelemetryProcessor next) : ITelemetryProcessor
{
private readonly ITelemetryProcessor _next = next;
public void Process(ITelemetry item)
{
if (item is DependencyTelemetry dependency && dependency.Type == "InProc" && dependency.Name == "Invoke")
return;
_next.Process(item);
}
}
Visual Studio 的诊断工具显示此方法使用了 5% 的 CPU 总量,我想是因为每个遥测都会调用它。
还有其他方法可以进行此过滤吗?该处理器在隔离进程模型上的天蓝色函数中运行,因此我无法在主机级别进行过滤(如果可能的话)。
如有任何其他性能优化技巧,我们将不胜感激!
从总体上看,5% 的 CPU 并不是一个很大的数字,但考虑到过滤器的简单性,这似乎没有必要。我有另一个更复杂的过滤器,它没有出现在诊断工具的顶级资源消耗方法中。
还有其他方法可以进行此过滤吗?
通过使用采样遥测来筛选隔离进程模型中的 Application Insights for Azure Functions 中的一些日志,我可以实现应用采样的自定义遥测处理器。检查下面的代码:
功能代码:
public class Function1
{
private readonly ILogger _logger;
private readonly TelemetryClient _telemetryClient;
public Function1(ILoggerFactory loggerFactory, TelemetryClient telemetryClient)
{
_logger = loggerFactory.CreateLogger<Function1>();
_telemetryClient = telemetryClient;
}
[Function("Function1")]
[Obsolete]
public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
{
_logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
_telemetryClient.TrackEvent("TimerTriggerEvent");
_telemetryClient.TrackDependency("HTTP", "https://example.com", DateTime.UtcNow, TimeSpan.FromMilliseconds(500), true);
_telemetryClient.TrackTrace("This is a custom trace message.");
}
}
Program.cs 具有采样遥测配置:
public class Program
{
public static void Main(string[] args)
{
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((context, services) =>
{
// Retrieve the Application Insights connection string from environment variables
var appInsightsConnectionString = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
// Configure Application Insights telemetry
services.AddSingleton<TelemetryConfiguration>(sp =>
{
var configuration = new TelemetryConfiguration
{
ConnectionString = appInsightsConnectionString
};
// Register the custom sampling telemetry processor
configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder
.Use((next) => new SamplingTelemetryProcessor(next, 10)) // 10% sampling
.Build();
return configuration;
});
// Register TelemetryClient
services.AddSingleton<TelemetryClient>(sp =>
{
return new TelemetryClient(sp.GetRequiredService<TelemetryConfiguration>());
});
})
.Build();
host.Run();
}
}
// Custom Sampling Telemetry Processor
public class SamplingTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor _next;
private static Random _random = new Random();
private double _samplingPercentage;
public SamplingTelemetryProcessor(ITelemetryProcessor next, double samplingPercentage)
{
_next = next;
_samplingPercentage = samplingPercentage;
}
public void Process(ITelemetry item)
{
if (_random.NextDouble() * 100 < _samplingPercentage)
{
_next.Process(item);
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "your-application-insights connection-string"
}
}
输出: