我想要在应用程序洞察中记录成功调用的百分比。我偶然在https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling上看到了这篇文章,我认为固定速率采样在这里是合适的。但是,这是否对所有日志记录都有同样的影响?是否将不再记录某些错误/故障?
我正在寻找一个解决方案,记录成功调用的百分比,但保留所有失败的请求/错误。
发布于 2019-04-18 02:45:15
我不认为这是开箱即用的支持,但您可以编写自己的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;
}
}
发布于 2019-04-18 13:25:31
要将失败的事件排除在采样之外(同时对其他所有事件进行采样),请使用此逻辑编写一个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)
https://stackoverflow.com/questions/55731036
复制相似问题