viva_telemetry/metrics

viva_telemetry/metrics - Metrics collection for Gleam

Inspired by: Prometheus, statsd, BEAM telemetry

Features

Quick Start

import viva_telemetry/metrics

pub fn main() {
  // Counter
  let requests = metrics.counter("http_requests_total")
  metrics.inc(requests)

  // Gauge
  let connections = metrics.gauge("active_connections")
  metrics.set(connections, 42.0)

  // Histogram
  let latency = metrics.histogram("request_latency_ms", [10.0, 50.0, 100.0, 500.0])
  metrics.observe(latency, 75.5)

  // Export
  let output = metrics.to_prometheus()
}

Types

BEAM memory info

pub type BeamMemory {
  BeamMemory(
    total: Int,
    processes: Int,
    processes_used: Int,
    system: Int,
    atom: Int,
    atom_used: Int,
    binary: Int,
    code: Int,
    ets: Int,
  )
}

Constructors

  • BeamMemory(
      total: Int,
      processes: Int,
      processes_used: Int,
      system: Int,
      atom: Int,
      atom_used: Int,
      binary: Int,
      code: Int,
      ets: Int,
    )

A counter metric (monotonically increasing)

pub type Counter {
  Counter(name: String, labels: dict.Dict(String, String))
}

Constructors

  • Counter(name: String, labels: dict.Dict(String, String))

A gauge metric (can go up or down)

pub type Gauge {
  Gauge(name: String, labels: dict.Dict(String, String))
}

Constructors

  • Gauge(name: String, labels: dict.Dict(String, String))

A histogram metric (distribution of values)

pub type Histogram {
  Histogram(
    name: String,
    buckets: List(Float),
    labels: dict.Dict(String, String),
  )
}

Constructors

  • Histogram(
      name: String,
      buckets: List(Float),
      labels: dict.Dict(String, String),
    )

Values

pub fn beam_memory() -> BeamMemory

Get current BEAM memory usage

pub fn counter(name: String) -> Counter

Create a new counter

pub fn counter_with_labels(
  name: String,
  labels: List(#(String, String)),
) -> Counter

Create a counter with labels

pub fn default_latency_buckets() -> List(Float)

Default buckets for latency (ms)

pub fn gauge(name: String) -> Gauge

Create a new gauge

pub fn gauge_add(gauge: Gauge, value: Float) -> Nil

Add to gauge

pub fn gauge_dec(gauge: Gauge) -> Nil

Decrement gauge by 1

pub fn gauge_inc(gauge: Gauge) -> Nil

Increment gauge by 1

pub fn gauge_with_labels(
  name: String,
  labels: List(#(String, String)),
) -> Gauge

Create a gauge with labels

pub fn get_counter(counter: Counter) -> Int

Get counter value

pub fn get_gauge(gauge: Gauge) -> Float

Get gauge value

pub fn get_histogram_stats(histogram: Histogram) -> #(Int, Float)

Get histogram stats

pub fn histogram(name: String, buckets: List(Float)) -> Histogram

Create a new histogram with buckets

pub fn histogram_with_labels(
  name: String,
  buckets: List(Float),
  labels: List(#(String, String)),
) -> Histogram

Create a histogram with labels

pub fn inc(counter: Counter) -> Nil

Increment counter by 1

pub fn inc_by(counter: Counter, value: Int) -> Nil

Increment counter by value

pub fn observe(histogram: Histogram, value: Float) -> Nil

Observe a value in the histogram

pub fn set(gauge: Gauge, value: Float) -> Nil

Set gauge value

pub fn time(histogram: Histogram, f: fn() -> a) -> a

Time a function and record duration in histogram (microseconds)

Example:

let latency = metrics.histogram("request_latency_us", metrics.default_latency_buckets())
let result = metrics.time(latency, fn() { do_work() })
pub fn time_ms(histogram: Histogram, f: fn() -> a) -> a

Time a function and record duration in histogram (milliseconds)

pub fn to_prometheus() -> String

Export all metrics in Prometheus text format

Search Document