Skip to content

Commit

Permalink
Add a KeyName argument to LabelFilter::should_include_label (metr…
Browse files Browse the repository at this point in the history
  • Loading branch information
zohnannor authored Jan 21, 2023
1 parent 7903c4f commit fab4004
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion metrics-tracing-context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metrics-tracing-context"
version = "0.12.0"
version = "0.13.0"
authors = ["MOZGIII <mike-n@narod.ru>"]
edition = "2018"
rust-version = "1.56.1"
Expand Down
11 changes: 6 additions & 5 deletions metrics-tracing-context/src/label_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
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.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
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
}
}
Expand All @@ -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())
}
}
4 changes: 3 additions & 1 deletion metrics-tracing-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions metrics-tracing-context/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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"
}
}
Expand Down

0 comments on commit fab4004

Please sign in to comment.