diff --git a/src/registry.rs b/src/registry.rs index ed9ecaa..bc743f8 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -81,24 +81,40 @@ impl Registry { registry.register_all(metrics_group_set) } - fn encode_inner(&self, writer: &mut impl Write) -> fmt::Result { + /// Encodes all metrics in the OpenMetrics text format. + /// + /// This does not write the terminal `# EOF\n` string to `writer`. + /// You can use [`encode_openmetrics_eof`] to do that. + pub fn encode_openmetrics_to_writer(&self, writer: &mut impl Write) -> fmt::Result { for group in &self.metrics { group.encode_openmetrics(writer, self.prefix.as_deref(), &self.labels)?; } for sub in self.sub_registries.iter() { - sub.encode_inner(writer)?; + sub.encode_openmetrics_to_writer(writer)?; } Ok(()) } } +/// Writes `# EOF\n` to `writer`. +/// +/// This is the expected last characters of an OpenMetrics string. +pub fn encode_openmetrics_eof(writer: &mut impl Write) -> fmt::Result { + write_eof(writer) +} + /// Helper trait to abstract over different ways to access metrics. pub trait MetricsSource: Send + 'static { /// Encodes all metrics into a string in the OpenMetrics text format. + /// + /// This is expected to also write the terminal `# EOF\n` string expected + /// by the OpenMetrics format. fn encode_openmetrics(&self, writer: &mut impl std::fmt::Write) -> Result<(), Error>; /// Encodes the metrics in the OpenMetrics text format into a newly allocated string. + /// + /// See also [`Self::encode_openmetrics`]. fn encode_openmetrics_to_string(&self) -> Result { let mut s = String::new(); self.encode_openmetrics(&mut s)?; @@ -108,7 +124,7 @@ pub trait MetricsSource: Send + 'static { impl MetricsSource for Registry { fn encode_openmetrics(&self, writer: &mut impl std::fmt::Write) -> Result<(), Error> { - self.encode_inner(writer)?; + self.encode_openmetrics_to_writer(writer)?; write_eof(writer)?; Ok(()) } diff --git a/src/static_core.rs b/src/static_core.rs index f17888f..5df9058 100644 --- a/src/static_core.rs +++ b/src/static_core.rs @@ -35,7 +35,7 @@ use std::sync::OnceLock; use erased_set::ErasedSyncSet; -use crate::{Error, MetricsGroup, NoMetricsSnafu, Registry}; +use crate::{Error, MetricsGroup, MetricsSource, NoMetricsSnafu, Registry}; #[cfg(not(feature = "metrics"))] type Registry = (); @@ -46,7 +46,7 @@ type Registry = (); #[derive(Clone, Copy, Debug)] pub struct GlobalRegistry; -impl crate::MetricsSource for GlobalRegistry { +impl MetricsSource for GlobalRegistry { fn encode_openmetrics(&self, writer: &mut impl std::fmt::Write) -> Result<(), Error> { let core = crate::static_core::Core::get().ok_or(NoMetricsSnafu.build())?; core.registry.encode_openmetrics(writer) @@ -86,7 +86,7 @@ impl Core { #[cfg(feature = "metrics")] registry, }) - .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "already set")) + .map_err(|_| std::io::Error::other("already set")) } /// Returns a reference to the core metrics.