Skip to content

feat: allow to encode a registry in openmetrics without EOF #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Error> {
let mut s = String::new();
self.encode_openmetrics(&mut s)?;
Expand All @@ -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(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/static_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
Loading