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