Skip to content
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

feat(encoding): new encode_registry and encode_end functions to compose cross-registry responses #154

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,89 @@
/// Encode the metrics registered with the provided [`Registry`] into the
/// provided [`Write`]r using the OpenMetrics text format.
pub fn encode<W>(writer: &mut W, registry: &Registry) -> Result<(), std::fmt::Error>
where
W: Write,
{
encode_registry(writer, registry)?;
encode_end(writer)
}

/// Encode the metrics registered with the provided
/// [`Registry`] into the provided [`Write`]r using the OpenMetrics text
/// format.
///
/// Can call repeatedly for the HTTP scrape response, then finish with a
/// call to [`encode_end`](encode_end) to end the response.

Check warning on line 51 in src/encoding/text.rs

View workflow job for this annotation

GitHub Actions / Check rustdoc intra-doc links

redundant explicit link target
///
/// /// Useful when assembling metrics from multiple registries.
///
/// Here is a simple example:
/// ```
/// # use prometheus_client::metrics::counter::Counter;
/// # use prometheus_client::metrics::histogram::{Histogram, exponential_buckets};
/// # use prometheus_client::registry::Registry;
/// # use prometheus_client::registry::Unit;
/// # use prometheus_client::encoding::text::{encode_registry, encode_end};
/// #
/// # fn main() -> Result<(), std::fmt::Error> {
amunra marked this conversation as resolved.
Show resolved Hide resolved
/// let mut orders_registry = Registry::default();
/// let total_orders: Counter<u64> = Default::default();
/// let processing_times = Histogram::new(exponential_buckets(1.0, 2.0, 10));
/// orders_registry.register(
/// "orders",
/// "Total orders received",
/// total_orders.clone(),
/// );
/// orders_registry.register_with_unit(
/// "processing_times",
/// "Order times",
/// Unit::Seconds,
/// processing_times.clone(),
/// );
///
/// total_orders.inc();
/// processing_times.observe(2.4);
///
/// let mut user_auth_registry = Registry::default();
/// let successful_logins: Counter<u64> = Default::default();
/// let failed_logins: Counter<u64> = Default::default();
/// user_auth_registry.register(
/// "successful_logins",
/// "Total successful logins",
/// successful_logins.clone(),
/// );
/// user_auth_registry.register(
/// "failed_logins",
/// "Total failed logins",
/// failed_logins.clone(),
/// );
///
/// successful_logins.inc();
///
/// let mut response = String::new();
/// encode_registry(&mut response, &orders_registry)?;
/// # assert_eq!(&response[response.len() - 20..], "bucket{le=\"+Inf\"} 1\n");
/// encode_registry(&mut response, &user_auth_registry)?;
/// # assert_eq!(&response[response.len() - 20..], "iled_logins_total 0\n");
/// encode_end(&mut response)?;
/// # assert_eq!(&response[response.len() - 20..], "ogins_total 0\n# EOF\n");
/// # Ok(())
/// # }
pub fn encode_registry<W>(writer: &mut W, registry: &Registry) -> Result<(), std::fmt::Error>
where
W: Write,
{
registry.encode(&mut DescriptorEncoder::new(writer).into())?;
Ok(())
}

/// Encode the end of the response.
/// Must be called after one or more calls to [`encode_registry`](encode_registry)

Check warning on line 116 in src/encoding/text.rs

View workflow job for this annotation

GitHub Actions / Check rustdoc intra-doc links

redundant explicit link target
/// to ensure protocol compliance.
pub fn encode_end<W>(writer: &mut W) -> Result<(), std::fmt::Error>
where
W: Write,
{
writer.write_str("# EOF\n")?;
Ok(())
}
Expand Down
Loading