Skip to content

Commit

Permalink
3316: Filter out 400 and 404 response codes from failed requests tele…
Browse files Browse the repository at this point in the history
…metry (#3399)
  • Loading branch information
chenriksson authored Dec 6, 2016
1 parent acde497 commit 4a325ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/NuGetGallery/App_Start/OwinStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using NuGetGallery.Authentication.Providers;
using NuGetGallery.Authentication.Providers.Cookie;
using NuGetGallery.Configuration;
using NuGetGallery.Helpers;
using NuGetGallery.Infrastructure;
using Owin;

Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/NuGetGallery/Helpers/TelemetryResponseCodeFilter.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@
<Compile Include="Controllers\ErrorsController.cs" />
<Compile Include="Controllers\PagesController.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\TelemetryResponseCodeFilter.cs" />
<Compile Include="Infrastructure\Authentication\CredentialBuilder.cs" />
<Compile Include="Infrastructure\Authentication\CredentialValidator.cs" />
<Compile Include="Infrastructure\Authentication\ICredentialBuilder.cs" />
Expand Down

0 comments on commit 4a325ed

Please sign in to comment.