How to make Application Insights telemetry meaningful? Configuration, enrichment and exclusion - Part 2
How to enrich Application Insights telemetry with user id, build version and role name, exclude the noise, and correlate requests across components.
How to make Application Insights telemetry meaningful? Configuration, enrichment and exclusion - Part 2
Enabling Application Insights is only the first step, the telemetry which is coming out of the box is not always useful. Requests from health checks and bots fill the logs, dependencies show up with generic names, and nobody knows which build or which user produced a failure. The cost goes up, and the signal goes down. In the previous article, we discussed the instance strategy and how to enable Application Insights for an application. In this article, we are going to see what we must add to every telemetry item, what we should leave out and how to make sure a request can be followed across the components.
TL;DR Summary
- Include User Id and session Id in the telemetry whenever possible.
- Include the application build version, and define a readable role name using a telemetry initializer.
- Configure the release pipeline to add release annotations into Application Insights.
- Exclude the unnecessary user agents and health check URLs, otherwise they will increase the data volume with no value.
- Enable CORS for the correlation headers
x-ms-request-idandx-ms-root-idfor all dependencies. - Use a custom dependency tracker to give a readable name to the dependencies which are not recognized automatically.
Why is raw telemetry not enough?
When we look at the default telemetry, we will see the following gaps,
No context
A failed request tells us what happened, but not who was affected and which version was running. Without user id, session id and build version, it is difficult to reproduce the issue, and it is also difficult to say whether the issue started after a deployment.
Unreadable names
Application Map and log analytics show whatever role name the telemetry carries, and it is not always readable. When many applications are monitored in one instance (and that is what we recommended in Part 1), we must have a readable role name to tell the primary site from the secondary site, or the web application from the API.
Noise
Health check requests from a load balancer or an always-on bot are sending requests every few seconds. Nobody is interested in them, but we are paying for the ingestion.
Broken trace across components
When a request travels from the browser to the web application, and then to an API, we expect to see one trace. If the correlation headers are blocked or the dependency is not recognized, the trace is broken in the middle and we are back to blind spots.
Let us see how we can fix each of these. The following diagram depicts the telemetry pipeline inside the application, and the places where we can control the data,

Telemetry initializers add the context, telemetry processors filter the data, and sampling reduces the volume before the data is sent to Application Insights. We will see the sampling in the next article.
How to enrich the telemetry?
Telemetry initializer is a class which is called for every telemetry item before it is sent, so it is the right place to add the common information. The following are mandatory for us,
- Should include User Id and session Id whenever possible.
- Should include the application build version.
- Must define the proper application role name, so that we have a readable name in Application Map as well as in log analytics.
- Must configure the Azure DevOps pipeline to add the release annotations.
Note: Release annotation marks a deployment on the charts. It helps to see whether a spike or a failure started just after a release, and it is mandatory to automate it in the build pipeline. Refer the release annotations documentation for the steps.
How to set the application/build version?
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Component.Version = "v2.1";
}
}
How to set the custom role name?
Server (C#):
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
namespace CustomInitializer.Telemetry
{
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
{
//set custom role name here
telemetry.Context.Cloud.RoleName = "Custom RoleName";
telemetry.Context.Cloud.RoleInstance = "Custom RoleInstance";
}
}
}
}
Client (JavaScript):
appInsights.queue.push(() => {
appInsights.addTelemetryInitializer((envelope) => {
envelope.tags["ai.cloud.role"] = "your role name";
envelope.tags["ai.cloud.roleInstance"] = "your role instance";
});
});
How to set the user id and session id into a request?
Server (C#):
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.User.Id = "<assign your application user id here>";
telemetry.Context.Session.Id = "<assign current session id here>";
}
Client (JavaScript):
appInsights.addTelemetryInitializer((envelope) => {
envelope.tags["ai.user.id"] = "<assign your application user id here>";
envelope.tags["ai.session.id"] = "<assign your session id here>";
});
Warning: User id and session id may be personal data. Use an application specific id and refer the personal data documentation before you decide what to store.
Sample telemetry initializer
In real projects, we put everything in one initializer. Find below a sample, which sets the version, role name, user id, session id and the custom properties. The role name here is a placeholder, read it from the configuration file.
public class ApplicationInsightsInitializer : ITelemetryInitializer
{
private readonly string _applicationVersion = GetApplicationVersion();
private readonly string _roleName = GetRoleName();
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Component.Version = _applicationVersion;
telemetry.Context.Cloud.RoleName = _roleName;
telemetry.Context.User.Id = GetUserId();
telemetry.Context.Session.Id = GetSessionId();
// Global here means global for the application - something which won't change for every request.
telemetry.Context.GlobalProperties["key"] = "";
// use for properties to add custom data to specific telemetry item
((ISupportProperties)telemetry).Properties["key"] = "";
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
// use for properties to add custom data to specific request
requestTelemetry.Properties["key"] = "";
}
private static string GetApplicationVersion()
{
//find the application version from assembly or other sources
return "2.1";
}
private static string GetRoleName()
{
//read it from configuration file
return "Product Web - Primary Site";
}
private string GetUserId() { return ""; } //fetch and return the user id
private string GetSessionId() { return ""; } //fetch and return the session id
}
How to exclude the unnecessary data?
Not all requests are worth tracking. It is important to remove the noise at the source, otherwise we pay for the data and we also make it difficult to find the real problems.
Must Do:
- Exclude the requests from specific user agents (health requests for always-on bot, unknown bots, etc.) in
ApplicationInsights.config. - Exclude the health check and other unnecessary URLs by creating a custom telemetry processor.
- Exclude the dependency domains which are not interesting.
How to exclude a user agent to collect the data?
Follow the following instructions,
- Open the
ApplicationInsights.configfile of your web application. - Go to
TelemetryInitializers>SyntheticUserAgentTelemetryInitializertag, then add your user agents to exclude.

How to exclude a dependency domain to track?
- Open the
ApplicationInsights.configfile of your web application. - Go to
TelemetryModules>DependencyTrackingTelemetryModuletag, then add your domains to exclude.

How to exclude the health check URLs?
Sometimes we need a custom business rule to exclude unnecessary requests. For this we create a custom telemetry processor. The following is a minimum sample, replace the /health path with your own health check URL and register the processor in the TelemetryProcessors section of ApplicationInsights.config. Refer the filtering and sampling documentation for more information.
public class HealthCheckTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public HealthCheckTelemetryProcessor(ITelemetryProcessor next)
{
Next = next;
}
public void Process(ITelemetry item)
{
var request = item as RequestTelemetry;
if (request != null && request.Url != null && request.Url.AbsolutePath.StartsWith("/health"))
{
return;
}
Next.Process(item);
}
}
How to make the dependencies and the trace complete?
Application Insights gives an id for every request and sub request (dependency request). Each sub request is automatically associated with the main request through the parent request id. In log analytics, we can find these ids as operation_id and operation_parentId respectively. It means we can follow one request end to end, from the browser to the database.

To make this work, we need to take care of the following,
- Must enable the CORS settings for
x-ms-request-idandx-ms-root-idheaders (correlation headers) for all dependencies, otherwise the browser calls can’t be correlated with the server side. - Use a custom dependency tracker to give a readable name to the unidentified dependencies. Application Insights recognizes only few dependencies like HTTP, SQL, Azure Storage, Event Hub and WCF, others will have a generic name. Refer the TrackDependency documentation.
Also, how application insights tracks the dependencies automatically is explained in the dependency tracking documentation, and the end-to-end view is available in transaction diagnostics.
Wrap up
In this article, we have looked at the gaps of default telemetry, how to enrich it with user id, session id, build version and role name, how to exclude the noise and how the correlation works across components. I appreciate your time and efforts to read this article. In the next article, I will walk you through how to control the cost with sampling, data retention and how to collect the MI logs.
Related Posts
How to build useful dashboards with Application Insights? Tools, queries and charts - Part 4
The tools, Kusto queries and dashboard charts which make Application Insights data useful, from web transactions to the application map.
How to control the cost of Application Insights? Sampling, retention and MI logs - Part 3
How to control the Application Insights cost with sampling, data retention, MI logs and custom events, with a flowchart to choose the sampling type.
How to organize Application Insights across product families? The instance strategy - Part 1
Why one Application Insights instance per product family works best, how to handle DR and pre-production, and how to enable it with the SDK.