Application Insights - 如何设置自定义操作 ID

Pra*_*win 4 azure-application-insights

在我们当前的本地设置中,我们有 20 多个 .net core 3.1 API 应用程序(单独的 ASP.NET Core API 应用程序)。我们已开始将 2 个 APi 应用程序迁移到带有单个 Application Insights 实例标记的 Azure 应用服务。

在 On-Prem 中,我们使用其他 18 个应用程序中的其他日志框架。所有这些 API 应用程序都会相互通信,并且所有日志都与本地的某些 unique_id 相关联。

现在,对于 Azure 中的 api,我们需要利用相同的 unique_Id 并将所有内容关联起来。

为了实现这一目标,我开始探索为 Azure 中托管的 2 个应用程序设置相同操作 ID 的功能。

在两个 API 中创建了 TelemetrInitializer。如果我在两个 API 中设置操作 ID(如下所示),那么它就可以工作。所有日志都与单一操作 ID“12345”相关联

      telemetry.Context.Operation.Id = "12345";
Run Code Online (Sandbox Code Playgroud)

但是,由于很明显要使操作 ID 成为动态的,因此我在第一个 API 中将其更改为以下内容

  telemetry.Context.Operation.Id = "CR" + Guid.NewGuid().ToString();
Run Code Online (Sandbox Code Playgroud)

因此,下一个挑战是,我需要将这个新的操作 ID 绑定到我的第二个 API 的 TelemetryInitiializer 中。为了实现这一目标,我尝试在第二个 API 的 TelemetryInitializer 中获取 Request-Id 标头。它始终为 NULL。

有办法实现这一点吗?

谢谢,普拉文·斯里拉姆。

Sam*_*ucy 5

tldr:这可以通过禁用 .NET Core 和 App Insights 中内置的依赖项跟踪并自行处理来实现。在大多数情况下,最好的办法是让 .NET Core 和 App Insights 进行跟踪。

我上传了一个简单的 WebAPI 应用程序,其中包含我要转到 Github 的代码: https: //github.com/SamaraSoucy-MSFT/customoperationid

为了获取标头和 App Insights 来获取自定义操作 ID,需要重写两件事。第一个是包装 HttpClient 的 Activity,因为它控制相关标头。第二个是 App Insights 中的依赖项跟踪。

可以在 HttpClients 中完全禁用操作,但为了最大限度地减少副作用,您可以通过设置来删除客户端中的操作Activity.Current = null

var operationId = "CR" + Guid.NewGuid().ToString();
var url = "https://www.microsoft.com";
using (var client = new HttpClient())
{
    using (var requestMessage =
        new HttpRequestMessage(HttpMethod.Get, url))
    {
        //Makes the headers configurable
        Activity.Current = null;

        //set correlation header manually
        requestMessage.Headers.Add("Request-Id", operationId);
        await client.SendAsync(requestMessage);
    }
}
Run Code Online (Sandbox Code Playgroud)

下一步是删除 App Insights对此请求的默认跟踪。同样,您可以完全禁用依赖项跟踪,也可以过滤掉此请求的默认遥测。就像初始化器一样,处理器在 Startup 类中注册。

services.AddApplicationInsightsTelemetryProcessor<CustomFilter>();

public class CustomFilter : ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; set; }

    // next will point to the next TelemetryProcessor in the chain.
    public CustomFilter(ITelemetryProcessor next)
    {
        this.Next = next;
    }

    public void Process(ITelemetry item)
    {
        // To filter out an item, return without calling the next processor.
        if (!OKtoSend(item)) { return; }

        this.Next.Process(item);
    }

    // Example: replace with your own criteria.
    private bool OKtoSend(ITelemetry item)
    {
        var dependency = item as DependencyTelemetry;

        if (dependency == null) return true;

        if (dependency.Type == "Http"
            && dependency.Data.Contains("microsoft.com")
            //This key is just there to help identify the custom tracking
            && !dependency.Context.GlobalProperties.ContainsKey("keep"))
        {
            return false;
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,在进行远程调用的方法中,您需要注入遥测客户端并调用TelemetryClient.TrackDependency()

var operationId = "CR" + Guid.NewGuid().ToString();

//setup telemetry client
telemetry.Context.Operation.Id = operationId;
if (!telemetry.Context.GlobalProperties.ContainsKey("keep"))
{
    telemetry.Context.GlobalProperties.Add("keep", "true");
}
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
//continue setting up context if needed

var url = "https:microsoft.com";
using (var client = new HttpClient())
{
    //Makes the headers configurable
    Activity.Current = null;

    using (var requestMessage =
        new HttpRequestMessage(HttpMethod.Get, url))
    {
        //Makes the headers configurable
        Activity.Current = null;

        //set header manually
        requestMessage.Headers.Add("Request-Id", operationId);
        await client.SendAsync(requestMessage);
    }
}

//send custom telemetry 
telemetry.TrackDependency("Http", url, "myCall", startTime, timer.Elapsed, true);
Run Code Online (Sandbox Code Playgroud)