metrics_tracing_context/
label_filter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Label filtering.

use std::collections::HashSet;

use metrics::{KeyName, Label};

/// [`LabelFilter`] trait encapsulates the ability to filter labels, i.e.
/// determining whether a particular span field should be included as a label or not.
pub trait LabelFilter {
    /// Returns `true` if the passed `label` of the metric named `name` should
    /// be included in the key.
    fn should_include_label(&self, name: &KeyName, label: &Label) -> bool;
}

/// A [`LabelFilter`] that allows all labels.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct IncludeAll;

impl LabelFilter for IncludeAll {
    fn should_include_label(&self, _name: &KeyName, _label: &Label) -> bool {
        true
    }
}

/// A [`LabelFilter`] that only allows labels contained in a predefined list.
#[derive(Debug, Clone)]
pub struct Allowlist {
    /// The set of allowed label names.
    label_names: HashSet<String>,
}

impl Allowlist {
    /// Create a [`Allowlist`] filter with the provided label names.
    pub fn new<I, S>(allowed: I) -> Allowlist
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self { label_names: allowed.into_iter().map(|s| s.as_ref().to_string()).collect() }
    }
}

impl LabelFilter for Allowlist {
    fn should_include_label(&self, _name: &KeyName, label: &Label) -> bool {
        self.label_names.contains(label.key())
    }
}