Skip to content

Nancy Metrics Adapter

Hintea Dan Alexandru edited this page Mar 30, 2016 · 2 revisions

##About The NancyFx adapter adds the metrics provided by the Metrics.NET to the awesome NancyFx web framework. The adapter registers metrics related to Nancy's functionality, but also configures a route where the metrics can be accessed in JSON and human readable format. A visualization app is also provided using the jquery flot library.

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

Install-Package Nancy.Metrics

##Adapter Usage The Nancy.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 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
  • /metrics/text - as human readable text
  • /metrics/json - as JSON

To enable the Global metrics, in the ApplicationStartup method of your Nancy bootstrapper you need to add:

    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        Metric.Config
                .WithAllCounters()
                .WithReporting(r => r.WithConsoleReport(TimeSpan.FromSeconds(30)))
                .WithNancy(pipelines);
    }

The adapter also provides extension methods on INancyModule that enable the registration of metrics for:

  • Request Timer for a particular route
  • Response Size monitor for a particular route
  • Request Size monitor for a particular route

To enable these metrics call the desired extension methods from the module:

    public class SampleModule : NancyModule
    {
        public SampleModule()
            : base("/")
        {
            this.MetricForRequestTimeAndResponseSize("TestRequest", "Get", "/test");
            this.MetricForRequestSize("TestRequestSize", "Post", "/action");

            Get["/test"] = _ => Response.AsText("test");

        }
    }

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