Available Metrics

etishor edited this page Oct 18, 2014 · 4 revisions

The Metrics.NET library provides five types of metrics that can be recorded:

  • Meters record the rate at which an event occurs
  • Histograms measure the distribution of values in a stream of data
  • Timers keep a histogram of the duration of a type of event and a meter of the rate of its occurrence
  • Counters 64 bit integers that can be incremented or decremented
  • Gauges instantaneous values

Metrics can be created and registered using the methods available on the Metric static class, which is available in the Metrics namespace. Metrics can be grouped into contexts and can have tags applied to them. See Grouping & Organizing Metrics for details.

using Metrics;
...
    // metrics registered in the global context
    Timer timer = Metric.Timer("Requests",Unit.Requests);
    Counter counter = Metric.Counter("Active Requests", Unit.Requests);
    Meter meter = Metric.Meter("Errors", Unit.Errors);
    Histogram histogram = Metric.Histogram("Results", Unit.Items);

    // timer registered in the context named "Biling"
    Timer timerInContext = Metric.Context("Biling").Timer("Biling Operations", Unit.Commands);
...

The static Metric class is just a wrapper for a global metrics context.

Metrics of the same type and in the same context must have unique names. Metrics of different types or in different contexts may have same name.

When calling the method to register a metric, if the metric already exists in the context with the same name, the existing metric is returned. The following sample only creates one timer instance, in the global context, named "Requests":

using(Metric.Timer("Requests",Unit.Requests).NewContext())
{ // do some processing }
using(Metric.Timer("Requests",Unit.Requests).NewContext())
{ // do some processing }
using(Metric.Timer("Requests",Unit.Requests).NewContext())
{ // do some processing }

In most cases it is recommended, unless otherwise needed, to create private static readonly instances for the metrics you use:

public class SampleOperation
{
    private static readonly Timer operationsTimer = 
        Metric.Timer("Operations",Unit.Requests);

    private static readonly Meter operationErrors = 
        Metric.Errors("Operation Errors",Unit.Errors);

    // rest of implementation
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.