The registry module provides functions for managing and exporting metrics from a Prometheus registry. It supports both text-based and Protobuf serialization formats.
Functions
print_as_text
Serialises the registry using the Prometheus text-based format.
pub fn print_as_text(registry_name registry_name: String) -> String
The name of the registry to serialize
Returns
String - The metrics formatted as text in the Prometheus exposition format.
Example
import promgleam/registry
import promgleam/metrics/counter
pub fn main() {
let assert Ok(_) =
counter.create_counter(
registry: "default",
name: "http_requests_total",
help: "Total HTTP requests",
labels: []
)
let assert Ok(_) = counter.increment_counter(
registry: "default",
name: "http_requests_total",
labels: [],
value: 1
)
let metrics_text = registry.print_as_text(registry_name: "default")
// Returns text format metrics
}
print_as_protobuf
Serialises the registry using the Prometheus Protobuf format.
pub fn print_as_protobuf(registry_name registry_name: String) -> BitArray
The name of the registry to serialize
Returns
BitArray - The metrics encoded in Prometheus Protobuf format.
Example
import promgleam/registry
import promgleam/metrics/gauge
pub fn main() {
let assert Ok(_) =
gauge.create_gauge(
registry: "default",
name: "temperature",
help: "Current temperature",
labels: []
)
let assert Ok(_) = gauge.set_gauge(
registry: "default",
name: "temperature",
labels: [],
value: 23
)
let metrics_protobuf = registry.print_as_protobuf(registry_name: "default")
// Returns binary protobuf data
}
clear_registry
Removes all the previously registered metrics from a registry.
pub fn clear_registry(name: String) -> Nil
The name of the registry to clear
Returns
Nil - This function always succeeds and returns Nil.
Example
import promgleam/registry
import promgleam/metrics/counter
pub fn main() {
// Register some metrics
let assert Ok(_) =
counter.create_counter(
registry: "default",
name: "requests",
help: "Request count",
labels: []
)
let assert Ok(_) = counter.increment_counter(
registry: "default",
name: "requests",
labels: [],
value: 5
)
// Clear all metrics from the registry
registry.clear_registry("default")
// Registry is now empty
}