Skip to content

Commit

Permalink
src/macros: Have labels! accept any type that implements Display
Browse files Browse the repository at this point in the history
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 <alin.sinpalean@gmail.com>
  • Loading branch information
alin-at-dfinity committed Oct 3, 2020
1 parent b7be575 commit af94a02
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/macros.rs
Expand Up @@ -8,15 +8,17 @@
/// # #[macro_use] extern crate prometheus;
/// # use std::collections::HashMap;
/// # fn main() {
/// let path = String::from("/metrics");
/// let status = 200;
/// let labels = labels!{
/// "test" => "hello",
/// "foo" => "bar",
/// "path" => path,
/// "status" => status,
/// };
/// assert_eq!(labels.len(), 2);
/// assert!(labels.get("test").is_some());
/// assert_eq!(*(labels.get("test").unwrap()), "hello");
/// assert_eq!(*(labels.get("path").unwrap()), "/metrics");
/// assert_eq!(*(labels.get("status").unwrap()), "200");
///
/// let labels: HashMap<&str, &str> = labels!{};
/// let labels = labels!{};
/// assert!(labels.is_empty());
/// # }
/// ```
Expand All @@ -26,9 +28,9 @@ macro_rules! labels {
{
use std::collections::HashMap;

let mut lbs = HashMap::new();
let mut lbs = HashMap::<String, String>::new();
$(
lbs.insert($KEY, $VALUE);
lbs.insert($KEY.to_string(), $VALUE.to_string());
)*

lbs
Expand All @@ -55,10 +57,11 @@ macro_rules! labels {
/// assert!(opts.const_labels.get("foo").is_some());
/// assert_eq!(opts.const_labels.get("foo").unwrap(), "bar");
///
/// let opts = opts!(name,
/// help,
/// labels!{"test" => "hello", "foo" => "bar",},
/// labels!{"ans" => "42",});
/// let opts = opts!(
/// name,
/// help,
/// labels!{"test" => "hello", "foo" => "bar"},
/// labels!{"ans" => 42});
/// assert_eq!(opts.const_labels.len(), 3);
/// assert!(opts.const_labels.get("ans").is_some());
/// assert_eq!(opts.const_labels.get("ans").unwrap(), "42");
Expand All @@ -74,7 +77,7 @@ macro_rules! opts {
let lbs = HashMap::<String, String>::new();
$(
let mut lbs = lbs;
lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into())));
lbs.extend($CONST_LABELS.into_iter().map(|(k, v)| (k.to_string(), v.to_string())));
)*

opts.const_labels(lbs)
Expand Down Expand Up @@ -102,15 +105,16 @@ macro_rules! opts {
/// assert_eq!(opts.common_opts.help, help);
/// assert_eq!(opts.buckets.len(), 4);
///
/// let opts = histogram_opts!(name,
/// help,
/// vec![1.0, 2.0],
/// labels!{"key".to_string() => "value".to_string(),});
/// let opts = histogram_opts!(
/// name,
/// help,
/// vec![1.0, 2.0],
/// labels!{"key" => "value", "status" => 200});
/// assert_eq!(opts.common_opts.name, name);
/// assert_eq!(opts.common_opts.help, help);
/// assert_eq!(opts.buckets.len(), 2);
/// assert!(opts.common_opts.const_labels.get("key").is_some());
/// assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value");
/// assert_eq!(opts.common_opts.const_labels.get("status").unwrap(), "200");
/// # }
/// ```
#[macro_export(local_inner_macros)]
Expand Down

0 comments on commit af94a02

Please sign in to comment.