Filtering Urls in Azure Application Insights

I've been a fan of Application Insights for quite some time now, it's helped debug and resolve problems that we didn't even know we had! In the new tiers in Azure, the free tier gets 5 million data points a month. That sounds like a lot, until you get a message 10 days into the month warning that you've already used 80% of the limit.

Our problem was that it tracks a whole heap of requests to things we don't care about. For example, requests for images, some AJAX requests for social media sharing links and our custom health probe (used by Azure load balancing to decide whether to route requests) were taking up a big portion of that data allowance.

I opened up Bing and Googled whether I could exclude some urls - that led me to this Stack Overflow question: http://stackoverflow.com/questions/31149720/is-it-possible-to-exclude-a-url-from-application-insights

It looked sensible enough. I first upgraded all of my Application Insights NuGet packages to the latest versions (1.1 at the time of writing) through the NuGet package manager.

Then I created a "RequestsFilteringChannel" which would handle not logging requests to certain urls:

namespace RobH.SomeWebApp.ApplicationInsights
{
    public sealed class RequestsFilteringChannel : ITelemetryChannel
    {
        private readonly TelemetryChannel channel;

        public RequestsFilteringChannel()
        {
            channel = new TelemetryChannel();
        }

        public List<string> IgnoredPaths { get; set; }

        public bool? DeveloperMode
        {
            get
            {
                return channel.DeveloperMode;
            }
            set
            {
                channel.DeveloperMode = value;
            }
        }

        public string EndpointAddress
        {
            get
            {
                return channel.EndpointAddress;
            }
            set
            {
                channel.EndpointAddress = value;
            }
        }

        public void Flush()
        {
            channel.Flush();
        }

        public void Send(ITelemetry item)
        {
            var request = item as RequestTelemetry;
            if (request == null || (request != null && !ShouldFilterRequest(request)))
            {
                channel.Send(item);
            }
        }

        public void Dispose()
        {
            channel.Dispose();
        }

        private bool ShouldFilterRequest(RequestTelemetry request)
        {
            return IgnoredPaths.Any(p => request.Url.AbsolutePath.StartsWith(p, StringComparison.OrdinalIgnoreCase));
        }
    }
}

All I needed to do then, was swap the declaration in the application insights configuration:

<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <!-- Snipped -->
  <TelemetryChannel Type="RobH.SomeWebApp.ApplicationInsights.RequestsFilteringChannel, RobH.SomeWebApp">
    <IgnoredPaths>
      <Add>/content</Add>
      <Add>/healthprobe</Add>
      <Add>/sharebuttons</Add>
    </IgnoredPaths>
  </TelemetryChannel>
  <!-- Snipped -->
</ApplicationInsights>

Notice that the type declaration is "TypeFullName, AssemblyName".

Comments

Popular posts from this blog

Trimming strings in action parameters in ASP.Net Web API

Full text search in Entity Framework 6 using command interception

Composing Expressions in C#