Skip to content

Instantly share code, notes, and snippets.

@naile
Created August 24, 2017 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naile/f796041ef3baafaf7f289d37d1c42cb3 to your computer and use it in GitHub Desktop.
Save naile/f796041ef3baafaf7f289d37d1c42cb3 to your computer and use it in GitHub Desktop.
Application Insights Telemetry ignore statuscodes for specific operations
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using System.Linq;
namespace AppInsightsFilter
{
public class ApplicationInsightsFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
private OperationNameResponseCodeFilter[] OperationResponseCodeFilter { get; set; }
public ApplicationInsightsFilter(ITelemetryProcessor next, params OperationNameResponseCodeFilter[] operationResponseCodeFilter)
{
Next = next;
OperationResponseCodeFilter = operationResponseCodeFilter;
}
public void Process(ITelemetry item)
{
// To filter out an item, just return
if (ShouldIgnoreRequest(item))
{
return;
}
Next.Process(item);
}
private bool ShouldIgnoreRequest(ITelemetry item)
{
if (item.Context.Operation.Name == null) return false;
var req = item as RequestTelemetry;
if (req == null) return false;
var operationName = item.Context.Operation.Name.ToLower();
return OperationResponseCodeFilter
.Any(f => operationName.StartsWith(f.OperationName) && f.ResponseCode == req.ResponseCode);
}
}
public struct OperationNameResponseCodeFilter
{
public string ResponseCode { get; set; }
public string OperationName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment