From 0b143af4bb03b9901f562c923e591d1ab364b66b Mon Sep 17 00:00:00 2001 From: koushiro Date: Sun, 10 Nov 2024 22:05:07 +0800 Subject: [PATCH] refactor(registry): improve constructors of Registry **Breaking Changes**: - change `fn with_prefix(prefix: ...) -> Self` => `fn with_prefix(mut self, prefix: ...) -> Self` - change `fn with_labels(labels: ...) -> Self` => `fn with_labels(mut self, labels: ...) -> Self` - remove `fn with_prefix_and_labels(prefix: ..., labels: ...) -> Self` Signed-off-by: koushiro --- examples/hyper.rs | 2 +- src/lib.rs | 6 +++--- src/registry.rs | 48 ++++++++++++----------------------------------- 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/examples/hyper.rs b/examples/hyper.rs index 82ee121b..b859deab 100644 --- a/examples/hyper.rs +++ b/examples/hyper.rs @@ -24,7 +24,7 @@ use tokio::{ async fn main() { let request_counter: Counter = Default::default(); - let mut registry = ::with_prefix("tokio_hyper_example"); + let mut registry = Registry::default().with_prefix("tokio_hyper_example"); registry.register( "requests", diff --git a/src/lib.rs b/src/lib.rs index cfff6238..bcafed2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ //! // //! // Note the angle brackets to make sure to use the default (dynamic //! // dispatched boxed metric) for the generic type parameter. -//! let mut registry = ::default(); +//! let mut registry = Registry::default(); //! //! // Define a type representing a metric label set, i.e. a key value pair. //! // @@ -37,13 +37,13 @@ //! method: Method, //! // Or just a plain string. //! path: String, -//! }; +//! } //! //! #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelValue)] //! enum Method { //! GET, //! PUT, -//! }; +//! } //! //! // Create a sample counter metric family utilizing the above custom label //! // type, representing the number of HTTP requests received. diff --git a/src/registry.rs b/src/registry.rs index c7dec3ba..d8810bf2 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -67,34 +67,19 @@ pub struct Registry { } impl Registry { - /// Creates a new default [`Registry`] with the given prefix. - pub fn with_prefix(prefix: impl Into) -> Self { - Self { - prefix: Some(Prefix(prefix.into())), - ..Default::default() - } + /// Sets the prefix of the [`Registry`]. + pub fn with_prefix(mut self, prefix: impl Into) -> Self { + self.prefix = Some(Prefix(prefix.into())); + self } - /// Creates a new default [`Registry`] with the given labels. + /// Sets the labels of the [`Registry`]. pub fn with_labels( + mut self, 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() - } + self.labels = labels.into_iter().collect(); + self } /// Register a metric with the [`Registry`]. @@ -242,13 +227,9 @@ impl Registry { /// See [`Registry::sub_registry_with_label`] for the same functionality, /// but namespacing with a label instead of a metric name prefix. pub fn sub_registry_with_prefix>(&mut self, prefix: P) -> &mut Self { - let sub_registry = Registry { - prefix: Some(Prefix( - self.prefix.clone().map(|p| p.0 + "_").unwrap_or_default() + prefix.as_ref(), - )), - labels: self.labels.clone(), - ..Default::default() - }; + let sub_registry = Registry::default() + .with_prefix(self.prefix.clone().map(|p| p.0 + "_").unwrap_or_default() + prefix.as_ref()) + .with_labels(self.labels.clone().into_iter()); self.priv_sub_registry(sub_registry) } @@ -269,12 +250,7 @@ impl Registry { let mut new_labels = self.labels.clone(); new_labels.extend(labels); - let sub_registry = Registry { - prefix: self.prefix.clone(), - labels: new_labels, - ..Default::default() - }; - + let sub_registry = Registry::default().with_labels(new_labels.into_iter()); self.priv_sub_registry(sub_registry) }