Skip to content

Commit a722af4

Browse files
committed
src/macros: Have labels! accept any type that implements Display
This allows (among other things) `histogram_opts!` to take the same kinds of types as `opts!`. E.g. to replace this: ```rust let status = String::from("200"); histogram_opts!( ... labels! {"status".to_string() => status.clone()} ) // Do more stuff with `status`. ``` with this: ```rust let status = String::from("200"); histogram_opts!( ... labels! {"status" => &status} ) // Do more stuff with `status`. ``` And a lot more, such as using numbers or enums as label values directly. Signed-off-by: Alin Sinpalean <[email protected]>
1 parent b7be575 commit a722af4

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/macros.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
/// # #[macro_use] extern crate prometheus;
99
/// # use std::collections::HashMap;
1010
/// # fn main() {
11+
/// let path = String::from("/metrics");
12+
/// let status = 200;
1113
/// let labels = labels!{
12-
/// "test" => "hello",
13-
/// "foo" => "bar",
14+
/// "path" => path,
15+
/// "status" => status,
1416
/// };
1517
/// assert_eq!(labels.len(), 2);
16-
/// assert!(labels.get("test").is_some());
17-
/// assert_eq!(*(labels.get("test").unwrap()), "hello");
18+
/// assert_eq!(*(labels.get("path").unwrap()), "/metrics");
19+
/// assert_eq!(*(labels.get("status").unwrap()), "200");
1820
///
19-
/// let labels: HashMap<&str, &str> = labels!{};
21+
/// let labels = labels!{};
2022
/// assert!(labels.is_empty());
2123
/// # }
2224
/// ```
@@ -26,9 +28,9 @@ macro_rules! labels {
2628
{
2729
use std::collections::HashMap;
2830

29-
let mut lbs = HashMap::new();
31+
let mut lbs = HashMap::<String, String>::new();
3032
$(
31-
lbs.insert($KEY, $VALUE);
33+
lbs.insert($KEY.to_string(), $VALUE.to_string());
3234
)*
3335

3436
lbs
@@ -55,10 +57,11 @@ macro_rules! labels {
5557
/// assert!(opts.const_labels.get("foo").is_some());
5658
/// assert_eq!(opts.const_labels.get("foo").unwrap(), "bar");
5759
///
58-
/// let opts = opts!(name,
59-
/// help,
60-
/// labels!{"test" => "hello", "foo" => "bar",},
61-
/// labels!{"ans" => "42",});
60+
/// let opts = opts!(
61+
/// name,
62+
/// help,
63+
/// labels!{"test" => "hello", "foo" => "bar"},
64+
/// labels!{"ans" => 42});
6265
/// assert_eq!(opts.const_labels.len(), 3);
6366
/// assert!(opts.const_labels.get("ans").is_some());
6467
/// assert_eq!(opts.const_labels.get("ans").unwrap(), "42");
@@ -74,7 +77,7 @@ macro_rules! opts {
7477
let lbs = HashMap::<String, String>::new();
7578
$(
7679
let mut lbs = lbs;
77-
lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into())));
80+
lbs.extend($CONST_LABELS.into_iter().map(|(k, v)| (k.to_string(), v.to_string())));
7881
)*
7982

8083
opts.const_labels(lbs)
@@ -102,15 +105,16 @@ macro_rules! opts {
102105
/// assert_eq!(opts.common_opts.help, help);
103106
/// assert_eq!(opts.buckets.len(), 4);
104107
///
105-
/// let opts = histogram_opts!(name,
106-
/// help,
107-
/// vec![1.0, 2.0],
108-
/// labels!{"key".to_string() => "value".to_string(),});
108+
/// let opts = histogram_opts!(
109+
/// name,
110+
/// help,
111+
/// vec![1.0, 2.0],
112+
/// labels!{"key" => "value", "status" => 200});
109113
/// assert_eq!(opts.common_opts.name, name);
110114
/// assert_eq!(opts.common_opts.help, help);
111115
/// assert_eq!(opts.buckets.len(), 2);
112-
/// assert!(opts.common_opts.const_labels.get("key").is_some());
113116
/// assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value");
117+
/// assert_eq!(opts.common_opts.const_labels.get("status").unwrap(), "200");
114118
/// # }
115119
/// ```
116120
#[macro_export(local_inner_macros)]

0 commit comments

Comments
 (0)