Azure App Insights智能警报的自定义排除



当我们的应用程序中出现异常时,我们正在Azure App Insights中使用智能检测器来生成一些警报。然而,在我们的代码中有一些故意的失败,我们抛出了403。有没有一种方法可以修改这些";智能警报";在Application Insights中,以便在其检测逻辑中排除这些已知故障?我们有一个与这些预期故障相关的特定异常类型,如果有办法的话,我们可以很容易地使用它在异常检测中排除这些故障,但我在UI上找不到这样做的选项。

谢谢你的指点。

您不能直接从Azure门户执行此操作,但您需要实现一个遥测处理器,该处理器可以帮助您覆盖遥测属性集。

如果请求标志为失败,则响应代码=403。但是,如果您想将其视为成功,可以提供一个遥测初始化器来设置success属性。

定义初始值设定项

C#

using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace MvcWebRole.Telemetry
{
/*
* Custom TelemetryInitializer that overrides the default SDK
* behavior of treating response codes >= 400 as failed requests
*
*/
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
// Is this a TrackRequest() ?
if (requestTelemetry == null) return;
int code;
bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
if (!parsed) return;
if (code >= 400 && code < 500)
{
// If we set the Success property, the SDK won't change it:
requestTelemetry.Success = true;
// Allow us to filter these requests in the portal:
requestTelemetry.Properties["Overridden400s"] = "true";
}
// else leave the SDK to set the Success property
}
}
}

在ApplicationInsights.config:中

XMLCopy

<ApplicationInsights>
<TelemetryInitializers>
<!-- Fully qualified type name, assembly name: -->
<Add Type="MvcWebRole.Telemetry.MyTelemetryInitializer, MvcWebRole"/>
...
</TelemetryInitializers>
</ApplicationInsights>

有关详细信息,请参阅本文档。

最新更新