Skip to content

Commit

Permalink
refactor(registry): improve constructors of Registry
Browse files Browse the repository at this point in the history
**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 <[email protected]>
  • Loading branch information
koushiro committed Nov 10, 2024
1 parent 12923ca commit 0b143af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tokio::{
async fn main() {
let request_counter: Counter<u64> = Default::default();

let mut registry = <Registry>::with_prefix("tokio_hyper_example");
let mut registry = Registry::default().with_prefix("tokio_hyper_example");

registry.register(
"requests",
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Registry>::default();
//! let mut registry = Registry::default();
//!
//! // Define a type representing a metric label set, i.e. a key value pair.
//! //
Expand All @@ -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.
Expand Down
48 changes: 12 additions & 36 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Self {
Self {
prefix: Some(Prefix(prefix.into())),
..Default::default()
}
/// Sets the prefix of the [`Registry`].
pub fn with_prefix(mut self, prefix: impl Into<String>) -> 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<Item = (Cow<'static, str>, 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<String>,
labels: impl Iterator<Item = (Cow<'static, str>, 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`].
Expand Down Expand Up @@ -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<P: AsRef<str>>(&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)
}
Expand All @@ -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)
}

Expand Down

0 comments on commit 0b143af

Please sign in to comment.