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(registry): add sub_registry_with_labels #145

Merged
merged 1 commit into from Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/registry.rs
Expand Up @@ -239,11 +239,20 @@ impl Registry {
&mut self,
label: (Cow<'static, str>, Cow<'static, str>),
) -> &mut Self {
let mut labels = self.labels.clone();
labels.push(label);
self.sub_registry_with_labels(std::iter::once(label))
}

/// Like [`Registry::sub_registry_with_prefix`] but with multiple labels instead.
pub fn sub_registry_with_labels(
&mut self,
labels: impl Iterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
) -> &mut Self {
let mut new_labels = self.labels.clone();
new_labels.extend(labels);

let sub_registry = Registry {
prefix: self.prefix.clone(),
labels,
labels: new_labels,
..Default::default()
};

Expand Down Expand Up @@ -549,10 +558,19 @@ mod tests {
let sub_sub_registry = sub_registry.sub_registry_with_label(label_1_2.clone());
sub_sub_registry.register(prefix_1_2_metric_name, "some help", counter.clone());

let prefix_1_2_1 = "prefix_1_2_1";
let prefix_1_2_1_metric_name = "my_prefix_1_2_1_metric";
let sub_sub_sub_registry = sub_sub_registry.sub_registry_with_prefix(prefix_1_2_1);
sub_sub_sub_registry.register(prefix_1_2_1_metric_name, "some help", counter.clone());
let labels_1_3 = vec![
(Cow::Borrowed("label_1_3_1"), Cow::Borrowed("value_1_3_1")),
(Cow::Borrowed("label_1_3_2"), Cow::Borrowed("value_1_3_2")),
];
let prefix_1_3_metric_name = "my_prefix_1_3_metric";
let sub_sub_registry =
sub_registry.sub_registry_with_labels(labels_1_3.clone().into_iter());
sub_sub_registry.register(prefix_1_3_metric_name, "some help", counter.clone());

let prefix_1_3_1 = "prefix_1_3_1";
let prefix_1_3_1_metric_name = "my_prefix_1_3_1_metric";
let sub_sub_sub_registry = sub_sub_registry.sub_registry_with_prefix(prefix_1_3_1);
sub_sub_sub_registry.register(prefix_1_3_1_metric_name, "some help", counter.clone());

let prefix_2 = "prefix_2";
let _ = registry.sub_registry_with_prefix(prefix_2);
Expand Down Expand Up @@ -589,8 +607,15 @@ mod tests {
);
assert_eq!(
Some((
prefix_1.to_string() + "_" + prefix_1_2_1 + "_" + prefix_1_2_1_metric_name,
vec![label_1_2]
prefix_1.to_string() + "_" + prefix_1_3_metric_name,
labels_1_3.clone()
)),
metric_iter.next()
);
assert_eq!(
Some((
prefix_1.to_string() + "_" + prefix_1_3_1 + "_" + prefix_1_3_1_metric_name,
labels_1_3.clone()
)),
metric_iter.next()
);
Expand Down