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

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

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

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

是否可以过滤它们?

Filer 基于 ITelemetryProcessor 我已经有了。

在您的活动和就绪探测配置中,您可以指定自定义 header 与请求一起发送,例如

          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 

然后您可以在 ITelemetryProcessor:

的实现中基于 header 进行过滤
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);
        }
    }