diff --git a/CHANGELOG.md b/CHANGELOG.md index 5394e8e3..c429a118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.21.2] + +### Added + +- Added `sub_registry_with_labels` method to `Registry`. + See [PR 145]. +- Added `with_labels` and `with_prefix_and_labels` constructors to `Registry`. + See [PR 147]. + +[PR 145]: https://github.com/prometheus/client_rust/pull/145 +[PR 147]: https://github.com/prometheus/client_rust/pull/147 + ## [0.21.1] ### Added diff --git a/Cargo.toml b/Cargo.toml index ebd184d4..a8bf0fee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "prometheus-client" -version = "0.21.1" +version = "0.21.2" authors = ["Max Inden "] edition = "2021" description = "Open Metrics client library allowing users to natively instrument applications." diff --git a/src/registry.rs b/src/registry.rs index 98a64082..0f252e3b 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -75,6 +75,28 @@ impl Registry { } } + /// Creates a new default [`Registry`] with the given labels. + pub fn with_labels( + labels: impl Iterator, Cow<'static, str>)>, + ) -> Self { + Self { + labels: labels.into_iter().collect(), + ..Default::default() + } + } + + /// Creates a new default [`Registry`] with the given prefix and labels. + pub fn with_prefix_and_labels( + prefix: impl Into, + labels: impl Iterator, Cow<'static, str>)>, + ) -> Self { + Self { + prefix: Some(Prefix(prefix.into())), + labels: labels.into_iter().collect(), + ..Default::default() + } + } + /// Register a metric with the [`Registry`]. /// /// Note: In the Open Metrics text exposition format some metric types have @@ -527,6 +549,53 @@ mod tests { use super::*; use crate::metrics::counter::Counter; + #[test] + fn constructors() { + let counter_name = "test_counter"; + let prefix = "test_prefix"; + let labels = vec![ + (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_1")), + (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_2")), + ]; + // test with_prefix constructor + let mut registry = Registry::with_prefix(prefix); + let counter: Counter = Counter::default(); + registry.register(counter_name, "some help", counter); + + assert_eq!( + Some((prefix.to_string() + "_" + counter_name, vec![])), + registry + .iter_metrics() + .map(|(desc, _)| (desc.name.clone(), desc.labels.clone())) + .next() + ); + + // test with_labels constructor + let mut registry = Registry::with_labels(labels.clone().into_iter()); + let counter: Counter = Counter::default(); + registry.register(counter_name, "some help", counter); + assert_eq!( + Some((counter_name.to_string(), labels.clone())), + registry + .iter_metrics() + .map(|(desc, _)| (desc.name.clone(), desc.labels.clone())) + .next() + ); + + // test with_prefix_and_labels constructor + let mut registry = Registry::with_prefix_and_labels(prefix, labels.clone().into_iter()); + let counter: Counter = Counter::default(); + registry.register(counter_name, "some help", counter); + + assert_eq!( + Some((prefix.to_string() + "_" + counter_name, labels)), + registry + .iter_metrics() + .map(|(desc, _)| (desc.name.clone(), desc.labels.clone())) + .next() + ); + } + #[test] fn register_and_iterate() { let mut registry = Registry::default();