Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I get an Application Insights operation id?

I have a AspNetCore web app that writes to EventHub and a webjob that reads from it. I'd like the telemetry from both parts of this transaction to have the same operation id in Application Insights.

So, when I'm about to send the data to EventHub I try to pull the operation id out of the TelemetryClient, e.g.

var myOperationId = MyTelemetryClient.Context.Operation.Id;

But this always gives me null. I found this article and tried using

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

But again null. Any pointers on a way I can extract this value when I need it?

My code looks like this:

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();
    }
}
like image 318
Iain Brown Avatar asked Aug 25 '16 15:08

Iain Brown


People also ask

How do I get application Insights ID?

To get the Application ID: From your Application Insights resource, click API Access. Copy the Application ID and paste it to the Application Insights Application ID field of your bot's settings.

What is Operation ID in app Insights?

Application Insights defines a data model for distributed telemetry correlation. To associate telemetry with a logical operation, every telemetry item has a context field called operation_Id . This identifier is shared by every telemetry item in the distributed trace.

How do I enable application Insights in Azure function app?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.

How do I enable applications Insights NET application?

Open your project in Visual Studio. Go to Project > Add Application Insights Telemetry. Choose Azure Application Insights, then select Next. Choose your subscription and Application Insights instance (or create a new instance with Create new), then select Next.


1 Answers

I did not have OperationIdTelemetryInitializer in my TelemetryConfiguration .Active.TelemetryInitializers.

But this provides me with the current operation id:

System.Diagnostics.Activity.Current.RootId

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

like image 146
Gopal Krishnan Avatar answered Sep 27 '22 19:09

Gopal Krishnan