Timers

A Timer is a combination of a Histogram and a Meter allowing us to measure the duration of a type of event, the rate of its occurrence and provide duration statistics. For example, the App.Metrics.AspNetCore.Tracking nuget package provides the ability to record a timer per endpoint.

Using Timers

Timers can be recorded by either passing an action into the Time method or by using a using statement as show below. When a using statement is used the Timer will end recording upon disposing.

var requestTimer = new TimerOptions
{
    Name = "Request Timer",
    MeasurementUnit = Unit.Requests,
    DurationUnit = TimeUnit.Milliseconds,
    RateUnit = TimeUnit.Milliseconds
};

_metrics.Measure.Timer.Time(requestTimer, () => PerformRequest());

// OR

using(_metrics.Measure.Timer.Time(requestTimer))
{
    PerformRequest();
}

Timers, like Histogram also allow us to track the min, max and last value that has been recorded in cases when a “user value” is provided when recording the timer. For example, when timing requests where the endpoint has couple of features flags implemented, we could track which feature flag as producing the min and max response times.

using(_metrics.Measure.Timer.Time(requestTimer, "feature-1"))
{
    PerformRequest();
}

When reporting metrics with counts we should keep in mind that they are a cumulative count, see notes in the Counters documentation. A Meters values can also be reset like a Counters as shown below.

_metrics.Provider.Timer.Instance(httpStatusMeter).Reset();