Making Application Insights record 404s as successful

We’re using AppInsights with our Sitecore10 system, and one frustration we have is that our “Failures” report contains thousands of entries for 404 responses. Unfortunately, these days, just having a site means you’re going to get a tonne of requests for “potentially vulnerable paths” – such as the WordPress login, Drupal Login, PHP Info pages, etc.. None of these paths are ever going to be in our site, and conceptually, is it really an error if we respond to someone who is spamming our site with requests that a particular page doesn’t exist? It’s just making it harder to see the genuine issues we actually care about.

To that end, I wanted to set some of the 4xx responses from our site to be “Successful” in AppInsights. I came up with this TelemetryInitializer…

public class CustomRequestInitializer : ITelemetryInitializer
{
  public ICollection<string> SuccessfulStatusCodes { get; }

  public CustomRequestInitializer()
  {
    SuccessfulStatusCodes = new List<string>();
  }

  public void Initialize(ITelemetry telemetry)
  {
    RequestTelemetry requestTelemetry = telemetry as RequestTelemetry;
    if (requestTelemetry != null)
    {
      if( SuccessfulStatusCodes.Contains( requestTelemetry.ResponseCode ))
      {
        requestTelemetry.Success = true;
      }
    }
  }
}

Configure that in your ApplicationInsights.config file. Note that you can add other statuses:

<TelemetryInitializers>
  <Add Type="Sitecore.Foundation.AppInsights.TelemetryInitializers.CustomRequestInitializer, Sitecore.Foundation.AppInsights">
    <SuccessfulStatusCodes>
      <!-- These aren't our fault! -->
      <Add>404</Add>
    </SuccessfulStatusCodes>
  </Add>
</TelemetryInitializers>
Making Application Insights record 404s as successful

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.