Skip to content

MS Application Insights integration for the Hellang.Middleware.ProblemDetails package

License

Notifications You must be signed in to change notification settings

christianacca/ApplicationInsights.ProblemDetails

Repository files navigation

ApplicationInsights.ProblemDetails Build Status

Overview

MS Application Insights integration for the Hellang.Middleware.ProblemDetails package.

Enriches request telemetry with custom dimensions extracted from a ProblemDetail response:

Screenshot of enriched telemetry captured in Application Insights

Usage

Prerequisites

Ensure your ASP.Net Core application has been configured with:

Steps

  1. Install package

    dotnet add package CcAcca.ApplicationInsights.ProblemDetails
  2. Register the library with .Net Core dependency injection system. In Statup.ConfigureServices method add:

    services.AddProblemDetailTelemetryInitializer()

Configuration / customization

  • Configure which ProblemDetail responses are sent to application insights:

    services.AddProblemDetailTelemetryInitializer(o => {
      o.ShouldSend = (ctx, problem) => problem.Status >= StatusCodes.Status500InternalServerError;
    })
  • Configure which properties of an ProblemDetail should be sent to application insights:

    services.AddProblemDetailTelemetryInitializer(o => {
     o.IncludeErrorsValue = (ctx, problem) => !SensitiveUrl(ctx);
     o.IncludeExtensionsValue = (ctx, problem) => !SensitiveUrl(ctx);
     o.IncludeRawJson = (ctx, problem) => !SensitiveUrl(ctx);
    });
  • Configure the values of the dimensions sent to application insights:

    services.AddProblemDetailTelemetryInitializer(o => {
     o.MapDimensions = (ctx, problem, dimensions) => {
       var sensitiveFields = new[] {"SSN", "SocialSecurityNumber", "AccountNumber"};
       var isSensitive = dimensions.Any(d => sensitiveFields.Any(f => d.Key.Contains(f)));
       var sanitized = isSensitive
         ? dimensions
           .Where(d =>
             !sensitiveFields.Concat(new[] {DefaultDimensionCollector.RawDimensionKey})
               .Any(f => d.Key.Contains(f)))
         : dimensions;
       return new Dictionary<string, string>(sanitized);
     };
    });
  • Configure when a ProblemDetail should be considered a success/failure

    // only status codes >= 500 treat as a failure
    services.AddProblemDetailTelemetryInitializer(options => {
       options.IsFailure = ProblemDetailsTelemetryOptions.ServerErrorIsFailure;
    });
  • Override the default serializer: see ProblemDetailsTelemetryOptions.SerializeValue

  • Override the default dimension collector for low level control: see DefaultDimensionCollector

Develop

To build and run tests you can use:

  • the dotnet cli tool
  • any IDE/editor that understands MSBuild eg Visual Studio or Visual Studio Code

Recommended workflow

  • Develop on a feature branch created from master:
    • create a branch from master.
    • perform all the code changes into the newly created branch.
    • merge master into your branch, then run tests locally (eg dotnet test src/CcAcca.ApplicationInsights.ProblemDetails.Tests)
    • on the new branch, bump the version number in CcAcca.ApplicationInsights.ProblemDetails.csproj; follow semver
    • update CHANGELOG.md
    • raise the PR (pull request) for code review & merge request to master branch.
    • PR will auto trigger a limited CI build (compile and test only)
    • approval of the PR will merge your branch code changes into the master

CI server

Github actions is used to run the dotnet cli tool to perform the build and test. See the yaml build definition for details.

Notes:

  • The CI build is configured to run on every commit to any branch
  • PR completion to master will also publish the nuget package for CcAcca.ApplicationInsights.ProblemDetails to Nuget gallery

About

MS Application Insights integration for the Hellang.Middleware.ProblemDetails package

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages