From 15184d1a79aa702f1e48529395789d58ecf2e660 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 2 Aug 2024 22:59:14 +0000 Subject: [PATCH] Add span scope names array to capture filter data; optimize values visitor vec. Signed-off-by: Jason Volk --- src/core/log/capture/data.rs | 5 ++--- src/core/log/capture/layer.rs | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/core/log/capture/data.rs b/src/core/log/capture/data.rs index 3f92101d..63f09b6c 100644 --- a/src/core/log/capture/data.rs +++ b/src/core/log/capture/data.rs @@ -7,7 +7,8 @@ pub struct Data<'a> { pub layer: &'a Layer, pub event: &'a Event<'a>, pub current: &'a Current, - pub values: Option<&'a mut [Value]>, + pub values: &'a [Value], + pub scope: &'a [&'static str], } impl Data<'_> { @@ -23,8 +24,6 @@ impl Data<'_> { #[must_use] pub fn message(&self) -> &str { self.values - .as_ref() - .expect("values are not composed for a filter") .iter() .find(|(k, _)| *k == "message") .map_or("", |(_, v)| v.as_str()) diff --git a/src/core/log/capture/layer.rs b/src/core/log/capture/layer.rs index 57f22dc7..674bc379 100644 --- a/src/core/log/capture/layer.rs +++ b/src/core/log/capture/layer.rs @@ -1,21 +1,25 @@ use std::{fmt, sync::Arc}; +use arrayvec::ArrayVec; use tracing::field::{Field, Visit}; use tracing_core::{Event, Subscriber}; use tracing_subscriber::{layer::Context, registry::LookupSpan}; use super::{Capture, Data, State}; -pub type Value = (&'static str, String); - pub struct Layer { state: Arc, } struct Visitor { - values: Vec, + values: Values, } +type Values = ArrayVec; +pub type Value = (&'static str, String); + +type ScopeNames = ArrayVec<&'static str, 32>; + impl Layer { #[inline] pub fn new(state: &Arc) -> Self { @@ -51,8 +55,9 @@ fn handle(layer: &Layer, capture: &Capture, event: &Event<'_>, ctx: &Context< where S: Subscriber + for<'a> LookupSpan<'a>, { + let names = ScopeNames::new(); let mut visitor = Visitor { - values: Vec::new(), + values: Values::new(), }; event.record(&mut visitor); @@ -61,7 +66,8 @@ where layer, event, current: &ctx.current_span(), - values: Some(&mut visitor.values), + values: &visitor.values, + scope: &names, }); } @@ -69,12 +75,21 @@ fn filter(layer: &Layer, capture: &Capture, event: &Event<'_>, ctx: &Context< where 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| { filter(Data { layer, event, current: &ctx.current_span(), - values: None, + values: &values, + scope: &names, }) }) }