From c6e6b03fee556f36d6365d78f61633c3a4363c21 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Tue, 30 May 2023 00:24:32 +0800 Subject: [PATCH 1/6] Fix #146: Add with_prefix_and_labels constructor to Registry Signed-off-by: Adrian Pop --- src/registry.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/registry.rs b/src/registry.rs index fabec4b0..b5c4bd0c 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -75,6 +75,18 @@ impl Registry { } } + /// 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 @@ -518,6 +530,40 @@ mod tests { use super::*; use crate::metrics::counter::Counter; + #[test] + fn constructors() { + let prefix = "test_prefix"; + let counter_name = "test_counter"; + + 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() + ); + + let labels = vec![ + (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_1")), + (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_2")), + ]; + 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(); From 1ce41b9cf0b5165cf71713579c51ef5227e7424c Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Sat, 8 Jul 2023 15:27:02 +0100 Subject: [PATCH 2/6] Fix #146: Add with_labels constructor to Registry Signed-off-by: Adrian Pop --- src/registry.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/registry.rs b/src/registry.rs index b5c4bd0c..48921ed3 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -75,6 +75,16 @@ 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, From 6e2173c3776e9a4e0fe1e2018993d2da97cf94fa Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Sat, 8 Jul 2023 15:29:40 +0100 Subject: [PATCH 3/6] Fix #146: Bump version in toml file & update changelog Signed-off-by: Adrian Pop --- CHANGELOG.md | 12 ++++++++++++ Cargo.toml | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) 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." From 35f554be4677b0aac0a30bfc39024e558c9486fd Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Sun, 9 Jul 2023 19:56:34 +0100 Subject: [PATCH 4/6] Fix #146: Add tests for with_labels constructor Signed-off-by: Adrian Pop --- src/registry.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 48921ed3..6af0a064 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -542,9 +542,14 @@ mod tests { #[test] fn constructors() { - let prefix = "test_prefix"; 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); @@ -557,10 +562,19 @@ mod tests { .next() ); - let labels = vec![ - (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_1")), - (Cow::Borrowed("global_label_1"), Cow::Borrowed("value_2")), - ]; + // 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); From 76e49039a6e6ea61db7170f606936b8b33c4e6da Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 10 Jul 2023 07:16:51 +0200 Subject: [PATCH 5/6] Update src/registry.rs Signed-off-by: Max Inden --- src/registry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registry.rs b/src/registry.rs index 6af0a064..3afac7e1 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -562,7 +562,7 @@ mod tests { .next() ); - // test with_labels constructor + // 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); From e1eb3e54758d3b0f46afc01b58800a17a390dfdd Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 10 Jul 2023 07:17:01 +0200 Subject: [PATCH 6/6] Update src/registry.rs Signed-off-by: Max Inden --- src/registry.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/registry.rs b/src/registry.rs index 3afac7e1..eae07a95 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -548,7 +548,6 @@ mod tests { (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();