metrics API reference Changelog View on GitHub

API reference

Prometheus text exposition for Zig: write metrics to any writer, with counters, gauges, and histograms above it, and no HTTP.

prometheus.Writer is the format: primitives (help, kind, name, value, line) and the compositions built on them (writeMetric, writeFamily, writeHistogram, and the series form a multi-series histogram family loops). Each exposition format is its own namespace, so a second one would sit beside it rather than become a flag.

counter and gauge are the recording side: an integer with the storage and the concurrency model chosen where it is declared, and no write method. The writer writes; get supplies the number.

module counter

src

A recording counter: an unsigned integer and an add, with the storage and the concurrency model chosen where the counter is declared.

more

Local(T) is one field and a +%. It belongs to whoever holds it, which is a worker thread or a struct the caller already owns. Atomic(T) stores the same integer with monotonic atomic operations, for the case where a scrape reads a counter another thread is incrementing. The constructor names the storage and T names the value type.

Both wrap at maxInt(T) and keep counting. A counter does not saturate: saturating turns a visible reset into a silent flat line.

There is no set and no write method. A counter only goes up, and the bytes on the wire are the writer's job: read it with get and pass the number.

Types

type fn Local

src
pub fn Local(comptime T: type) type

A counter whose field the caller owns. Only the owner writes it.

The name says the storage; the argument says the value type. Local(u64) is a plain u64 field; Atomic(u32) is an atomic u32.

Fields

value: S

Constants

pub const ValueType = S.Value

src

pub const atomic = S.atomic

src

True when the field is atomic, which decides whether a histogram can hand out its buckets as a slice.

pub const zero: Self = .{ .value = .zero }

src

A counter at zero.

Functions

pub inline fn inc(self: *Self) void

src

One more.

pub inline fn add(self: *Self, n: S.Value) void

src

Add n. Wraps at maxInt(T).

pub inline fn get(self: *const Self) S.Value

src

The value now.

type fn Atomic

src
pub fn Atomic(comptime T: type) type

A counter any thread may write and any thread may read.

Fields

value: S

Constants

pub const ValueType = S.Value

src

pub const atomic = S.atomic

src

True when the field is atomic, which decides whether a histogram can hand out its buckets as a slice.

pub const zero: Self = .{ .value = .zero }

src

A counter at zero.

Functions

pub inline fn inc(self: *Self) void

src

One more.

pub inline fn add(self: *Self, n: S.Value) void

src

Add n. Wraps at maxInt(T).

pub inline fn get(self: *const Self) S.Value

src

The value now.

module gauge

src

A recording gauge: a number that moves both ways, with the storage chosen by the constructor and the value type by the argument.

more

Local(T) is one field and belongs to whoever holds it, which is a worker thread or a struct the caller already owns. Atomic(T) stores the same value with monotonic atomic operations, and a float as the integer of its bit pattern, for a gauge a scrape reads while another thread writes it.

A gauge holds a temperature, a delta, a queue depth that dips below its last reading, so its integer may be signed and its float is f32 or f64. The counter has neither.

There is no write method. The bytes on the wire are the writer's job: read the gauge with get and pass the number.

Types

type fn Local

src
pub fn Local(comptime T: type) type

A gauge whose field the caller owns. Only the owner writes it.

The name says the storage; the argument says the value type. Local(i64) is an i64 field, Local(f64) an f64, and Atomic(u32) an atomic u32.

Fields

value: S

Constants

pub const ValueType = S.Value

src

pub const zero: Self = .{ .value = .zero }

src

A gauge at zero.

Functions

pub inline fn set(self: *Self, v: S.Value) void

src

Replace the value.

pub inline fn add(self: *Self, n: S.Value) void

src

Add n. Wraps at maxInt(T).

pub inline fn sub(self: *Self, n: S.Value) void

src

Subtract n, wrapping at the type's bounds.

more

Nothing here looks for a dip below the type's floor. A gauge that goes negative is a + and a - that do not match, and the arithmetic cannot find the second one; saturating would turn the mismatch into a flat line, which is the same bug with less evidence. A float has no floor at all: going negative is the point.

pub inline fn inc(self: *Self) void

src

One more.

pub inline fn dec(self: *Self) void

src

One less.

pub inline fn get(self: *const Self) S.Value

src

The value now.

type fn Atomic

src
pub fn Atomic(comptime T: type) type

A gauge any thread may write and any thread may read: an integer or a float.

Fields

value: S

Constants

pub const ValueType = S.Value

src

pub const zero: Self = .{ .value = .zero }

src

A gauge at zero.

Functions

pub inline fn set(self: *Self, v: S.Value) void

src

Replace the value.

pub inline fn add(self: *Self, n: S.Value) void

src

Add n. Wraps at maxInt(T).

pub inline fn sub(self: *Self, n: S.Value) void

src

Subtract n, wrapping at the type's bounds.

more

Nothing here looks for a dip below the type's floor. A gauge that goes negative is a + and a - that do not match, and the arithmetic cannot find the second one; saturating would turn the mismatch into a flat line, which is the same bug with less evidence. A float has no floor at all: going negative is the point.

pub inline fn inc(self: *Self) void

src

One more.

pub inline fn dec(self: *Self) void

src

One less.

pub inline fn get(self: *const Self) S.Value

src

The value now.

module histogram

src

A fixed-bucket histogram: a bucket array, a count, and a sum, each slot the same recording counter type the caller picks.

more

Fixed(counter.Atomic(u64), &.{ 10, 100 }) gives atomic buckets for a scrape that reads while workers observe. Fixed(counter.Local(u64), ...) gives plain fields for a histogram one thread owns. The sum is exact because it is an integer counter, not a float, so nothing loses digits above 2^53.

Observations are u64: a byte count, a subscriber count, or microseconds. Microseconds stand in for seconds the same way the counter accumulates nanoseconds, which keeps the comparison and the sum in whole numbers.

Buckets are cumulative at observe, so the scrape reads what the writer wants and writeHistogramSeries takes it unchanged. Reading differs by mode: a local histogram hands out its buckets as a slice, and an atomic one fills a Snapshot the caller owns, because a slice of atomics read without @atomicLoad is a race.

A histogram is additive, which is what a per-worker collect-then-aggregate scrape needs: merge adds one histogram into another and mergeAll folds a slice of them into one Snapshot, adding the buckets, the count, and the sum. mergeAllPtrs is the same fold over pointers, for histograms that live in another struct. All of it wraps like the counters it adds, and none of it takes a lock.

Functions

pub fn linear(comptime start: f64, comptime width: f64, comptime count: usize) [count]f64

src

count bounds from start, each width above the one before, for Fixed(S, &histogram.linear(1, 10, 5)). The +Inf bucket is implicit, so count is the finite buckets only.

pub fn exponential(comptime start: f64, comptime factor: f64, comptime count: usize) [count]f64

src

count bounds from start, each one factor times the last, for Fixed(S, &histogram.exponential(1, 2, 10)). Same implicit +Inf.

Types

type fn Fixed

src
pub fn Fixed(comptime S: type, comptime bounds: []const f64) type

A histogram with bounds.len finite buckets. S is a recording counter type whose value is u64, such as counter.Atomic(u64).

Fields

buckets: [bounds.len]S
total: S

Constants

pub const limits = bounds

src

The bucket limits, the array writeHistogram takes.

pub const Slot = S

src

pub const zero: Self = …

src
value
.{
            .buckets = @splat(.zero),
            .observed = .zero,
            .total = .zero,
        }

A histogram with nothing observed.

Functions

pub fn snapshot(self: *const Self, out: *Snapshot) void

src

Fill out with everything a scrape needs: the counts are already cumulative, so &out.buckets, out.total, and out.observed go straight to writeHistogramSeries. An atomic histogram has this; a local one has counts.

pub fn counts(self: *const Self) []const u64

src

The bucket counts, borrowed from the histogram. The slots are one integer each and nothing sits between them, so this is the histogram's own bytes rather than a copy.

pub fn observe(self: *Self, v: u64) void

src

One observation of v. Every bucket whose bound is at or above v goes up, which is what makes the counts cumulative.

pub fn merge(self: *Self, other: *const Self) void

src

Add other into this histogram: its buckets, its count, and its sum. other is read before this histogram is written, so the two may be the same histogram, which doubles it.

pub fn mergeAll(histograms: []const Self) Snapshot

src

Every histogram in histograms added together, as if each one were merged in turn, and handed back as the snapshot a scrape writes. One call holds the total in registers instead of writing it back per merge.

pub fn mergeAllPtrs(histograms: []const *const Self) Snapshot

src

mergeAll over a slice of pointers, for the histograms that live in another struct instead of in a run of their own: a worker's histogram is a field of the worker's struct, not next to the next worker's, and copying it into an array to fold it is the work this avoids. The two agree count for count; only the indirection differs.

Types

struct Snapshot

src

What a scrape reads: the bucket counts, how many observations there were, and what they summed to. The caller owns the storage, so this is [bounds.len]u64 plus two words on the stack. Atomic slots need it filled; a local histogram can take counts and skip the copy.

Fields

buckets: [bounds.len]u64
observed: u64
total: u64

Constants

pub const limits = bounds

src

The bucket limits this snapshot counts, so a writer can take a folded Snapshot where it takes the histogram it came from.

pub const zero: Snapshot = …

src
value
.{
                .buckets = @splat(0),
                .observed = 0,
                .total = 0,
            }

A snapshot with nothing counted, which is what a fold starts from.

Functions

pub fn merge(self: *Snapshot, other: *const Snapshot) void

src

Add every number in other into this snapshot: the buckets, the count, and the sum. Wraps at maxInt(u64), like the counters being folded, so a merge cannot saturate what the observations would have wrapped.

pub fn mergeAll(others: []const Snapshot) Snapshot

src

Every snapshot in others added together, as if each one were merged in turn. One call holds the total in registers instead of writing it back per merge, which measures about 2.7x a caller's merge loop at twelve buckets.

pub fn mergeAllPtrs(others: []const *const Snapshot) Snapshot

src

mergeAll over a slice of pointers, for the snapshots that live in another struct instead of in a buffer of their own. The two agree count for count; only the indirection differs.

module prometheus

src

The Prometheus text exposition format.

Types

struct Writer

src
pub const Writer = writer.Writer

Fields

out: *Io.Writer

Functions

pub fn init(out: *Io.Writer) Writer

src

pub fn help( self: *const Writer, comptime metric_name: []const u8, text: []const u8, ) Io.Writer.Error!void

src

# HELP name text, with the text escaped. An empty text writes the line without a trailing space.

pub fn kind( self: *const Writer, comptime metric_name: []const u8, type_kind: Kind, ) Io.Writer.Error!void

src

# TYPE name kind.

pub fn name( self: *const Writer, comptime name_text: []const u8, labels: anytype, ) Io.Writer.Error!void

src

name{label="value",...} with no trailing space. A struct gives its fields in order; a tuple of structs gives their fields in sequence, which is how a caller adds a label of its own to one it already has.

pub fn value(self: *const Writer, value_text: anytype) Io.Writer.Error!void

src

value and the newline that ends the record. An integer type is written as decimal; a float follows the expfmt rule in value.zig.

pub fn line(self: *const Writer, bytes: []const u8) Io.Writer.Error!void

src

Bytes the caller already formatted. Written as given: no newline, no escaping.

pub fn writeMetric( self: *const Writer, comptime name_text: []const u8, label_values: anytype, value_text: anytype, ) Io.Writer.Error!void

src

One sample line: the name, the labels, the value, and the newline.

pub fn writeFamily(self: *const Writer, comptime m: Metric) Io.Writer.Error!FamilyWriter(m)

src

# HELP, # TYPE, and the handle its samples go through.

pub fn writeHistogram( self: *const Writer, comptime m: Metric, label_values: anytype, limits: []const f64, counts: []const u64, sum: anytype, count: u64, ) Io.Writer.Error!void

src

A whole classic histogram for one label set: the header and one series.

A family with more than one series cannot use this twice, because each call writes # HELP and # TYPE again and the parser rejects the second. Write writeFamily once and loop FamilyWriter.writeHistogramSeries instead.

enum Kind

src
pub const Kind = writer.Kind

The # TYPE line's vocabulary.

Fields

counter counter
gauge gauge
histogram histogram
untyped untyped

struct Metric

src
pub const Metric = writer.Metric

One family: the name, its kind, and the help text. The name and kind are read at comptime, which is where a bad one is caught.

Fields

name: []const u8
kind: Kind
help: []const u8 = ""

type fn FamilyWriter

src
pub const FamilyWriter = writer.FamilyWriter

A family's samples, from the value writeFamily handed back. It carries the family's name, which is why the series form hangs off it: a bare Writer has nowhere to get one.

Fields

writer: Writer

Functions

pub fn write( self: *const Self, label_values: anytype, value_text: anytype, ) Io.Writer.Error!void

src

pub fn writeHistogramSeries( self: *const Self, label_values: anytype, limits: []const f64, counts: []const u64, sum: anytype, count: u64, ) Io.Writer.Error!void

src

One histogram series: the bucket lines, _sum, and _count, with no header. A family with more than one series writes help and kind once through writeFamily and then calls this per series.

sum is anytype so a caller whose sum is a u64 keeps every digit; a f64 cannot hold one above 2^53.