Function prometheus_client::encoding::text::encode_registry

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

Encode the metrics registered with the provided Registry into the provided Writer using the OpenMetrics text format.

Note: The OpenMetrics exposition requires that a complete message must end with an EOF marker.

This function may be called repeatedly for the HTTP scrape response until encode_eof signals the end of the response.

This may also be used to compose a partial message with metrics assembled from multiple registries.

§Examples

// Initialize registry with a counter
let mut reg_counter = Registry::default();
let counter: Counter = Counter::default();
reg_counter.register(
    "my_counter",
    "This is my counter",
    counter.clone(),
);

// Encode the counter registry into the buffer
let mut buffer = String::new();
encode_registry(&mut buffer, &reg_counter)?;

// Initialize another registry but with a gauge
let mut reg_gauge = Registry::default();
let gauge: Gauge = Gauge::default();
reg_gauge.register(
  "my_gauge",
  "This is my gauge",
  gauge.clone(),
);

// Encode the gauge registry into the buffer
encode_registry(&mut buffer, &reg_gauge)?;