App Metrics 1.0 Fork me on GitHub
Show / Hide Table of Contents

Gauges

A Gauge is simply an action that returns the instantaneous measure of a value, where the value abitrarily increases and decreases, for example CPU usage.

Gauges are ideal to use for measured values such as current memory usage, cpu usage, temperatures, disk space etc.

Using Gauges

var processPhysicalMemoryGauge = new GaugeOptions
{
    Name = "Process Physical Memory",
    MeasurementUnit = Unit.Bytes
}

var process = Process.GetCurrentProcess();

_metrics.Measure.Gauge.SetValue(MetricsRegistry.Gauges.TestGauge, () => process.WorkingSet64);

Which for example when using the JSON formatter would result in something similar to:

{
    "context": "Process",
    "gauges": [
        {
            "value": 51683328,
            "name": "Process Physical Memory",           
            "unit": "bytes"
        }
    ]
}

Derived Gauges

Derived Gauges allow you to derive a value from another Gauge and using a transformation, calculate the measurement.

var process = Process.GetCurrentProcess();

var processPhysicalMemoryGauge = new GaugeOptions
{
    Name = "Process Physical Memory (MB)",
    MeasurementUnit = Unit.MegaBytes
};

var physicalMemoryGauge = new FunctionGauge(() => process.WorkingSet64);

_metrics.Measure.Gauge.SetValue(MetricsRegistry.Gauges.DerivedGauge,
        () => new DerivedGauge(physicalMemoryGauge, g => g / 1024.0 / 1024.0));

Ratio Gauges

Ratio Gauges allow you to measure a Gauge with a measurement which is the ratio between two values.

var cacheHits = _metrics.Provider.Meter.Instance(MetricsRegistry.Meters.CacheHits);
var calls = _metrics.Provider.Timer.Instance(MetricsRegistry.Timers.DatabaseQueryTimer);

var cacheHit = Rnd.Next(0, 2) == 0;

using (calls.NewContext())
{
    if (cacheHit)
    {
        cacheHits.Mark();
    }

    Thread.Sleep(cacheHit ? 10 : 100);
}

_metrics.Measure.Gauge.SetValue(MetricsRegistry.Gauges.CacheHitRatioGauge, () => new HitRatioGauge(cacheHits, calls, m => m.OneMinuteRate));
  • Edit this Doc
Back to top Copyright © 2017 Allan Hardy
Generated by DocFX