Add span scope names array to capture filter data; optimize values visitor vec.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-02 22:59:14 +00:00
parent 0c6bbde25f
commit 15184d1a79
2 changed files with 23 additions and 9 deletions

View file

@ -7,7 +7,8 @@ pub struct Data<'a> {
pub layer: &'a Layer, pub layer: &'a Layer,
pub event: &'a Event<'a>, pub event: &'a Event<'a>,
pub current: &'a Current, pub current: &'a Current,
pub values: Option<&'a mut [Value]>, pub values: &'a [Value],
pub scope: &'a [&'static str],
} }
impl Data<'_> { impl Data<'_> {
@ -23,8 +24,6 @@ impl Data<'_> {
#[must_use] #[must_use]
pub fn message(&self) -> &str { pub fn message(&self) -> &str {
self.values self.values
.as_ref()
.expect("values are not composed for a filter")
.iter() .iter()
.find(|(k, _)| *k == "message") .find(|(k, _)| *k == "message")
.map_or("", |(_, v)| v.as_str()) .map_or("", |(_, v)| v.as_str())

View file

@ -1,21 +1,25 @@
use std::{fmt, sync::Arc}; use std::{fmt, sync::Arc};
use arrayvec::ArrayVec;
use tracing::field::{Field, Visit}; use tracing::field::{Field, Visit};
use tracing_core::{Event, Subscriber}; use tracing_core::{Event, Subscriber};
use tracing_subscriber::{layer::Context, registry::LookupSpan}; use tracing_subscriber::{layer::Context, registry::LookupSpan};
use super::{Capture, Data, State}; use super::{Capture, Data, State};
pub type Value = (&'static str, String);
pub struct Layer { pub struct Layer {
state: Arc<State>, state: Arc<State>,
} }
struct Visitor { struct Visitor {
values: Vec<Value>, values: Values,
} }
type Values = ArrayVec<Value, 32>;
pub type Value = (&'static str, String);
type ScopeNames = ArrayVec<&'static str, 32>;
impl Layer { impl Layer {
#[inline] #[inline]
pub fn new(state: &Arc<State>) -> Self { pub fn new(state: &Arc<State>) -> Self {
@ -51,8 +55,9 @@ fn handle<S>(layer: &Layer, capture: &Capture, event: &Event<'_>, ctx: &Context<
where where
S: Subscriber + for<'a> LookupSpan<'a>, S: Subscriber + for<'a> LookupSpan<'a>,
{ {
let names = ScopeNames::new();
let mut visitor = Visitor { let mut visitor = Visitor {
values: Vec::new(), values: Values::new(),
}; };
event.record(&mut visitor); event.record(&mut visitor);
@ -61,7 +66,8 @@ where
layer, layer,
event, event,
current: &ctx.current_span(), current: &ctx.current_span(),
values: Some(&mut visitor.values), values: &visitor.values,
scope: &names,
}); });
} }
@ -69,12 +75,21 @@ fn filter<S>(layer: &Layer, capture: &Capture, event: &Event<'_>, ctx: &Context<
where where
S: Subscriber + for<'a> LookupSpan<'a>, S: Subscriber + for<'a> LookupSpan<'a>,
{ {
let values = Values::new();
let mut names = ScopeNames::new();
if let Some(scope) = ctx.event_scope(event) {
for span in scope {
names.push(span.name());
}
}
capture.filter.as_ref().map_or(true, |filter| { capture.filter.as_ref().map_or(true, |filter| {
filter(Data { filter(Data {
layer, layer,
event, event,
current: &ctx.current_span(), current: &ctx.current_span(),
values: None, values: &values,
scope: &names,
}) })
}) })
} }