prometheus_client/collector.rs
1//! Metric collector implementation.
2//!
3//! See [`Collector`] for details.
4
5use crate::encoding::DescriptorEncoder;
6
7/// The [`Collector`] abstraction allows users to provide additional metrics and
8/// their description on each scrape.
9///
10/// An example use-case is an exporter that retrieves a set of operating system metrics
11/// ad-hoc on each scrape.
12///
13/// Register a [`Collector`] with a [`Registry`](crate::registry::Registry) via
14/// [`Registry::register_collector`](crate::registry::Registry::register_collector).
15///
16/// ```
17/// # use prometheus_client::metrics::counter::ConstCounter;
18/// # use prometheus_client::collector::Collector;
19/// # use prometheus_client::encoding::{DescriptorEncoder, EncodeMetric};
20/// #
21/// #[derive(Debug)]
22/// struct MyCollector {}
23///
24/// impl Collector for MyCollector {
25/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
26/// let counter = ConstCounter::new(42u64);
27/// let metric_encoder = encoder.encode_descriptor(
28/// "my_counter",
29/// "some help",
30/// None,
31/// counter.metric_type(),
32/// )?;
33/// counter.encode(metric_encoder)?;
34/// Ok(())
35/// }
36/// }
37/// ```
38pub trait Collector: std::fmt::Debug + Send + Sync + 'static {
39 /// Once the [`Collector`] is registered, this method is called on each scrape.
40 fn encode(&self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error>;
41}