Search code examples
azureasp.net-coreazure-application-insightsazure-aks

Application Insights and kubernetes: How to not log successful /liveness and /hc probes to trace logs


I have deployed an aspnetcore project in Azure with kubernetes.

I'm using Application Insights, and I do not want to get thousands messages about successful liveness and readiness (/liveness and /hc) probes from kubernetes.

Is it possible to filter them?

Filer based on ITelemetryProcessor I already have.

enter image description here


Solution

  • In your configuration of the liveness and readiness probe you can specify custom headers to send along with the request, for example

              livenessProbe:
                httpGet:
                  path: /api/health
                  port: http
                  httpHeaders:
                  - name: HealthProbe-Type
                    value: Liveness
              readinessProbe:
                httpGet:
                  path: /api/health
                  port: http
                  httpHeaders:
                  - name: HealthProbe-Type
                    value: Readiness 
    

    You can then filter based on that header in your implementation of ITelemetryProcessor:

    public class HealthProbeTelemetryProcessor : ITelemetryProcessor
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            private readonly ITelemetryProcessor _nextProcessor;
            public static string HealthProbeHeaderName => "HealthProbe-Type";
    
            public HealthProbeTelemetryProcessor(IHttpContextAccessor httpContextAccessor, ITelemetryProcessor nextProcessor)
            {
                _httpContextAccessor = httpContextAccessor;
                _nextProcessor = nextProcessor;
            }
    
            public void Process(ITelemetry item)
            {
                if (item == null) throw new ArgumentNullException(nameof(item));
    
                if (!string.IsNullOrWhiteSpace(item.Context.Operation.SyntheticSource))
                    return;
    
                var isNotRequestTelemetry = !(item is RequestTelemetry);
                
                if ((isNotRequestTelemetry || _httpContextAccessor.HttpContext == null || !(_httpContextAccessor.HttpContext.Request?.Headers.ContainsKey(HealthProbeHeaderName)).GetValueOrDefault()))
                    _nextProcessor.Process(item);
            }
        }