Application Insights 和 kubernetes:如何不将成功的 /liveness 和 /hc 探针记录到跟踪日志

Dmi*_*try 2 azure azure-application-insights asp.net-core azure-aks

我已经使用 kubernetes 在 Azure 中部署了一个 aspnetcore 项目。

我正在使用 Application Insights,并且我不想从 kubernetes 收到数千条有关成功的活动性和就绪性 (/liveness/hc) 探测的消息。

是否可以过滤它们?

我已经拥有基于ITelemetryProcessor 的文件管理器。

在此输入图像描述

Pet*_*ons 6

在活性和就绪探针的配置中,您可以指定与请求一起发送的自定义标头,例如

          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 
Run Code Online (Sandbox Code Playgroud)

然后,您可以在实现中根据该标头进行过滤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);
        }
    }
Run Code Online (Sandbox Code Playgroud)