我想在应用洞察中记录一定比例的成功通话。我遇到了这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling,我认为固定费率抽样在这里是合适的。但这是否同样影响所有日志记录?是否会记录某些错误/失败?
我正在寻找一种解决方案,记录一定比例的成功呼叫,但保留所有失败的请求/错误。
我不认为这是开箱即用的,但你可以编写自己的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;
}
}
要排除失败事件不受采样影响(在对其他所有事情进行采样时),请使用此逻辑编写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)