我什么时候可以获得Application Insights操作ID?

时间:2016-08-25 15:56:16

标签: c# asp.net-core-mvc azure-application-insights

我有一个写入EventHub的AspNetCore网络应用程序和一个从中读取的webjob。我希望此事务的两个部分中的遥测在Application Insights中具有相同的操作ID。

因此,当我要将数据发送到EventHub时,我尝试将操作ID从TelemetryClient中拉出来,例如。

var myOperationId = MyTelemetryClient.Context.Operation.Id;

但这总是让我无效。我找到了article并尝试使用

var request.HttpContext.Items["Microsoft.ApplicationInsights.RequestTelemetry"] as RequestTelemetry;

但是再次无效。在我需要的时候我可以提取这个值的任何指针吗?

我的代码如下所示:

public class Startup
{
    public void ConfigureServices( IServiceCollection IServices )
    {
        var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
        builder.Use((next) => new MyTelemetryProcessor(next));
        builder.Build();

        var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
        aiOptions.EnableQuickPulseMetricStream = true;

        IServices.AddApplicationInsightsTelemetry( Configuration, aiOptions);
        IServices.AddMvc();
        IServices.AddOptions();

        TelemetryClient AppTelemetry = new TelemetryClient();
        AppTelemetry.InstrumentationKey = InsightsInstrumentationKey;
        IServices.AddSingleton(typeof(TelemetryClient), AppTelemetry);
    }
    public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory )
    {
        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();

        var configuration = app.ApplicationServices.GetService<TelemetryConfiguration>();
        configuration.TelemetryInitializers.Add(new MyTelemetryInitializer());
    }
}

[Route("[controller]")]
public class MyController
{
    private readonly TelemetryClient mTelemetryClient;

    public MyController(
        TelemetryClient TelemetryClientArg)
    {
        mTelemetryClient = TelemetryClientArg;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]MyPostDataClass MyPostData)
    {
        string telemetryId = mTelemetryClient.Context.Operation.Id; // this is null

        return Ok();
    }
}

4 个答案:

答案 0 :(得分:5)

想想我最终破解了这个而没有创建不必要的遥测。以下是针对AspNetCore的,但只要操作ID初始化程序可用,就应该进行转换:

var operationId = default(string);
try
{
    var telemetry = new RequestTelemetry();

    TelemetryConfiguration
        .Active
        .TelemetryInitializers
        .OfType<OperationIdTelemetryInitializer>()
        .Single()
        .Initialize(telemetry);

    operationId = telemetry.Context.Operation.Id;

}
catch { }

答案 1 :(得分:0)

Asp.Net核心软件包有一个OperationIdTelemetryInitializersee github),它试图从请求中设置它。假设您有请求跟踪并且此遥测初始化程序已打开,则该请求中发生的所有内容都会设置Operation.Id。

如果它不适合您,您可以创建自己的自定义TelemetryInitializer来设置它,无论您需要什么,或者,在您确定它为空的地方,您可以设置在那里,那个环境中的所有东西都应该有那个id。

答案 2 :(得分:0)

您可以通过发送遥测数据然后从遥测对象读回操作ID来获取此值。不优雅,但它有效。

[HttpPost]
public async Task<IActionResult> Post([FromBody]MyPostDataClass MyPostData)
{
   var dummyTrace = new TraceTelemetry("hello", SeverityLevel.Verbose);
   mTelemetryClient.TrackTrace(dummyTrace);
   string opId = dummyTrace.Context.Operation.Id;

   return Ok();
}

答案 3 :(得分:0)

我的OperationIdTelemetryInitializer中没有TelemetryConfiguration .Active.TelemetryInitializers

但这为我提供了当前的操作ID:

System.Diagnostics.Activity.Current.RootId

https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/504