ITelemetryProcessor打破了依赖跟踪

时间:2018-08-01 15:33:20

标签: asp.net-core .net-core azure-application-insights

我已经为.NET Core 2.1 Web API设置了自定义ITelemetryProcessor。我不希望HTTP状态代码400或401在Application Insights中显示为错误。下面的类可以做到这一点,但是一旦我使用它,SQL和HTTP客户端依赖项跟踪将不再起作用:

ApplicationInsights400IsOkProcessor

public class ApplicationInsights400IsOkProcessor : ITelemetryProcessor {

    private ITelemetryProcessor Next { get; set; }


    /// <summary>
    /// Link processors to each other in a chain. 
    /// </summary>
    /// <param name="next"></param>
    public ApplicationInsights400IsOkProcessor(ITelemetryProcessor next) {
        this.Next = next;
    }

    /// <summary>
    /// API Aufrufe mit Rückgabecode 400 und 401 sind keine Fehler
    /// </summary>
    /// <param name="item">Telemetry zum Verarbeiten</param>
    public void Process(ITelemetry item) {
        var requestTelemetry = item as RequestTelemetry;          
        if (requestTelemetry == null) return;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out int code);
        if (!parsed) return;
        switch (code) {
            case 400:
                requestTelemetry.Success = true;
                requestTelemetry.Context.Properties["Overridden400"] = "true";
                break;
            case 401:
                requestTelemetry.Context.Properties["Overridden401"] = "true";
                requestTelemetry.Success = true;
                break;
        }

        this.Next.Process(item);
    }

在.NET Core 2.0中,我使用了一个自定义的ITelemetryInitializer,例如Microsoft的以下示例: https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer

这是通过将以下内容添加到Configure Services()来使用的:

TelemetryConfiguration.Active.TelemetryInitializers
    .Add(new MyTelemetryInitializer());

在.NET Core 2.1中,这似乎不再起作用

更新: 基于I的回答,cijothomas审查了代码以确保在所有情况下均调用this.Next.Process(item)。下面是工作版本:

ApplicationInsights400IsOkProcessor

 public class ApplicationInsights400IsOkProcessor : ITelemetryProcessor {

    private ITelemetryProcessor Next { get; set; }

    public ApplicationInsights400IsOkProcessor(ITelemetryProcessor next) {
        this.Next = next;
    }

    public void Process(ITelemetry item) {
        if (item is RequestTelemetry requestTelemetry) {
            bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out int code);
            if (parsed) {
                switch (code) {
                    case 400:
                        requestTelemetry.Success = true;
                        requestTelemetry.Context.Properties["Overridden400"] = "true";
                        break;
                    case 401:
                        requestTelemetry.Context.Properties["Overridden401"] = "true";
                        requestTelemetry.Success = true;
                        break;
                }
            }
        }

        this.Next.Process(item);
    }

}

1 个答案:

答案 0 :(得分:3)

并非在所有情况下都调用next.Process(),因此会导致项目被丢弃。您的方案要求使用ITelemetryInitializer而不是ITelemetryProcessor,因为您只是在尝试修改自动收集的信息,而不是根据条件删除项目。

https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer

类似以下内容:

 public class ApplicationInsights400IsOkInitializer: ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out int code);
        if (!parsed) return;
        switch (code) 
        {
        case 400:
            requestTelemetry.Success = true;
            requestTelemetry.Context.Properties["Overridden400"] = "true";
            break;
        case 401:
            requestTelemetry.Context.Properties["Overridden401"] = "true";
            requestTelemetry.Success = true;
            break;
         }
    }
  }