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 with_prefix_and_labels constructor #147

Merged
merged 7 commits into from Jul 10, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prometheus-client"
version = "0.21.1"
version = "0.21.2"
authors = ["Max Inden <mail@max-inden.de>"]
edition = "2021"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand Down
69 changes: 69 additions & 0 deletions src/registry.rs
Expand Up @@ -75,6 +75,28 @@ impl Registry {
}
}

/// Creates a new default [`Registry`] with the given labels.
pub fn with_labels(
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()
}
}

/// Register a metric with the [`Registry`].
///
/// Note: In the Open Metrics text exposition format some metric types have
Expand Down Expand Up @@ -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();
Expand Down