metrics: Metrics

Description Usage Arguments Details Value See Also Examples

Description

A metric is a measure which can be aggregated into a time series, and comes in one of three types: counters, gauges, and histograms.

Metrics must have a unique name.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
counter_metric(
  name,
  help,
  labels = character(),
  ...,
  unit = NULL,
  registry = global_registry()
)

gauge_metric(
  name,
  help,
  labels = character(),
  ...,
  unit = NULL,
  registry = global_registry()
)

histogram_metric(
  name,
  help,
  buckets = c(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10),
  labels = character(),
  ...,
  unit = NULL,
  registry = global_registry()
)

Arguments

name

The name of the metric.

help

A brief, one-sentence explanation of the metric's meaning.

labels

A vector of label names for the metric.

...

For backward compatibility, otherwise ignored.

unit

An optional unit for the metric, e.g. "seconds". Must match the metric name.

registry

Where to register the metric for later retrieval.

buckets

A sequence of buckets to bin observations into. Defaults to Prometheus's suggested buckets, which are a good fit for measuring user-visible latency in seconds (e.g. for web services).

Details

All metric objects have a reset() method that reverts the underlying value (or values) to zero, an unregister() method that removes them from the registry they were created in, and a render() method that writes a representation of the metric in the text-based OpenMetrics format. Normally, render_metrics() is used instead.

In addition, various metrics have their own methods:

Value

An object with methods to manipulate the metric. See details.

See Also

The OpenMetrics specification on Metric Types as well as the original Prometheus documenation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
meows <- counter_metric("meows", "Heard around the house.", labels = "cat")
meows$inc(cat = "Shamus") # Count one meow from Shamus.
meows$inc(3, cat = "Unknown") # Count three meows of unknown origin.
meows$render()

thermostat <- gauge_metric("thermostat", "Thermostat display.")
thermostat$set(21.3) # Read from the display...
thermostat$dec(2) # ... and then turn it down 2 degrees.
thermostat$render()

temperature <- histogram_metric(
  "temperature", "Ambient room temperature measurements.",
  buckets = c(10, 15, 20, 22, 25), labels = "room"
)
set.seed(9090)
# Simulate taking ambient temperature samples.
for (measure in rnorm(20, mean = 21.5)) {
  temperature$observe(measure, room = sample(c("kitchen", "bathroom"), 1))
}
temperature$render()

atheriel/openmetrics documentation built on July 8, 2021, 3:34 a.m.