.net - 遥测采样而不影响错误/故障

标签 .net azure azure-application-insights telemetry

我想在应用洞察中记录成功调用的百分比。 我看到这篇文章https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling我认为固定速率采样在这里是合适的。但这是否会同样影响所有日志记录?某些错误/失败将不再被记录吗?

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

最佳答案

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

参见:https://learn.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;
    }
}

关于.net - 遥测采样而不影响错误/故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731036/

相关文章:

Azure AD Multi-Tenancy 应用程序

asp.net-mvc - Azure 虚拟机上的远程调试生产 IIS

Azure 应用程序洞察

c# - 无法在C#中为列表添加值

c# - 对 SSPI 的调用失败,请参阅内部异常 - 无法联系本地安全机构

.net - .net 4.0 中的云计算

c# - 构建最有效的查询发现如果日期范围在一个间隔内?

azure - 从 Windows 桌面应用程序调用具有 AD 授权的 Azure Function

asp.net-core - 为多个客户端提供服务的 Web API 的 Application Insights 策略

java - Application Insights 支持 Java 的多种环境