DevelopmentDynamically using Instrumentation Keys with Azure Application Insights

Azure Application Insights is a great way to keep an eye on failures, response times and other performance related data on your Azure App Service and its dependencies.

After setting up Application Insights and using it for a while, it wasn’t all that obvious to me that it was also collecting data for my staging deployment slot. This only came to my attention when I noticed the dependency names for the failures were that of my staging slot. So I found a need to add another Application Insights resource for the staging slot.

Keeping Application Insights resources separated for each deployment slot provides some benefits:

  • Easier to differentiate between failures on different slots
  • Less likely to hit the Application Insights quota
  • If you reach the quota on one Application Insights resource, your other resources will continue collecting data

Adding Application Insights to a deployment slot was easy as navigating to the Application Insights blade within the staging slot and following the prompts to create one.

However, I then needed a way to dynamically set the Instrumentation Key (a configuration key which Application Insights uses to identify the Application Insights resource to use).

There are a few ways you could go about this. You could set up a config transform for ApplicationInsights.config, but this file would be included in your slot swap, and suddenly your production Application Insights would show you data for another slot.

Setting the instrumentation key dynamically

Luckily, when you add an Application Insights resource to a slot, it automatically adds an app setting to your slot called APPINSIGHTS_INSTRUMENTATIONKEY that contains the correct instrumentation key. So we can make use of it:

In your web/API project’s Startup.cs in the App_Start folder, add these lines to Configuration():

// Set the app insights instrumentation key if we have one in our Azure app service.
string instrumentationKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey))
    TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;

Stopping Application Insights from reporting on your development environment

Next, to prevent Application Insights from collecting data from your development environment, change the InstrumentationKey in ApplicationInsights.config to an empty guid: 00000000-0000-0000-0000-000000000000

Alternatively, set TelemetryConfiguration.Active.InstrumentationKeyin Startup.cs to an empty GUID (in string form), but I prefer it in the .config file so it’s clear to other developers (and myself down the line… give it a few weeks and I’ll have forgotten I worked on it) that the instrumentation key used in production isn’t coming from the .config file.
Connect with us

Subscribe to our newsletter today to receive updates on the latest news, releases and special offers. We respect your privacy. Your information is safe.

©2023 Aerion Technologies. All rights reserved | Terms of Service | Privacy Policy