Meters
A Meter measures the rate at which an event occurs along with a total count of the occurances. Rates that are measured are the mean, one minute, five minute and fifteen minute. The mean rate is an average rate of events for the lifetime of the application which does not provide a sense of recency, x-minute rates use an Exponential Weighted Moving Average (EWMA) for their calculations which do provide a sense of recency.
Meters provide the ability to track a count and percentage of each item within a set along with the rate of each item, for example if we were measuring the rate of HTTP requests made to an API protected with OAuth2, we could also track the rate at which each client was making these requests along with the number of requests and their percentage of the overall count within the same meter.
Using Meters
var cacheHitsMeter = new MeterOptions
{
Name = "Cache Hits",
MeasurementUnit = Unit.Calls
};
_metrics.Measure.Meter.Mark(cacheHitsMeter);
_metrics.Measure.Meter.Mark(cacheHitsMeter, 10);
And if we wanted to track the rate at which each HTTP status was occuring with an API:
var httpStatusMeter = new MeterOptions
{
Name = "Http Status",
MeasurementUnit = Unit.Calls
};
_metrics.Measure.Meter.Mark(httpStatusMeter, "200");
_metrics.Measure.Meter.Mark(httpStatusMeter, "500");
_metrics.Measure.Meter.Mark(httpStatusMeter, "401");
Which for example when using the JSON formatter would result in something similar to:
{
"context": "Application",
"meters": [
{
"count": 1000,
"fifteenMinuteRate": 0.2,
"fiveMinuteRate": 0.2,
"items": [
{
"count": 100,
"fifteenMinuteRate": 0.02,
"fiveMinuteRate": 0.02,
"item": "500",
"meanRate": 0.011,
"oneMinuteRate": 0.02,
"percent": 10
},
{
"count": 200,
"fifteenMinuteRate": 0.04,
"fiveMinuteRate": 0.04,
"item": "401",
"meanRate": 0.023,
"oneMinuteRate": 0.04,
"percent": 20
},
{
"count": 700,
"fifteenMinuteRate": 0.14,
"fiveMinuteRate": 0.14,
"item": "200",
"meanRate": 0.080,
"oneMinuteRate": 0.14,
"percent": 70
}
],
"meanRate": 0.1155,
"oneMinuteRate": 0.2,
"rateUnit": "ms",
"name": "Http Status",
"unit": "Calls"
}
]
}
Note
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 Counter as shown below.
_metrics.Provider.Meter.Instance(httpStatusMeter).Reset();