pub struct PrometheusRenderer { /* private fields */ }Expand description
A renderer for the Prometheus text exposition format.
The renderer owns internal buffers that are reused across calls, amortizing allocation costs when rendering multiple payloads over time.
§Usage
There are two ways to use the renderer:
High-level: Use [render_group][Self::render_group] for counter/gauge groups where all series are simple
label/value pairs. This handles the TYPE/HELP headers and all series lines in one call.
Low-level: Use begin_group to start a new metric group (writes TYPE/HELP headers), then
call individual series writing methods like write_gauge_or_counter_series,
write_histogram_series, or
write_summary_series for each tag combination. Finally, call
finish_group to append the group to the output. This is useful when you have multiple series
with complex types (histograms/summaries) sharing a single TYPE header.
Implementations§
Source§impl PrometheusRenderer
impl PrometheusRenderer
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the output buffer to start a fresh payload, retaining allocated capacity.
Sourcepub fn normalize_metric_name<'a>(&'a mut self, name: &str) -> &'a str
pub fn normalize_metric_name<'a>(&'a mut self, name: &str) -> &'a str
Normalizes a metric name to a valid Prometheus metric name, returning a reference to the internal name buffer.
Periods (.) are converted to double underscores (__). Other invalid characters are
converted to a single underscore (_).
Sourcepub fn render_scalar_group<S, L, K, V>(
&mut self,
metric_name: &str,
metric_type: MetricType,
help_text: Option<&str>,
series: S,
)where
S: IntoIterator<Item = (L, f64)>,
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
pub fn render_scalar_group<S, L, K, V>(
&mut self,
metric_name: &str,
metric_type: MetricType,
help_text: Option<&str>,
series: S,
)where
S: IntoIterator<Item = (L, f64)>,
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
Renders a counter or gauge metric group: # TYPE header, optional # HELP header, and all series lines.
The metric name is automatically normalized to a valid Prometheus name.
Sourcepub fn begin_group(
&mut self,
metric_name: &str,
metric_type: MetricType,
help_text: Option<&str>,
)
pub fn begin_group( &mut self, metric_name: &str, metric_type: MetricType, help_text: Option<&str>, )
Begins a new metric group by writing the # TYPE and optional # HELP headers.
The metric name is automatically normalized to a valid Prometheus name and stored internally for use by subsequent series writing methods.
After calling this, write individual series using write_gauge_or_counter_series,
write_histogram_series, or
write_summary_series. When done, call finish_group.
Sourcepub fn write_gauge_or_counter_series<L, K, V>(&mut self, labels: L, value: f64)
pub fn write_gauge_or_counter_series<L, K, V>(&mut self, labels: L, value: f64)
Writes a single counter or gauge series line to the current metric group.
Uses the metric name set by the most recent begin_group call.
Sourcepub fn write_histogram_series<L, K, V, B>(
&mut self,
labels: L,
buckets: B,
sum: f64,
count: u64,
)where
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
B: IntoIterator<Item = (&'static str, u64)>,
pub fn write_histogram_series<L, K, V, B>(
&mut self,
labels: L,
buckets: B,
sum: f64,
count: u64,
)where
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
B: IntoIterator<Item = (&'static str, u64)>,
Writes a single histogram series (one set of labels) to the current metric group.
Uses the metric name set by the most recent begin_group call.
Each bucket is (upper_bound_str, cumulative_count). A +Inf bucket is automatically
appended using the total count.
Sourcepub fn write_summary_series<L, K, V, Q>(
&mut self,
labels: L,
quantiles: Q,
sum: f64,
count: u64,
)where
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
Q: IntoIterator<Item = (f64, f64)>,
pub fn write_summary_series<L, K, V, Q>(
&mut self,
labels: L,
quantiles: Q,
sum: f64,
count: u64,
)where
L: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<str>,
Q: IntoIterator<Item = (f64, f64)>,
Writes a single summary series (one set of labels) to the current metric group.
Uses the metric name set by the most recent begin_group call.
Each quantile is (quantile_value, observed_value).
Sourcepub fn finish_group(&mut self)
pub fn finish_group(&mut self)
Finishes the current metric group, appending the metric buffer to the output.