Application Insights如何设置自定义操作Id



在我们当前的on-prem设置中,我们有20+.net核心3.1 API应用程序(单独的ASP.net核心API应用程序(。我们已经开始将2个APi应用程序迁移到使用单个Application Insights实例标记的Azure应用程序服务。

在On Prem中,我们使用了其他18个应用程序中的一些日志框架。所有这些API应用程序相互通信,所有日志都绑定到on-prem中的某个unique_id。

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

为了实现这一点,我开始探索为azure中托管的两个应用程序设置相同操作Id的功能。

在两个API中都创建了TelemetryInitializer。如果我在两个API中都设置了OperationalId,如下所示,它就可以工作了。所有日志都绑定到Single Operation Id";12345";

telemetry.Context.Operation.Id = "12345";

然而,很明显,操作Id是动态的,我在我的第一个API 中将其更改为以下内容

telemetry.Context.Operation.Id = "CR" + Guid.NewGuid().ToString();

因此,下一个挑战是,我需要将这个新的操作Id绑定到我的第二个API的TelemetryInitiializer中。为了实现这一点,我试图在第二个API的TelemetryInitializer中获取Request-Id头。它总是空的。

有办法做到这一点吗?

谢谢,Praveen Sreeram。

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

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

有两件事需要重写才能获得标头和App Insights来获得自定义操作Id。第一件事是Activity,它将HttpClient包装为控制相关性标头的对象。第二个是App Insights中的依赖项跟踪。

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

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);
}
}

下一步是删除此请求的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;
}
}

最后,在进行远程调用的方法中,您需要注入一个遥测客户端并调用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);

最新更新