Skip to content

OWIN Metrics Adapter

aramirez-asuresoftware edited this page Sep 14, 2017 · 10 revisions

OWIN Metrics Adapter

The OWIN adapter exposes the metrics provided by the Metrics.NET as a set of OWIN Middlewares. Routes are configured where metrics and health checks can be accessed in JSON and human readable format. A visualization app is also provided using the jquery flot library.

Installation

The OWIN Metrics Adapter is available on NuGet and can be installed with the following command:

Install-Package Owin.Metrics -Pre

Adapter Usage

The Owin.Metrics adapter providers the following Global configurable metrics:

  • Global Request Timer (timer updated for all requests)
  • Global POST & PUT Request Size histogram
  • Global Error Meter (measures count and rate at which requests return errors)
  • Global Active Requests Counter (number of current concurrent requests)

The values of the current metrics can be accessed (if enabled) at the following endpoints:

  • /metrics/ - as visualization app that uses the json endpoint for updates
  • /text - as human readable text
  • /json - as JSON - defaults to version one. For later versions use /v{version-number}/json
  • /health - for health checks in JSON format

To enable the Global metrics, in the Configuration method of your OWIN StartUp class you need to add:

public void Configuration(IAppBuilder app)
{
    var httpconfig = new HttpConfiguration();
    httpconfig.MapHttpAttributeRoutes();

    // Sets the route template for the current request in the OWIN context
    httpconfig.MessageHandlers.Add(new SetOwinRouteTemplateMessageHandler());

    Metric.Config
        .WithAllCounters()
        .WithReporting(r => r.WithConsoleReport(TimeSpan.FromSeconds(30)))
        .WithOwin(middleware => app.Use(middleware), config => config
            .WithRequestMetricsConfig(c => c.WithAllOwinMetrics())
            .WithMetricsEndpoint()
        );

    app.UseWebApi(httpconfig);
}

Endpoints can be changed and enabled/disabled through the optional configuration class on the OWIN app builder extension UseMetrics.

Metric.Config
    .WithOwin(middleware => app.Use(middleware), config => config
        .WithMetricsEndpoint(endpointConfig =>
        {
            endpointConfig
                .MetricsHealthEndpoint(endpoint: "new-healthcheck-endpoint")
                .MetricsPingEndpoint(endpoint: "new-ping-endpoint")
                .MetricsJsonEndpoint(endpoint: "new-json-endpoint")
                .MetricsTextEndpoint(enabled: false)
                .MetricsEndpoint(enabled: false);
        })
);

Global metrics can be configured through the owin metrics configuration class, this provides the ability to customise global metrics names and configure only what is need.

Metric.Config
    .WithOwin(middleware => app.Use(middleware), config => config
        .WithRequestMetricsConfig(c => 
            c.WithErrorsMeter("new-error-meter-name")
                .WithRequestTimer("new_request-timer-name")
        )
    );

Additional custom metrics can by registered using the Metric static class in the Metrics.NET library.

The OWIN middleware is able to ignore specified endpoints from metrics calculation. The endpoints can be set via the metrics config as an array of regex patterns.

Metric.Config               
    .WithOwin(middleware => app.Use(middleware), config => config
        .WithRequestMetricsConfig(c => c.WithAllOwinMetrics(), new[]
        {
            new Regex("(?i)^sampleignore"),
            new Regex("(?i)^metrics"),
            new Regex("(?i)^health"), 
            new Regex("(?i)^json")
            })
        .WithMetricsEndpoint()
    );