1//! Label filtering.
23use std::collections::HashSet;
45use metrics::{KeyName, Label};
67/// [`LabelFilter`] trait encapsulates the ability to filter labels, i.e.
8/// determining whether a particular span field should be included as a label or not.
9pub trait LabelFilter {
10/// Returns `true` if the passed `label` of the metric named `name` should
11 /// be included in the key.
12fn should_include_label(&self, name: &KeyName, label: &Label) -> bool;
13}
1415/// A [`LabelFilter`] that allows all labels.
16#[derive(Debug, Copy, Clone, Eq, PartialEq)]
17pub struct IncludeAll;
1819impl LabelFilter for IncludeAll {
20fn should_include_label(&self, _name: &KeyName, _label: &Label) -> bool {
21true
22}
23}
2425/// A [`LabelFilter`] that only allows labels contained in a predefined list.
26#[derive(Debug, Clone)]
27pub struct Allowlist {
28/// The set of allowed label names.
29label_names: HashSet<String>,
30}
3132impl Allowlist {
33/// Create a [`Allowlist`] filter with the provided label names.
34pub fn new<I, S>(allowed: I) -> Allowlist
35where
36I: IntoIterator<Item = S>,
37 S: AsRef<str>,
38 {
39Self { label_names: allowed.into_iter().map(|s| s.as_ref().to_string()).collect() }
40 }
41}
4243impl LabelFilter for Allowlist {
44fn should_include_label(&self, _name: &KeyName, label: &Label) -> bool {
45self.label_names.contains(label.key())
46 }
47}