From 4a325ede0884924605a2467407a98cf1681925ce Mon Sep 17 00:00:00 2001 From: Christy Henriksson Date: Tue, 6 Dec 2016 11:30:08 -0800 Subject: [PATCH] 3316: Filter out 400 and 404 response codes from failed requests telemetry (#3399) --- src/NuGetGallery/App_Start/OwinStartup.cs | 8 +++-- .../Helpers/TelemetryResponseCodeFilter.cs | 32 +++++++++++++++++++ src/NuGetGallery/NuGetGallery.csproj | 1 + 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/NuGetGallery/Helpers/TelemetryResponseCodeFilter.cs diff --git a/src/NuGetGallery/App_Start/OwinStartup.cs b/src/NuGetGallery/App_Start/OwinStartup.cs index 43fd77e3b2..5f83dc83e6 100644 --- a/src/NuGetGallery/App_Start/OwinStartup.cs +++ b/src/NuGetGallery/App_Start/OwinStartup.cs @@ -20,6 +20,7 @@ using NuGetGallery.Authentication.Providers; using NuGetGallery.Authentication.Providers.Cookie; using NuGetGallery.Configuration; +using NuGetGallery.Helpers; using NuGetGallery.Infrastructure; using Owin; @@ -58,16 +59,19 @@ public static void Configuration(IAppBuilder app) { TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey; + var telemetryProcessorChainBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; + telemetryProcessorChainBuilder.Use(next => new TelemetryResponseCodeFilter(next)); + // Note: sampling rate must be a factor 100/N where N is a whole number // e.g.: 50 (= 100/2), 33.33 (= 100/3), 25 (= 100/4), ... // https://azure.microsoft.com/en-us/documentation/articles/app-insights-sampling/ var instrumentationSamplingPercentage = config.Current.AppInsightsSamplingPercentage; if (instrumentationSamplingPercentage > 0 && instrumentationSamplingPercentage < 100) { - var telemetryProcessorChainBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; telemetryProcessorChainBuilder.UseSampling(instrumentationSamplingPercentage); - telemetryProcessorChainBuilder.Build(); } + + telemetryProcessorChainBuilder.Build(); } // Configure logging diff --git a/src/NuGetGallery/Helpers/TelemetryResponseCodeFilter.cs b/src/NuGetGallery/Helpers/TelemetryResponseCodeFilter.cs new file mode 100644 index 0000000000..0c693c8734 --- /dev/null +++ b/src/NuGetGallery/Helpers/TelemetryResponseCodeFilter.cs @@ -0,0 +1,32 @@ +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.DataContracts; + +namespace NuGetGallery.Helpers +{ + public class TelemetryResponseCodeFilter : ITelemetryProcessor + { + public TelemetryResponseCodeFilter(ITelemetryProcessor next) + { + Next = next; + } + + private ITelemetryProcessor Next { get; set; } + + public void Process(ITelemetry item) + { + var request = item as RequestTelemetry; + int responseCode; + + if (request != null && int.TryParse(request.ResponseCode, out responseCode)) + { + if (responseCode == 400 || responseCode == 404) + { + request.Success = true; + } + } + + this.Next.Process(item); + } + } +} \ No newline at end of file diff --git a/src/NuGetGallery/NuGetGallery.csproj b/src/NuGetGallery/NuGetGallery.csproj index d086f1d13c..d2506fc1da 100644 --- a/src/NuGetGallery/NuGetGallery.csproj +++ b/src/NuGetGallery/NuGetGallery.csproj @@ -747,6 +747,7 @@ +