From 7e4724d2cd0330b7cd1ce73e48f42d4dfa8d1531 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 --- .../filter/subscriber_filters/combinator.rs | 19 ++++++++++++++++++- .../src/filter/subscriber_filters/mod.rs | 3 ++- tracing-subscriber/src/subscribe/mod.rs | 10 ++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tracing-subscriber/src/filter/subscriber_filters/combinator.rs b/tracing-subscriber/src/filter/subscriber_filters/combinator.rs index 730f1abb2a..92e768d3e4 100644 --- a/tracing-subscriber/src/filter/subscriber_filters/combinator.rs +++ b/tracing-subscriber/src/filter/subscriber_filters/combinator.rs @@ -3,7 +3,7 @@ use crate::subscribe::{Context, Filter}; use std::{cmp, fmt, marker::PhantomData}; use tracing_core::{ collect::Interest, - span::{Attributes, Id}, + span::{Attributes, Id, Record}, 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()); @@ -324,6 +330,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()); @@ -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/subscriber_filters/mod.rs b/tracing-subscriber/src/filter/subscriber_filters/mod.rs index 6b80d59a3d..266b6493a2 100644 --- a/tracing-subscriber/src/filter/subscriber_filters/mod.rs +++ b/tracing-subscriber/src/filter/subscriber_filters/mod.rs @@ -587,7 +587,8 @@ where fn on_record(&self, span: &span::Id, values: &span::Record<'_>, cx: Context<'_, C>) { if let Some(cx) = cx.if_enabled_for(span, self.id()) { - self.subscriber.on_record(span, values, cx) + self.filter.on_record(span, values, cx.clone()); + self.subscriber.on_record(span, values, cx); } } diff --git a/tracing-subscriber/src/subscribe/mod.rs b/tracing-subscriber/src/subscribe/mod.rs index d1dba2a12a..5573e7ec74 100644 --- a/tracing-subscriber/src/subscribe/mod.rs +++ b/tracing-subscriber/src/subscribe/mod.rs @@ -1242,6 +1242,16 @@ pub trait Filter { 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