Filtering App Insights Server-side Dependency messages

So, previously I’ve written about filtering out all the successful Dependency messages going to App Insights. What about unsuccessful ones, though?

My Sitecore instance seems have a failing dependency that is clogging up my logs. It’s the same as mentioned in this StackExchange question. It doesn’t seem to cause any issue, though… and it isn’t every environment either. Anyway, I’d like to block it. Telemetry processors to the rescue…

public class DependencyFilter : ITelemetryProcessor
{
	private readonly ITelemetryProcessor _nextProcessor;
	
	public string NameRegex { get; set; }
	public string DependencyType { get; set; }
	public string ResultCode { get; set; }

	public string FilterIfAllMatch { get; set; }


	private Regex _rgx = null;
	private bool _rgxInited = false; 


	public DependencyFilter (ITelemetryProcessor nextProcessor)
	{
		FilterIfAllMatch = "true";  //Default to all must match
		_nextProcessor = nextProcessor;
	}

	public void Process(ITelemetry telemetry)
	{
		DependencyTelemetry dependencyTelemetry = telemetry as DependencyTelemetry;
		if (dependencyTelemetry != null)
		{
			int rule = 0;
			int pass = 0;

			if(!string.IsNullOrEmpty(NameRegex))
			{
				if (_rgx == null && !_rgxInited)
				{
					_rgxInited = true;
					try
					{
						_rgx = new Regex(NameRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
					}
					catch (Exception ex)
					{
						Log.Warn(string.Format("Application Insights: {0} - {1}", ex.GetType().ToString(), ex.Message), this);
						_rgx = null;
					}
				}

				if (_rgx != null)
				{
					rule++;
					if (!_rgx.IsMatch(dependencyTelemetry.Name))
					{
						pass++;
					}
				}
			}

			if (!string.IsNullOrEmpty(DependencyType))
			{
				rule++;
				if (!dependencyTelemetry.Type.Equals(DependencyType, System.StringComparison.OrdinalIgnoreCase))
				{
					pass++;
				}
			}

			if (!string.IsNullOrEmpty(ResultCode))
			{
				rule++;
				if (!dependencyTelemetry.ResultCode.Equals(ResultCode, 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;
			}
		}

		_nextProcessor.Process(telemetry);
	}

	public override string ToString()
	{
		return string.Format("DependencyFilter: {0} , {1}, {2}, {3}", NameRegex, DependencyType, ResultCode, FilterIfAllMatch);
	}
}

This filter allows you to match dependency message names with by regex, type, or result code, and exclude them if any or all of those rules match.

Note that there are public properties used for configuration of the telemetry processor; these match elements in the ApplicationInsights.config file.

Note also that the Regex object is instantiated the first time the processor runs. This is because when the constructor is run the public properties – including the value for the Regex pattern – haven’t been set yet.

To use this telemetry processor, add it to the TelemetryProcessors section of ApplicationInsights.config:

Obviously, do use this filter carefully; a failing dependency like this might be trying to tell you something that you need to know about…

Filtering App Insights Server-side Dependency messages

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.