Created
August 24, 2017 12:32
-
-
Save naile/f796041ef3baafaf7f289d37d1c42cb3 to your computer and use it in GitHub Desktop.
Application Insights Telemetry ignore statuscodes for specific operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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