Skip to content

prometheus

stable

Formats and parses Prometheus exposition text and provides a MetricRegistry class for building counter, gauge, histogram, and summary output.

use plugin prometheus::{format_counter, format_gauge, format_histogram_bucket, …}
16 functions Observability
/ filter jk navigate Esc clear
Functions (16)
  1. format_counter Format a counter metric line
  2. format_gauge Format a gauge metric line
  3. format_histogram_bucket Format a histogram bucket line
  4. format_help Format a `# HELP` comment line
  5. format_type Format a `# TYPE` comment line
  6. format_metric Format a generic metric line with optional timestamp
  7. parse_metric_line Parse a metric exposition line
  8. format_summary Format a summary metric with quantiles
  9. MetricRegistry Creates a registry that tracks counter, gauge, and histogram definitions and their samples.
  10. register_counter Register a counter definition
  11. register_gauge Register a gauge definition
  12. register_histogram Register a histogram definition
  13. inc Increment a counter
  14. set_gauge Set a gauge value
  15. observe Record a histogram observation
  16. expose Render full Prometheus exposition text

Format a counter metric line

Formats a single counter metric line. labels is an optional map of label key-value pairs.

use plugin prometheus::{format_counter}

let line = format_counter("http_requests_total", 142, #{"method": "GET", "status": "200"})
print(line)
// http_requests_total{method="GET",status="200"} 142

Format a gauge metric line

Formats a gauge metric line. Identical format to a counter line — use context and # TYPE to distinguish.

use plugin prometheus::{format_gauge, format_help, format_type}

print(format_help("memory_bytes", "Current process memory usage in bytes"))
print(format_type("memory_bytes", "gauge"))
print(format_gauge("memory_bytes", 52428800))

Format a histogram bucket line

Formats one histogram bucket line. le is the upper-bound string (use "+Inf" for the catch-all bucket).

use plugin prometheus::{format_histogram_bucket}

print(format_histogram_bucket("request_duration_seconds", "0.1", 45))
print(format_histogram_bucket("request_duration_seconds", "0.5", 89))
print(format_histogram_bucket("request_duration_seconds", "+Inf", 100))

Format a `# HELP` comment line

Returns a # HELP line for a metric.

use plugin prometheus::{format_help}

print(format_help("rpc_errors_total", "Total number of RPC errors"))
// # HELP rpc_errors_total Total number of RPC errors

Format a `# TYPE` comment line

Returns a # TYPE line. Valid type strings: counter, gauge, histogram, summary, untyped.

use plugin prometheus::{format_type}

print(format_type("rpc_errors_total", "counter"))
// # TYPE rpc_errors_total counter

Format a generic metric line with optional timestamp

Formats a metric line with optional labels and an optional Unix millisecond timestamp.

use plugin prometheus::{format_metric}

let line = format_metric("up", #{"job": "api"}, 1, 1609459200000)
print(line)
// up{job="api"} 1 1609459200000

Parse a metric exposition line

Parses a single Prometheus text exposition line and returns #{name, labels, value}. Returns nil for blank or comment lines.

use plugin prometheus::{parse_metric_line}

let m = parse_metric_line('http_requests_total{method="GET"} 142')
print(m["name"])   // http_requests_total
print(m["value"])  // 142

Format a summary metric with quantiles

Formats a complete summary block: quantile lines, _sum, and _count. quantiles is a table of #{quantile, value} entries.

use plugin prometheus::{format_summary}

let qs = [
  #{"quantile": "0.5", "value": 0.23},
  #{"quantile": "0.95", "value": 0.91},
  #{"quantile": "0.99", "value": 1.42}
]
print(format_summary("request_latency_seconds", qs, 47.3, 200))

Creates a registry that tracks counter, gauge, and histogram definitions and their samples.

Creates a registry that tracks counter, gauge, and histogram definitions and their samples. Use expose() to render the full text output.

use plugin prometheus::{MetricRegistry}

let reg = MetricRegistry()
reg.register_counter("api_calls_total", "Number of API calls")
reg.inc("api_calls_total", #{"endpoint": "/search"}, 1)
print(reg.expose())

Register a counter definition

Registers a counter definition in the registry. Must be called before inc.

reg.register_counter("cache_hits_total", "Cache lookup hits")

Register a gauge definition

Registers a gauge definition. Use set_gauge to update its value.

reg.register_gauge("active_connections", "Open TCP connections")
reg.set_gauge("active_connections", #{}, 42)

Register a histogram definition

Registers a histogram. buckets is an optional array of upper bound floats. Defaults to standard HTTP latency buckets (0.005..10).

reg.register_histogram("response_time_seconds", "Request latency",
  [0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 5.0])

Increment a counter

Increments a counter by amount (default 1). Labels is an optional map.

reg.inc("api_calls_total", #{"method": "POST"}, 1)
reg.inc("api_calls_total", #{"method": "GET"})

Set a gauge value

Sets a gauge to an exact value. Replaces any existing value for the same name and label set.

reg.set_gauge("active_connections", #{}, 17)

Record a histogram observation

Records an observation for a histogram. The value is sorted into the appropriate buckets on expose.

reg.observe("response_time_seconds", 0.043, #{"route": "/api/users"})

Render full Prometheus exposition text

Renders the full Prometheus text exposition for all registered metrics.

use plugin prometheus::{MetricRegistry}

let reg = MetricRegistry()
reg.register_counter("requests_total", "Requests served")
reg.inc("requests_total", #{})
let output = reg.expose()
print(output)
enespt-br