A counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart. For example, you can use a counter to represent the number of requests served, tasks completed, or errors.
Do not use a counter to expose a value that can decrease. For example, do not use a counter for the number of currently running processes; instead use a gauge.
create_counter
Creates a new Counter metric.
pub fn create_counter(
registry registry: String,
name name: String,
help help: String,
labels labels: List(String),
) -> Result(Nil, String)
The name of the registry to create the counter in (typically “default”).
The name of the counter metric.
A description of what the counter measures.
A list of label names for this metric. The order matters and must match when incrementing.
Returns
Ok(Nil) if the counter was created successfully
Error(String) with an error message if creation failed (e.g., “Metric already exists”)
Example
create_counter(
registry: "default",
name: "http_requests_total",
help: "Total number of HTTP requests",
labels: [ "method", "route", "status" ],
)
increment_counter
Increments the Counter with the given value.
pub fn increment_counter(
registry registry: String,
name name: String,
labels labels: List(String),
value value: Int,
) -> Result(Nil, String)
The name of the registry where the counter exists.
The name of the counter metric to increment.
A list of label values for this observation. Must match the number and order of labels defined when creating the counter.
The amount to increment the counter by. Must be a positive integer.
Returns
Ok(Nil) if the counter was incremented successfully
Error(String) with an error message if the operation failed (e.g., “Unknown metric” or “Invalid metric arity (labels mismatch)“)
Example
increment_counter(
registry: "default",
name: "http_requests_total",
labels: [ "GET", "/", "200" ],
value: 1,
)