|
| 1 | +# Gauge API Overview |
| 2 | +A `Gauge` is used to record already aggregated metrics, or metrics that can go up and down. Typical examples of gauges would be the number of jobs/entries in a queue or number of threads in a running state. |
| 3 | + |
| 4 | +The `Gauge` values can be negative. This document describes the key types and the overall bahavior of API. |
| 5 | + |
| 6 | +## Gauge API |
| 7 | + |
| 8 | +The value that is published for gauges is an instantaneous measurement of an `int64` or `double` value. This API is useful when you want to manually increase and decrease values as per service requirements. |
| 9 | + |
| 10 | +The following general operations MUST be provided by the API: |
| 11 | +* Defining a `name`, `description`, `unit`, `labelKeys` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back the gauge object to getOrcreate time series, remove time series and clear all time series. |
| 12 | + * `name`: a string describing the name of the metric, e.g. "vm_cpu_cycles" or "queue_size". Names MUST be unique within the library. It is recommended to use names compatible with the intended end usage. |
| 13 | + * `description`: a string describing the metric, e.g."Virtual cycles executed on VM". |
| 14 | + * `unit`: a string describing the unit used for the metric. Follows the format described by |
| 15 | +[Unified Code for Units of Measure](http://unitsofmeasure.org/ucum.html). |
| 16 | + * `labelKeys`: the list of the label keys to track different types of metric. |
| 17 | + * `constantLabels`: the map of label keys and label values. |
| 18 | +* Add a new time series with label values, which returns a `Point` (which is part of the `TimeSeries`). Each point represents an instantaneous measurement of a varying gauge value. Each Gauge Metric has one or more time series for a single metric. |
| 19 | + * `labelValues`: the list of label values. The number of label values must be the same to that of the label keys. |
| 20 | +* The `Point` class should provide functionalities to manually increment/decrement values. Example: `add(long amt)`, `set(long value)`. |
| 21 | +* Remove a single time series from the gauge metric, if it is present. |
| 22 | + * `labelValues`: the list of label values. |
| 23 | +* Clear all time series from the gauge metric i.e. References to all previous point objects are invalid (not part of the metric). |
| 24 | +* Get a default time series for a gauge with all labels not set, or default labels. |
| 25 | + |
| 26 | +Example in Java: |
| 27 | +```java |
| 28 | +private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); |
| 29 | + |
| 30 | +List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "description")); |
| 31 | +List<LabelValue> labelValues = Arrays.asList(LabelValue.create("queue1")); |
| 32 | + |
| 33 | +LongGauge gauge = metricRegistry.addLongGauge("queue_size", "The number of jobs", "1", labelKeys); |
| 34 | +LongPoint point = gauge.getOrCreateTimeSeries(labelValues); |
| 35 | + |
| 36 | +void doSomeWork() { |
| 37 | + point.set(15); |
| 38 | +} |
| 39 | +``` |
| 40 | +It is recommended to keep a reference of a point for manual operations instead of always calling `getOrCreateTimeSeries` method. |
| 41 | + |
| 42 | +## Derived Gauge API |
| 43 | + |
| 44 | +The value that is published for gauges is an instantaneous measurement of an `int64` or `double` value. This gauge is self sufficient once created, so users should never need to interact with it. The value of the gauge is observed from the `object` and a `callback function`. The callback function is invoked whenever metrics are collected, meaning the reported value is up-to-date. The implementation should keep a `WeakReference` to the object and it is the user's responsibility to manage the lifetime of the object. |
| 45 | + |
| 46 | +The following general operations MUST be provided by the API: |
| 47 | +* Defining a `name`, `description`, `unit`, `labelKeys` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back gauge object to add new time series, remove time series and clear all time series. |
| 48 | + * `name`: a string describing the name of the metric, e.g. "vm_cpu_cycles". Names MUST be unique within the library. It is recommended to use names compatible with the intended end usage. |
| 49 | + * `description`: a string describing the metric, e.g."Virtual cycles executed on VM". |
| 50 | + * `unit`: a string describing the unit used for the metric. Follows the format described by |
| 51 | +[Unified Code for Units of Measure](http://unitsofmeasure.org/ucum.html). |
| 52 | + * `labelKeys`: the list of the label keys to track different types of metric. |
| 53 | + * `constantLabels`: the map of label keys and label values. |
| 54 | +* Add a new time series with label values, an `object` and a `callback function`. The number of label values must be the same to that of the label keys. |
| 55 | + * `labelValues`: the list of label values. The number of label values must be the same to that of the label keys. |
| 56 | + * `obj`: the state object from which the function derives a measurement. |
| 57 | + * `function`: the callback function to be called. |
| 58 | +* Remove a single time series from the gauge metric, if it is present. |
| 59 | + * `labelValues`: the list of label values. |
| 60 | +* Clear all time series from the gauge metric i.e. References to all previous point objects are invalid (not part of the metric). |
| 61 | + |
| 62 | +Example in Java: |
| 63 | +```java |
| 64 | +private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); |
| 65 | + |
| 66 | +List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "description")); |
| 67 | +List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1")); |
| 68 | + |
| 69 | +DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge( |
| 70 | + "vm_cpu_cycles", "Virtual cycles executed on VM", "1", labelKeys); |
| 71 | +CpuInfo cpuInfo = new CpuInfo(); |
| 72 | + |
| 73 | +gauge.createTimeSeries(labelValues, cpuInfo, |
| 74 | + new ToDoubleFunction<CpuInfo>() { |
| 75 | + {@literal @}Override |
| 76 | + public double applyAsDouble(CpuInfo cpuInfo) { |
| 77 | + return cpuInfo.cycles(); |
| 78 | + } |
| 79 | + }); |
| 80 | +``` |
0 commit comments