Mike Christensen
Mike Christensen

Reputation: 91608

Is it possible to set AppInsights instrumentation key at runtime within an Azure Function

Normally, the instrumentation key for AppInsights is set within the IConfiguration in a key called APPINSIGHTS_INSTRUMENTATIONKEY. This might appear in your appsettings.json file, or in an environment variable, or anything else an IConfigurationBuilder supports. However, what if one wanted to set the instrumentation key at runtime. This is what I expect should work:

public override void Configure(IFunctionsHostBuilder builder)
{
    // Test code
    builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
    {
        InstrumentationKey = "461df8ba-af74-444b-a896-f47c5c6dd02d",
        DeveloperMode = true
    });

    // ... Various configuration stuff
}

When I do this, I can verify that a TelemetryClient is injected into the constructor of my function classes, and I can verify the right instrumentation key was set:

enter image description here

These telemetry items even show up in the local debugger window, so I know the client is processing them:

enter image description here

However, I wait and I wait and they never actually show up in AppInsights. When I put the same instrumentation key in my local settings JSON, everything works perfectly.

Does anyone have a good example showing how to set the AppInsights key at runtime and NOT use IConfiguration. Thanks!

Update:

Dug through some Microsoft docs, and it looks like calling AddApplicationInsightsTelemetry will conflict with the out of the box AppInsights services:

enter image description here

So, I might be out of luck. I'll keep this question open for a bit though, in case anyone has a work around. BTW, the reason I'm asking is my team is building a framework to configure all microservices in a consistent way. Some of our microservices use Azure Functions, so if the standard configuration calls don't work, we'll need to indicate this in the documentation or perhaps throw an exception or something.

Upvotes: 6

Views: 1228

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

Please try install this package Microsoft.Azure.WebJobs.Logging.ApplicationInsights.

Then in your Startup.cs, the code looks like below:

    public override void Configure(IFunctionsHostBuilder builder)
    {            
        builder.Services.AddSingleton(sp =>
        {
            return new TelemetryConfiguration("put the instrumentation key here");
        });
    }

Then in the function, use TelemetryClient to send data:

    private readonly TelemetryClient telemetryClient;

    public Function1(TelemetryClient telemetryClient)
    {
        this.telemetryClient = telemetryClient;
    }

    [FunctionName("Function1")]
    public void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
    {
        telemetryClient.TrackTrace("11 track trace 2222...");
    }

Upvotes: 1

Related Questions