Application Insights is an extensible Application Performance Management (APM) service for web developers. It provides rich performance monitoring, powerful alerting, and easy-to-consume dashboards to help ensure your applications are available and performing as you expect.

One of the powerful characteristics of Application Insights is that it provides an extensible API where you can customize the behavior of the standard telemetry modules the way you want. This can be done through ITelemetryInitializer, an interface that allows you to implement custom behavior that plugs into Application Insights to initialize the telemetry data before it is sent to the service. If you provide a telemetry initializer, it is called whenever any of the Track*() methods is called.

Usage Examples

Custom Failed Requests

Application Insights for Web collects telemetry about HTTP requests and flags as failed any request with a response code >= 400. If you want the requests with NotFound (404) response to be treated as a success, you can provide a telemetry initializer that sets the Success property whenever the response is 404.

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MyApp.Api.Infrastructure.Telemetry
{
    public class TelemetryNotFoundInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            if (requestTelemetry == null) return;

            int code;
            var parsed = int.TryParse(requestTelemetry.ResponseCode, out code);
            if (!parsed) return;

            if (code == 404)
            {
                requestTelemetry.Success = true;
                
                // Allow us to filter these requests in the portal:
                requestTelemetry.Context.Properties["Overridden400s"] = "true";
            }
        }
    }
}

To enable a custom telemetry initializer you need to register it with Application Insights at the startup of your application (Startup.cs or Global.asax)

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // other initialization code
        // ....

        // Telemetry Configuration
        TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryNotFoundInitializer());
    }
}

Track Custom Headers

Application Insights does not track all the HTTP request and response headers included in the request data. This may be a problem if you have HTTP requests from different mobile devices and you want to track the device’s type (Android, iOS), or the version that the device is running. To track this data again you can create a custom class that implements ITelemetryInitializer and define the headers you want to track.

using System.Collections.Generic;
using System.Web;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace MyApp.Api.Infrastructure.Telemetry
{
    public class TelemetryHeadersInitializer : ITelemetryInitializer
    {
        public List<string> RequestHeaders { get; set; }
        public List<string> ResponseHeaders { get; set; }

        public TelemetryHeadersInitializer()
        {
            RequestHeaders = new List<string>();
            ResponseHeaders = new List<string>();
        }

        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            // Is this a TrackRequest() ?
            if (requestTelemetry == null) return;

            var context = HttpContext.Current;
            if (context == null) return;

            foreach (var headerName in RequestHeaders)
            {
                var header = context.Request.Headers[headerName];
                if (header != null)
                {
                    telemetry.Context.Properties.Add($"Request-{headerName}", header);
                }
            }
            foreach (var headerName in ResponseHeaders)
            {
                var header = context.Response.Headers[headerName];
                if (header != null)
                {
                    telemetry.Context.Properties.Add($"Response-{headerName}", header);
                }
            }
        }
    }
}

You need then to register the `TelemetryHeadersInitializer` in your startup class (Startup.cs, Global.asax) and define which headers you want to track:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // other initialization code
        // ....

        // Telemetry Configuration
        TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryHeadersInitializer()
        {
            RequestHeaders = { "DeviceName", "DeviceVersion" },
            ResponseHeaders = { "Content-Type" }
        });
    }
}

Subsequently, if you click on a request in the Azure portal you will see that the custom headers are being tracked and presented as Custom Data:

 

 

 

Categorized in:

Tagged in: