How to ignore / drop HealthCheck request samples in Application Insights #5843
-
Hi Team, How to drop the samples of health check requests or ignore them when using Azure Monitor Open Telemetry. using .Net 8 and packages Azure.Monitor.OpenTelemetry.Exporter --version 1.3.0 & Microsoft.ApplicationInsights --version 2.22.0 Below are the configurations updated a bit based on LINK {
public class HealthCheck : IHealthCheck
{
public static readonly HealthCheck _healthCheck = new HealthCheck();
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
return Task.FromResult(HealthCheckResult.Healthy());
}
[Function("HealthCheck")]
public static async Task<IActionResult> HealthCheckStatus(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "health")] HttpRequest req, FunctionContext context)
{
var healthContext = new HealthCheckContext();
var healthStatus = await _healthCheck.CheckHealthAsync(healthContext);
return new OkObjectResult(Enum.GetName(typeof(HealthStatus), healthStatus.Status));
}
}
public class CustomTelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CustomTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
}
public void Initialize(ITelemetry telemetry)
{
var requestPath = _httpContextAccessor.HttpContext?.Request.Path.Value;
if (string.IsNullOrWhiteSpace(requestPath))
return;
// List of paths to sample out
var pathsToSampleOut = new[] { "/api/health" };
if (pathsToSampleOut.Any(path => requestPath.Contains(path, StringComparison.OrdinalIgnoreCase)))
{
// We don't want to track these requests in the metrics.
if (telemetry is ISupportAdvancedSampling advancedSampling)
advancedSampling.ProactiveSamplingDecision = SamplingDecision.SampledOut;
// For the case that we cannot filter out the telemetry, we mark it as synthetic
if (string.IsNullOrWhiteSpace(telemetry.Context.Operation.SyntheticSource))
telemetry.Context.Operation.SyntheticSource = "FilteredRequest";
}
}
}
public class CustomTelemetryProcessor : ITelemetryProcessor
{
private readonly ITelemetryProcessor _next;
public CustomTelemetryProcessor(ITelemetryProcessor next)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
}
public void Process(ITelemetry item)
{
if (!string.IsNullOrEmpty(item.Context.Operation.SyntheticSource)) { return; }
_next.Process(item);
}
}
} have initialized the exporters var resource = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("GetCatalystOneEvent")
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation(options =>
{
options.Filter = context => !context.Request.Path.Equals("/api/health");
})
.SetResourceBuilder(resource)
.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = connectionString;
})
.Build();
MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.SetResourceBuilder(resource)
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = connectionString;
})
.Build();
var host = Host.CreateDefaultBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddHttpClient();
services.AddHealthChecks();
services.AddSingleton(tracerProvider);
services.AddSingleton(meterProvider);
services.AddApplicationInsightsTelemetryWorkerService();
services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
services.AddSingleton<ITelemetryProcessor, CustomTelemetryProcessor>();
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @dhayanands, This is the wrong repo for issues related to Azure.Monitor.OpenTelemetry.Exporter. |
Beta Was this translation helpful? Give feedback.
Hi @dhayanands, This is the wrong repo for issues related to Azure.Monitor.OpenTelemetry.Exporter.
Please open an issue in that SDK's repo: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter