Module std.metrics
Developer-defined application metrics.
Declare your own counters, gauges, and histograms; their live values flow
into the same scrape surface as the runtime built-ins, readable through
std.observe.
Examples
import std.metrics;
import std.observe;
fn main() {
let requests = match metrics.counter("app.requests") {
.Ok(counter) => counter,
.Err(error) => panic(to_string(error)),
};
requests.inc();
requests.add(4);
let active = match metrics.gauge("app.active_connections") {
.Ok(gauge) => gauge,
.Err(error) => panic(to_string(error)),
};
active.set(3);
active.dec();
print(observe.scrape());
}
Handles
Counter, Gauge, and Histogram are Copy handles — each wraps the
i64 slot index the runtime registry hands back. They own no heap memory,
so there is no free() or close(): copying a handle is just copying the
index, and the registry value lives for the session.
A name is registered once. Calling metrics.counter("x") a second time with
the same name returns a handle to the same slot, so the same metric can be
reached from anywhere in a program without threading the handle through.
Names
Metric names follow the Prometheus charset [a-zA-Z_:][a-zA-Z0-9_:.]*. The
scrape renders a name to its Prometheus series by mapping . to _. A name
that is malformed, that exceeds the registry's caps, or whose rendered series
name collides with a runtime built-in or an already-registered user metric
(heap_live_bytes vs the built-in heap.live_bytes; foo.bar vs foo_bar)
is rejected, so a user metric cannot shadow or alias another's scrape output.
The constructors return a MetricsError when
registration is rejected.
Contents
Functions
Function counter
pub fn counter(name: string) -> Result<Counter, MetricsError>
Register (or get) a counter named name.
Examples
import std.metrics;
fn main() {
let hits = match metrics.counter("app.cache_hits") {
.Ok(counter) => counter,
.Err(error) => panic(to_string(error)),
};
hits.inc();
}
Returns a structured error if the registry rejects the name.
Function gauge
pub fn gauge(name: string) -> Result<Gauge, MetricsError>
Register (or get) a gauge named name.
Returns a structured error if the registry rejects the name.
Function histogram
pub fn histogram(name: string) -> Result<Histogram, MetricsError>
Register (or get) a histogram named name.
The histogram records observations; the scrape exposes the running count
and sum as name_count / name_sum. This API does not register custom
bucket upper bounds or emit name_bucket lines.
Returns a structured error if the registry rejects the name.
Types
Enum MetricsError
Why a metric registration was rejected.
Variants
Rejected(string)The name was malformed, exceeded a registry cap, or collided with a runtime built-in metric. The registry counts the rejection in its
hew_metrics_*_dropped_totalself-metrics, visible in the scrape.
Struct Counter
A monotonic counter handle.
Created by metrics.counter(name). Copy: the handle is the registry slot
index, not an owning pointer.
Fields
- id: i64
Methods
fn inc(self)
Increment the counter by one.
fn add(self, n: i64)
Add n to the counter. A negative n is rejected by the runtime and
counted in the registry self-metrics (counters are monotonic).
Struct Gauge
A bidirectional gauge handle.
Created by metrics.gauge(name). Copy: the handle is the registry slot
index, not an owning pointer.
Fields
- id: i64
Methods
fn inc(self)
Increment the gauge by one.
fn dec(self)
Decrement the gauge by one.
fn set(self, n: i64)
Set the gauge to n.
fn add(self, n: i64)
Add n (any sign) to the gauge.
Struct Histogram
A histogram handle.
Created by metrics.histogram(name). Copy: the handle is the registry
slot index, not an owning pointer.
This handle records observations; the scrape exposes the running
observation count and sum as valid name_count / name_sum lines.
The stdlib surface exposes count and sum; it has no custom bucket API.
Fields
- id: i64
Methods
fn observe(self, v: f64)
Record one observation of value v; fractional values are preserved
in the rendered histogram _sum sample.