【问题标题】:Custom exclusions for Azure App Insights Smart AlertsAzure App Insights 智能警报的自定义排除项
【发布时间】:2021-09-14 20:21:07
【问题描述】:

我们使用 Azure App Insights 中的智能检测器在我们的应用出现异常时生成一些警报。但是,我们的代码中存在一些故意的故障,我们会抛出 403。有没有办法在 Application Insights 中修改这些“智能警报”,以便在其检测逻辑中排除这些已知故障?我们有一个与这些预期故障相关的特定异常类型,如果有办法,我们可以很容易地在异常检测中使用它来排除这些故障,但我在 UI 上找不到执行此操作的选项。

感谢您的任何指点。

【问题讨论】:

    标签: azure azure-application-insights appinsights


    【解决方案1】:

    您无法直接从 Azure 门户执行此操作,但您需要实现 Telemetry Processor,它可以帮助您覆盖遥测属性集。

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

    定义你的初始化器

    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 中:

    XML复制

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

    更多信息可以参考这个Document

    【讨论】:

    • 不幸的是,我不认为让每个 403 都成功对我们有用。它只是一个特定的代码路径,它引发了我想视为成功的特定类型的自定义异常。
    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2020-04-16
    • 2018-12-13
    相关资源
    最近更新 更多