From c2470c59a0fd1fb679f1599e47e49dee16d2fa9c Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 24 Mar 2022 10:12:42 -0700 Subject: [PATCH] subscriber: add `Filter::on_record` callback Currently, `Filter` only has the `on_new_span`, `on_enter`, `on_exit`, and `on_close` callbacks. This means it won't handle cases where a span records a new field value that changes its filter state. This branch adds the missing `on_record` callback. Signed-off-by: Eliza Weisman --- .../src/filter/layer_filters/combinator.rs | 21 +++++++++++++++++-- .../src/filter/layer_filters/mod.rs | 1 + tracing-subscriber/src/layer/mod.rs | 11 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tracing-subscriber/src/filter/layer_filters/combinator.rs b/tracing-subscriber/src/filter/layer_filters/combinator.rs index cf93c8c0e0..e79de20870 100644 --- a/tracing-subscriber/src/filter/layer_filters/combinator.rs +++ b/tracing-subscriber/src/filter/layer_filters/combinator.rs @@ -2,7 +2,7 @@ use crate::layer::{Context, Filter}; use std::{cmp, fmt, marker::PhantomData}; use tracing_core::{ - span::{Attributes, Id}, + span::{Attributes, Id, Record}, subscriber::Interest, LevelFilter, Metadata, }; @@ -143,6 +143,12 @@ where self.b.on_new_span(attrs, id, ctx) } + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + self.b.on_record(id, values, ctx); + } + #[inline] fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { self.a.on_enter(id, ctx.clone()); @@ -317,13 +323,19 @@ where // If either hint is `None`, return `None`. Otherwise, return the less restrictive. Some(cmp::max(self.a.max_level_hint()?, self.b.max_level_hint()?)) } - + #[inline] fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { self.a.on_new_span(attrs, id, ctx.clone()); self.b.on_new_span(attrs, id, ctx) } + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + self.b.on_record(id, values, ctx); + } + #[inline] fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { self.a.on_enter(id, ctx.clone()); @@ -414,6 +426,11 @@ where self.a.on_new_span(attrs, id, ctx); } + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + } + #[inline] fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { self.a.on_enter(id, ctx); diff --git a/tracing-subscriber/src/filter/layer_filters/mod.rs b/tracing-subscriber/src/filter/layer_filters/mod.rs index c77ad165db..b71b011c2e 100644 --- a/tracing-subscriber/src/filter/layer_filters/mod.rs +++ b/tracing-subscriber/src/filter/layer_filters/mod.rs @@ -592,6 +592,7 @@ where fn on_record(&self, span: &span::Id, values: &span::Record<'_>, cx: Context<'_, S>) { if let Some(cx) = cx.if_enabled_for(span, self.id()) { + self.filter.on_record(span, values, cx.clone()); self.layer.on_record(span, values, cx) } } diff --git a/tracing-subscriber/src/layer/mod.rs b/tracing-subscriber/src/layer/mod.rs index 8255aef486..500a3d9dc6 100644 --- a/tracing-subscriber/src/layer/mod.rs +++ b/tracing-subscriber/src/layer/mod.rs @@ -1065,6 +1065,17 @@ feature! { let _ = (attrs, id, ctx); } + + /// Notifies this filter that a span with the given `Id` recorded the given + /// `values`. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when new spans are created can override this + /// method. + fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + let _ = (id, values, ctx); + } + /// Notifies this filter that a span with the given ID was entered. /// /// By default, this method does nothing. `Filter` implementations that