Skip to main content

PrometheusRenderer

Struct PrometheusRenderer 

Source
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

Source

pub fn new() -> Self

Creates a new PrometheusRenderer.

Source

pub fn clear(&mut self)

Clears the output buffer to start a fresh payload, retaining allocated capacity.

Source

pub fn output(&self) -> &str

Returns the accumulated output as a string slice.

Source

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 (_).

Source

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.

Source

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.

Source

pub fn write_gauge_or_counter_series<L, K, V>(&mut self, labels: L, value: f64)
where L: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<str>,

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.

Source

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.

Source

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).

Source

pub fn finish_group(&mut self)

Finishes the current metric group, appending the metric buffer to the output.

Trait Implementations§

Source§

impl Default for PrometheusRenderer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.