microsoft/ApplicationInsights-dotnet

Alternative solution for deprecated TelemetryConfiguration.Active on Azure Functions

mehric opened this issue · 1 comments

Hi,

I am using TelemetryClient for adding correlation between 2 azure functions communicating with each other using Azure Event Grid. Unlike the Service Bus, the end-to-end transactions don't appear on the logs out of the box with the Event Grid, so I am using the telemetry client to create custom dependency and requests.

However, I am having a problem here.

  • When I am injecting the TelemetryConfiguration into my function, it is completely empty.
  • When I am injecting the IOptions<TelemetryConfiguration> to my function, the TelemetryConfiguration is there but missing the actual config, even the instrumentation key.
  • When I am injecting my customized TelemetryConfiguration as below, it works but the end-to-end transaction logs get weird and loses significant correlations in the logs:
return services
    .AddSingleton(sp =>
    {
        var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
        telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
        var config = sp.GetService<IConfiguration>();
        var instrumentationKey = config.GetValue<string?>("APPINSIGHTS_INSTRUMENTATIONKEY");
        if (instrumentationKey != null)
            telemetryConfiguration.InstrumentationKey = instrumentationKey;
        return telemetryConfiguration;
    });

However, when using the deprecated TelemetryConfiguration.Active it works perfectly. I noticed the Active config has more initializers in addition to the OperationCorrelationTelemetryInitializer, namely speaking:

  • Microsoft.Azure.WebJobs.Logging.ApplicationInsights.WebJobsRoleEnvironmentTelemetryInitializer
  • Microsoft.Azure.WebJobs.Logging.ApplicationInsights.WebJobsTelemetryInitializer

Not sure if that is the reason, but I couldn't find any good documentation or solution for this! Is there an alternative solution to make this work like the TelemetryConfiguration.Active?

For anybody else experiencing issues with custom telemetry on Azure Function, for me, the issue was caused by installing the Microsoft.ApplicationInsights nuget package. Somehow, just instilling that package without a line of code will mess up the default logging on Azure Functions. Uninstall it and instead install Microsoft.Azure.WebJobs.Logging.ApplicationInsights instead, this package includes the Application Insights and all the required collectors on its dependencies. Then without any extra configuration, injecting the TelemetryConfiguration into your class will result in the correct configuration with all the collectors added to it! 😄