Function prometheus_client::encoding::text::encode

source ·
pub fn encode<W>(writer: &mut W, registry: &Registry) -> Result<(), Error>
where W: Write,
Expand description

Encode both the metrics registered with the provided Registry and the EOF marker into the provided Writer using the OpenMetrics text format.

Note: This function encodes the complete OpenMetrics exposition.

Use encode_registry or encode_eof if partial encoding is needed.

See OpenMetrics exposition format for additional details.

§Examples

// Initialize registry with metric families
let mut registry = Registry::default();
let counter: Counter = Counter::default();
registry.register(
    "my_counter",
    "This is my counter",
    counter.clone(),
);
let gauge: Gauge = Gauge::default();
registry.register(
    "my_gauge",
    "This is my gauge",
    gauge.clone(),
);

// Encode the complete OpenMetrics exposition into the buffer
let mut buffer = String::new();
encode(&mut buffer, &registry)?;