From 1bb22fe1536c5733e616669c00c930cbca62cfdd Mon Sep 17 00:00:00 2001 From: zohnannor <35764628+zohnannor@users.noreply.github.com> Date: Sat, 21 Jan 2023 05:11:37 +0300 Subject: [PATCH] Add a `KeyName` argument to `LabelFilter::should_include_label` (#342) --- metrics-tracing-context/Cargo.toml | 2 +- metrics-tracing-context/src/label_filter.rs | 11 ++++++----- metrics-tracing-context/src/lib.rs | 4 +++- metrics-tracing-context/tests/integration.rs | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/metrics-tracing-context/Cargo.toml b/metrics-tracing-context/Cargo.toml index d9fbdff3..83ddddcb 100644 --- a/metrics-tracing-context/Cargo.toml +++ b/metrics-tracing-context/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "metrics-tracing-context" -version = "0.12.0" +version = "0.13.0" authors = ["MOZGIII "] edition = "2018" rust-version = "1.56.1" diff --git a/metrics-tracing-context/src/label_filter.rs b/metrics-tracing-context/src/label_filter.rs index c8b44d2e..fa42fc80 100644 --- a/metrics-tracing-context/src/label_filter.rs +++ b/metrics-tracing-context/src/label_filter.rs @@ -2,13 +2,14 @@ use std::collections::HashSet; -use metrics::Label; +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 should be included in the key. - fn should_include_label(&self, label: &Label) -> bool; + /// 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. @@ -16,7 +17,7 @@ pub trait LabelFilter { pub struct IncludeAll; impl LabelFilter for IncludeAll { - fn should_include_label(&self, _label: &Label) -> bool { + fn should_include_label(&self, _name: &KeyName, _label: &Label) -> bool { true } } @@ -40,7 +41,7 @@ impl Allowlist { } impl LabelFilter for Allowlist { - fn should_include_label(&self, label: &Label) -> bool { + fn should_include_label(&self, _name: &KeyName, label: &Label) -> bool { self.label_names.contains(label.key()) } } diff --git a/metrics-tracing-context/src/lib.rs b/metrics-tracing-context/src/lib.rs index 4206213a..b43052b0 100644 --- a/metrics-tracing-context/src/lib.rs +++ b/metrics-tracing-context/src/lib.rs @@ -178,7 +178,9 @@ where let filtered_labels = new_labels .iter() - .filter(|label| self.label_filter.should_include_label(label)) + .filter(|label| { + self.label_filter.should_include_label(&name, label) + }) .cloned(); labels.extend(filtered_labels); diff --git a/metrics-tracing-context/tests/integration.rs b/metrics-tracing-context/tests/integration.rs index ada7ab66..35ec2b18 100644 --- a/metrics-tracing-context/tests/integration.rs +++ b/metrics-tracing-context/tests/integration.rs @@ -1,4 +1,4 @@ -use metrics::{counter, Key, Label}; +use metrics::{counter, Key, KeyName, Label}; use metrics_tracing_context::{LabelFilter, MetricsLayer, TracingContextLayer}; use metrics_util::debugging::{DebugValue, DebuggingRecorder, Snapshotter}; use metrics_util::{layers::Layer, CompositeKey, MetricKind}; @@ -317,7 +317,7 @@ fn test_nested_spans() { struct OnlyUser; impl LabelFilter for OnlyUser { - fn should_include_label(&self, label: &Label) -> bool { + fn should_include_label(&self, _name: &KeyName, label: &Label) -> bool { label.key() == "user" } }