Gauge

Gauge metric, to report instantaneous values.

A Gauge is a metric that represents a single numerical value that can arbitrarily go up and down.

Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of concurrent requests.

Labelled versions can be aggregated and processed together much more easily in the Prometheus server than individual metrics for each labelset.

Imports

import io.chrisdavenport.epimetheus._
import cats.effect._

import cats.effect.unsafe.implicits.global

An Example of a Gauge with no labels:

val noLabelsGaugeExample = {
  for {
    pr <- PrometheusRegistry.build[IO]
    gauge <- Gauge.noLabels(pr, Name("gaugetotal"), "Example Gauge")
    _ <- gauge.inc
    _ <- gauge.inc
    _ <- gauge.dec
    currentMetrics <- pr.write004
  } yield currentMetrics
}
// noLabelsGaugeExample: IO[String] = FlatMap(
//   ioe = Delay(
//     thunk = io.chrisdavenport.epimetheus.PrometheusRegistry$$$Lambda$11305/0x00007f5281ea8c00@300baf3a,
//     event = cats.effect.tracing.TracingEvent$StackTrace
//   ),
//   f = <function1>,
//   event = cats.effect.tracing.TracingEvent$StackTrace
// )

noLabelsGaugeExample.unsafeRunSync()
// res0: String = """# HELP gaugetotal Example Gauge
// # TYPE gaugetotal gauge
// gaugetotal 1.0
// """

An Example of a Gauge with labels:

val labelledGaugeExample = {
  for {
    pr <- PrometheusRegistry.build[IO]
    gauge <- Gauge.labelled(
      pr,
      Name("gaugetotal"),
      "Example Gauge",
      Sized(Label("foo")),
      {s: String => Sized(s)}
    )
    _ <- gauge.label("bar").inc
    _ <- gauge.label("baz").inc
    _ <- gauge.label("bar").inc
    _ <- gauge.label("baz").inc
    _ <- gauge.label("bar").dec
    currentMetrics <- pr.write004
  } yield currentMetrics
}
// labelledGaugeExample: IO[String] = FlatMap(
//   ioe = Delay(
//     thunk = io.chrisdavenport.epimetheus.PrometheusRegistry$$$Lambda$11305/0x00007f5281ea8c00@439dc3c1,
//     event = cats.effect.tracing.TracingEvent$StackTrace
//   ),
//   f = <function1>,
//   event = cats.effect.tracing.TracingEvent$StackTrace
// )

labelledGaugeExample.unsafeRunSync()
// res1: String = """# HELP gaugetotal Example Gauge
// # TYPE gaugetotal gauge
// gaugetotal{foo="bar"} 1.0
// gaugetotal{foo="baz"} 2.0
// """