【问题标题】:Telemetry sampling without affecting the errors/failures在不影响错误/故障的情况下进行遥测采样
【发布时间】:2019-09-07 21:44:51
【问题描述】:

我想在应用洞察中记录成功调用的百分比。 我遇到了这个帖子https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling,我认为固定速率采样在这里是合适的。但这是否同样影响所有日志记录?是否会不再记录某些错误/故障?

我正在寻找一种解决方案,可以记录一定百分比的成功调用,但保留所有失败的请求/错误。

【问题讨论】:

    标签: .net azure azure-application-insights telemetry


    【解决方案1】:

    我不认为这是开箱即用的支持,但您可以编写自己的 ITelemetryProcessor

    见:https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#filtering-itelemetryprocessor

    .NET 中的 Application Insights 使用可用于过滤遥测数据的遥测处理器链,因此您可以编写自己的代码来检查 resultCode(我认为 Application Insights 称之为 HTTP 状态代码,但您必须仔细检查)请求遥测对象,如果它是 500(或 5xx)则批准它,但如果它是 2xx 或 3xx,则只有 10% 的机会发送它。您可以重写OKToSend()方法对ITelemetry输入进行上述检查,并相应地返回真/假。

    可能是这样的(我在浏览器中写了这个,它不一定能按原样完美运行):

    // Approves 500 errors and 10% of other telemetry objects
    private bool OKtoSend (ITelemetry telemetry)
    {
        if (telemetry.ResponseCode == 500) {
            return true;
        } else {
            Random rnd = new Random();
            int filter = rnd.Next(1, 11);
            return filter == 1;
        }
    }
    

    【讨论】:

    • 我无法调用 item["resultCode"],因为我得到“无法将 [] 索引应用于 ITelemetry 类型的表达式”。 item as DependencyTelemetry() 具有 ResultCode 属性(字符串)。 docs.microsoft.com/en-us/dotnet/api/…
    • @Wouter 我猜你想要RequestTelemetry (docs.microsoft.com/en-us/dotnet/api/…) 类而不是DependencyTelemetry,因为你正在记录请求,HTTP 响应代码的正确属性是@ 987654331@.
    • 似乎一切都通过了 ITelemetryProcessor。我将不得不分别对待它们。
    • 我还有一个问题。一切都需要分开处理。我不想丢弃与异常相关的依赖项(否则可能有不完整的数据需要调试)。有没有办法将所有内容放在一起或找出哪些依赖项与异常/错误相关?
    • @Wouter 在我使用大量 Application Insights 遥测的 Rails 应用程序之一中,有一个自定义记录器,可将自定义跟踪发送到应用程序洞察力并将任意对象转换为 JSON,我曾经使用过用自定义数据补充默认的异常捕获。我不喜欢这种解决方案,并且强烈怀疑有更好的方法可以使用自定义遥测处理器来实现,但这是一种选择。
    【解决方案2】:

    为了排除失败的事件进行采样,(同时对其他所有内容进行采样)使用此逻辑编写TelemetryInitializer

    public class PreventSamplingForFailedTelemetryInitializer: ITelemetryInitializer
    {
      public void Initialize(ITelemetry telemetry)
      {
            if(failed)
            {
                // Set to 100, so that actual SamplingProcessors ignore this from sampling considerations.
                ((ISupportSampling)telemetry).SamplingPercentage = 100;
            }
       }
    }
    
    (Make sure to add this TelemetryInitializer to the TelemetryConfiguration)
    
    Failed or not can be determined from RequestTelemetry and DependencyTelemetry from their `Success` field.
    
    (the last one in FAQ sections has hints to answer your question https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling#frequently-asked-questions)
    

    【讨论】:

    • 顺便说一句-您可以将上述内容与 AdaptiveSampling 一起使用。在大多数情况下,AdaptiveSampling 效果最好,并且是在 asp.net 和 asp.net 核心应用程序中启用的默认采样。
    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2021-01-28
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    相关资源
    最近更新 更多