Skip to content

Commit 7e4724d

Browse files
committed
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 <[email protected]>
1 parent 70fcfce commit 7e4724d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

tracing-subscriber/src/filter/subscriber_filters/combinator.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::subscribe::{Context, Filter};
33
use std::{cmp, fmt, marker::PhantomData};
44
use tracing_core::{
55
collect::Interest,
6-
span::{Attributes, Id},
6+
span::{Attributes, Id, Record},
77
LevelFilter, Metadata,
88
};
99

@@ -143,6 +143,12 @@ where
143143
self.b.on_new_span(attrs, id, ctx)
144144
}
145145

146+
#[inline]
147+
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
148+
self.a.on_record(id, values, ctx.clone());
149+
self.b.on_record(id, values, ctx);
150+
}
151+
146152
#[inline]
147153
fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
148154
self.a.on_enter(id, ctx.clone());
@@ -324,6 +330,12 @@ where
324330
self.b.on_new_span(attrs, id, ctx)
325331
}
326332

333+
#[inline]
334+
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
335+
self.a.on_record(id, values, ctx.clone());
336+
self.b.on_record(id, values, ctx);
337+
}
338+
327339
#[inline]
328340
fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
329341
self.a.on_enter(id, ctx.clone());
@@ -414,6 +426,11 @@ where
414426
self.a.on_new_span(attrs, id, ctx);
415427
}
416428

429+
#[inline]
430+
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
431+
self.a.on_record(id, values, ctx.clone());
432+
}
433+
417434
#[inline]
418435
fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
419436
self.a.on_enter(id, ctx);

tracing-subscriber/src/filter/subscriber_filters/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ where
587587

588588
fn on_record(&self, span: &span::Id, values: &span::Record<'_>, cx: Context<'_, C>) {
589589
if let Some(cx) = cx.if_enabled_for(span, self.id()) {
590-
self.subscriber.on_record(span, values, cx)
590+
self.filter.on_record(span, values, cx.clone());
591+
self.subscriber.on_record(span, values, cx);
591592
}
592593
}
593594

tracing-subscriber/src/subscribe/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,16 @@ pub trait Filter<S> {
12421242
let _ = (attrs, id, ctx);
12431243
}
12441244

1245+
/// Notifies this filter that a span with the given `Id` recorded the given
1246+
/// `values`.
1247+
///
1248+
/// By default, this method does nothing. `Filter` implementations that
1249+
/// need to be notified when new spans are created can override this
1250+
/// method.
1251+
fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) {
1252+
let _ = (id, values, ctx);
1253+
}
1254+
12451255
/// Notifies this filter that a span with the given ID was entered.
12461256
///
12471257
/// By default, this method does nothing. `Filter` implementations that

0 commit comments

Comments
 (0)