Filtering App Insights server-side Health Check requests

So, again, I’m trying to tame Application Insights. My logs are filling up with various requests for different health-check URLs. These get requested, over and over, day after day, and all are recorded in App Insights as Requests. However, I don’t care about these requests if they’re successful. In fact, I only care about if they fail. Can I exclude them?

Yes, I can. I’ll build a telemetry processor to filter them out.

public class RequestFilter : ITelemetryProcessor
{
	public string UrlContains { get; set; }
	public string ResponseCode { get; set; }
	public string FilterIfAllMatch { get; set; }
	
	private ITelemetryProcessor Next { get; set; }

	// next will point to the next TelemetryProcessor in the chain.
	public RequestFilter(ITelemetryProcessor next)
	{
		FilterIfAllMatch = "true";  //Default to all must match
		this.Next = next;
	}

	public void Process(ITelemetry item)
	{
		RequestTelemetry requestTelemetry = item as RequestTelemetry;
		if (requestTelemetry != null )
		{
			int rule = 0;
			int pass = 0;

			if (!string.IsNullOrEmpty(UrlContains))
			{
				rule++;
				if (!requestTelemetry.Url.ToString().Contains(UrlContains))
				{
					pass++;
				}
			}

			if (!string.IsNullOrEmpty(ResponseCode))
			{
				rule++;
				if (!requestTelemetry.ResponseCode.Equals(ResponseCode, System.StringComparison.OrdinalIgnoreCase))
				{
					pass++;
				}
			}

			if (FilterIfAllMatch == null || FilterIfAllMatch.Equals("true", System.StringComparison.OrdinalIgnoreCase))
			{
				// If all rules match, then there are no passes.
				if (pass == 0 && rule > 0) return;
			}
			else
			{
				// Else if any rules match, then there are no passes
				if (pass < rule) return;
			}

		}

		this.Next.Process(item);
	}

	public override string ToString()
	{
		return string.Format("RequestFilter: {0} , {1}, {2}", UrlContains, ResponseCode, FilterIfAllMatch);
	}
}

This filter checks a number of rules, and can filter messages that match one or more, or that match ALL the rules. The rules check status code, or does the URL contain a particular string. Note the public properties in the class – this allows configuration to be set in the ApplicationInsights.config file:

Note that this configuration will only filter if a particular health check URL gives a 200 response. If it gave a different status code, the telemetry message would still be recorded.

Note also that you can add multiple of these TelemetryProcessors, so you could exclude other requests and response codes in addition.

Filtering App Insights server-side Health Check requests

Leave a Reply

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.