遥测采样不会影响错误/故障

问题描述 投票:3回答:2

我想在应用洞察中记录一定比例的成功通话。我遇到了这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling,我认为固定费率抽样在这里是合适的。但这是否同样影响所有日志记录?是否会记录某些错误/失败?

我正在寻找一种解决方案,记录一定比例的成功呼叫,但保留所有失败的请求/错误。

.net azure azure-application-insights telemetry
2个回答
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输入执行上述检查,并相应地返回true / false。

也许是这样的(我在浏览器中写道,它不一定能完美无缺地工作):

// 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;
    }
}

1
投票

要排除失败事件不受采样影响(在对其他所有事情进行采样时),请使用此逻辑编写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)
© www.soinside.com 2019 - 2024. All rights reserved.