Skip to content

Commit 97d8de8

Browse files
authored
feat(registry): add with_prefix_and_labels constructor (prometheus#147)
Signed-off-by: Adrian Pop <[email protected]>
1 parent 6c3c2f0 commit 97d8de8

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.21.2]
8+
9+
### Added
10+
11+
- Added `sub_registry_with_labels` method to `Registry`.
12+
See [PR 145].
13+
- Added `with_labels` and `with_prefix_and_labels` constructors to `Registry`.
14+
See [PR 147].
15+
16+
[PR 145]: https://github.com/prometheus/client_rust/pull/145
17+
[PR 147]: https://github.com/prometheus/client_rust/pull/147
18+
719
## [0.21.1]
820

921
### Added

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus-client"
3-
version = "0.21.1"
3+
version = "0.21.2"
44
authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

src/registry.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ impl Registry {
7575
}
7676
}
7777

78+
/// Creates a new default [`Registry`] with the given labels.
79+
pub fn with_labels(
80+
labels: impl Iterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
81+
) -> Self {
82+
Self {
83+
labels: labels.into_iter().collect(),
84+
..Default::default()
85+
}
86+
}
87+
88+
/// Creates a new default [`Registry`] with the given prefix and labels.
89+
pub fn with_prefix_and_labels(
90+
prefix: impl Into<String>,
91+
labels: impl Iterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
92+
) -> Self {
93+
Self {
94+
prefix: Some(Prefix(prefix.into())),
95+
labels: labels.into_iter().collect(),
96+
..Default::default()
97+
}
98+
}
99+
78100
/// Register a metric with the [`Registry`].
79101
///
80102
/// Note: In the Open Metrics text exposition format some metric types have
@@ -527,6 +549,53 @@ mod tests {
527549
use super::*;
528550
use crate::metrics::counter::Counter;
529551

552+
#[test]
553+
fn constructors() {
554+
let counter_name = "test_counter";
555+
let prefix = "test_prefix";
556+
let labels = vec![
557+
(Cow::Borrowed("global_label_1"), Cow::Borrowed("value_1")),
558+
(Cow::Borrowed("global_label_1"), Cow::Borrowed("value_2")),
559+
];
560+
// test with_prefix constructor
561+
let mut registry = Registry::with_prefix(prefix);
562+
let counter: Counter = Counter::default();
563+
registry.register(counter_name, "some help", counter);
564+
565+
assert_eq!(
566+
Some((prefix.to_string() + "_" + counter_name, vec![])),
567+
registry
568+
.iter_metrics()
569+
.map(|(desc, _)| (desc.name.clone(), desc.labels.clone()))
570+
.next()
571+
);
572+
573+
// test with_labels constructor
574+
let mut registry = Registry::with_labels(labels.clone().into_iter());
575+
let counter: Counter = Counter::default();
576+
registry.register(counter_name, "some help", counter);
577+
assert_eq!(
578+
Some((counter_name.to_string(), labels.clone())),
579+
registry
580+
.iter_metrics()
581+
.map(|(desc, _)| (desc.name.clone(), desc.labels.clone()))
582+
.next()
583+
);
584+
585+
// test with_prefix_and_labels constructor
586+
let mut registry = Registry::with_prefix_and_labels(prefix, labels.clone().into_iter());
587+
let counter: Counter = Counter::default();
588+
registry.register(counter_name, "some help", counter);
589+
590+
assert_eq!(
591+
Some((prefix.to_string() + "_" + counter_name, labels)),
592+
registry
593+
.iter_metrics()
594+
.map(|(desc, _)| (desc.name.clone(), desc.labels.clone()))
595+
.next()
596+
);
597+
}
598+
530599
#[test]
531600
fn register_and_iterate() {
532601
let mut registry = Registry::default();

0 commit comments

Comments
 (0)