Skip to content

Commit

Permalink
feat(registry): add with_prefix_and_labels constructor (#147)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Pop <[email protected]>
  • Loading branch information
popadi authored Jul 10, 2023
1 parent 6c3c2f0 commit 97d8de8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prometheus-client"
version = "0.21.1"
version = "0.21.2"
authors = ["Max Inden <[email protected]>"]
edition = "2021"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand Down
69 changes: 69 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
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

0 comments on commit 97d8de8

Please sign in to comment.