Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUERY] How to ignore / drop HealthCheck request samples in Application Insights #46056

Open
dhayanands opened this issue Sep 19, 2024 · 2 comments
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@dhayanands
Copy link

dhayanands commented Sep 19, 2024

Library name and version

Azure.Monitor.OpenTelemetry.Exporter 1.3.0

Query/Question

Hi Team,

How to drop the samples of health check requests or ignore them when using Azure Monitor Open Telemetry.

using functions .Net 8 Isolated Model 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>();
    })

image

Environment

Azure Functions isolated Model with .Net 8

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Sep 19, 2024
@jsquire jsquire added Service Attention Workflow: This issue is responsible by Azure service team. Client This issue points to a problem in the data-plane of the library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team Monitor - Exporter Monitor OpenTelemetry Exporter and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Sep 19, 2024
Copy link

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @cijothomas @rajkumar-rangaraj @reyang @TimothyMothra.

@TimothyMothra
Copy link
Contributor

Hi @dhayanands,
It looks like you're mixing two different telemetry SDKs. This is not recommended.
Please onboard to either one or the other.

Your filtering code looks correct, but I can't begin to troubleshoot your configuration until you switch to using one telemetry SDK or the other.

Application Insights SDK

This is our classic SDK. This is fully supported and feature rich.

services.AddApplicationInsightsTelemetryWorkerService();
services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
services.AddSingleton<ITelemetryProcessor, CustomTelemetryProcessor>();

Azure Monitor Exporter & OpenTelemetry

This is our newer SDK which will replace the classic SDK.

.AddAzureMonitorTraceExporter(options =>
.AddAzureMonitorMetricExporter(options =>

Functions

I see that you're using Azure Functions.
Please pay attention to their specific onboarding instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

3 participants