Application Insights: Ignore 404 status codes for Web APIs

Application Insights is an awesome monitoring tool, but it considers all 4xx and 5xx HTTP Status Codes as errors, and when writing REST APIs some of these codes have a special meaning and are not errors. A 404 (Not found) response from a REST API usually means there are no results for a given action, not that you have hit a non-existent page.

So, how do we tell Application Insights to ignore those 404s? Simple: we write what is called a Telemetry processor.

using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace Samples
{
public class My404Filter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public My404Filter(ITelemetryProcessor next)
{
Next = next;
}
public void Process(ITelemetry item)
{
// To filter out an item, just return
if (ShouldIgnoreRequest(item))
{
return;
}
Next.Process(item);
}
private static bool ShouldIgnoreRequest(ITelemetry item)
{
if (item.Context.Operation.Name != null)
{
var operationName = item.Context.Operation.Name.ToLower();
if (operationName.StartsWith("get api/getobjectwith404s"))
{
var req = item as RequestTelemetry;
return req != null && req.ResponseCode.Equals("404", StringComparison.OrdinalIgnoreCase);
}
return false;
}
return false;
}
}
}

view raw
My404Filter.cs
hosted with ❤ by GitHub

We then need to add this processor to our Application Insights telemetry configuration, for this we’ll use a helper class:

using System.Configuration;
using Microsoft.ApplicationInsights.Extensibility;
namespace Samples.App_Start
{
public static class AppInsightsConfig
{
public static void RegisterAppInsights()
{
TelemetryConfiguration.Active.InstrumentationKey =
System.Web.Configuration.WebConfigurationManager.AppSettings["ApplicationInsightsKey"];
var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
builder.Use(next => new My404Filter(next));
builder.Build();
}
}
}

view raw
AppInsightsConfig.cs
hosted with ❤ by GitHub

And we call it in our global.asax:

using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Samples.App_Start;
namespace Samples
{
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AppInsightsConfig.RegisterAppInsights();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}

view raw
global.asax.cs
hosted with ❤ by GitHub

Now we will not see those 404s as errors in Application Insights.

@gjbellmann

2 thoughts on “Application Insights: Ignore 404 status codes for Web APIs

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s