Recording Application Metrics
App Metrics provides access to an IMetrics
instance, registered as a single instance. This can be injected where required to start recording different types of metrics.
Each metric being measured should be described through one of the below classes, which provides details about the metric being measured.
Options | Description |
---|---|
GaugeOptions |
Allows the metric context, metric name, unit of measurement, and tags to be specified. |
CounterOptions |
Allows the metric context, metric name, unit of measurement, and tags to be specified. |
MeterOptions |
Allows the metric context, metric name, unit of measurement, rate unit and tags to be specified. |
TimerOptions |
Allows the metric context, metric name, unit of measurement, rate unit, duration unit, sampling type and tags to be specified. Also allows a custom IReservoir to be used for sampling. |
HistogramOptions |
Allows the metric context, metric name, unit of measurement, sampling type and tags to be specified. Also allows a custom IReservoir to be used for sampling. |
Name
is the only required property is the property on each metric option instance.
Declaring Metrics
public static class AppMetricsRegistery
{
public static GaugeOptions Errors { get; } = new GaugeOptions
{
Name = "Errors"
};
public static CounterOptions SampleCounter { get; } = new CounterOptions
{
Name = "Sample Counter",
MeasurementUnit = Unit.Calls,
};
public static HistogramOptions SampleHistogram { get; } = new HistogramOptions
{
Name = "Sample Histogram",
Reservoir = new Lazy<IReservoir>(() => new DefaultAlgorithmRReservoir()),
MeasurementUnit = Unit.MegaBytes
};
public static MeterOptions SampleMeter { get; } = new MeterOptions
{
Name = "Sample Meter",
MeasurementUnit = Unit.Calls
};
public static TimerOptions SampleTimer { get; } = new TimerOptions
{
Name = "Sample Timer",
MeasurementUnit = Unit.Items,
DurationUnit = TimeUnit.Milliseconds,
RateUnit = TimeUnit.Milliseconds,
Reservoir = new Lazy<IReservoir>(() => new DefaultForwardDecayingReservoir(sampleSize:1028, alpha:0.015))
};
}
Recording Metrics using an IMetrics
instance
_metrics.Measure.Counter.Increment(AppMetricsRegistery.SampleCounter);
_metrics.Measure.Counter.Decrement(AppMetricsRegistery.SampleCounter);
_metrics.Measure.Gauge.SetValue(AppMetricsRegistery.Errors, () => 1);
_metrics.Provider.Histogram.Update(AppMetricsRegistery.SampleHistogram, 1);
_metrics.Provider.Meter.Mark(AppMetricsRegistery.SampleMeter, 1);
using(_metrics.Measure.Timer.Time(AppMetricsRegistery.SampleTimer))
{
// Do something
}