首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不影响错误/故障的遥测采样

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

Stack Overflow用户
提问于 2019-04-17 23:20:49
回答 2查看 341关注 0票数 3

我想要在应用程序洞察中记录成功调用的百分比。我偶然在https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling上看到了这篇文章,我认为固定速率采样在这里是合适的。但是,这是否对所有日志记录都有同样的影响?是否将不再记录某些错误/故障?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-18 02:45:15

我不认为这是开箱即用的支持,但您可以编写自己的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。

也许是这样的(我在浏览器中写了这段代码,它不一定能完美地工作):

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2019-04-18 13:25:31

要将失败的事件排除在采样之外(同时对其他所有事件进行采样),请使用此逻辑编写一个TelemetryInitializer

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55731036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档