Format stack report paths from tracing spans

This commit is contained in:
pedrocarlo
2026-04-29 12:13:49 -03:00
parent 988f734d23
commit 7d14deda67
3 changed files with 206 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
macro_rules! trace_stack {
() => {
let _stack = $crate::stack::trace_scope(concat!(module_path!(), "::", line!()));
let _stack = $crate::stack::trace_scope(concat!("line:", line!()));
};
($label:expr $(,)?) => {
let _stack = $crate::stack::trace_scope($label);

View File

@@ -95,13 +95,13 @@ fn trace_guard(args: &TraceStackArgs, function_name: &syn::Ident) -> proc_macro2
let (span_name, guard) = match args {
TraceStackArgs::Inferred => {
let guard = quote! {
crate::stack::trace_stack!(concat!(module_path!(), "::", stringify!(#function_name)));
crate::stack::trace_stack!(stringify!(#function_name));
};
(inferred_span_name, guard)
}
TraceStackArgs::InferredWithDetail(detail) => {
let guard = quote! {
crate::stack::trace_stack!(concat!(module_path!(), "::", stringify!(#function_name)), #detail);
crate::stack::trace_stack!(stringify!(#function_name), #detail);
};
(inferred_span_name, guard)
}
@@ -121,7 +121,7 @@ fn trace_guard(args: &TraceStackArgs, function_name: &syn::Ident) -> proc_macro2
quote! {
#[cfg(feature = "stacker")]
let _stack_span = tracing::debug_span!(target: "turso_stack", #span_name).entered();
let _stack_span = tracing::debug_span!(target: "turso_stack", #span_name, module_path = module_path!()).entered();
#guard
}
}

View File

@@ -8,7 +8,7 @@ use std::{
use anyhow::{Context, Result};
use clap::{Parser, ValueEnum};
use serde::Serialize;
use tracing::{Event, Level, Subscriber, field::Visit};
use tracing::{Event, Id, Level, Subscriber, field::Visit, span::Attributes};
use tracing_subscriber::{
Layer,
layer::{Context as LayerContext, SubscriberExt},
@@ -141,7 +141,18 @@ where
metadata.target() == "turso_stack" && *metadata.level() <= Level::DEBUG
}
fn on_event(&self, event: &Event<'_>, _ctx: LayerContext<'_, S>) {
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: LayerContext<'_, S>) {
let mut visitor = StackSpanVisitor::default();
attrs.record(&mut visitor);
if let Some(span) = ctx.span(id) {
span.extensions_mut().insert(StackSpanFields {
module_path: visitor.module_path,
});
}
}
fn on_event(&self, event: &Event<'_>, ctx: LayerContext<'_, S>) {
if event.metadata().target() != "turso_stack" {
return;
}
@@ -155,6 +166,7 @@ where
let Some(remaining_stack) = visitor.remaining_stack else {
return;
};
let label = scoped_stack_label(&ctx, event, &label);
self.collector
.samples
@@ -169,6 +181,30 @@ where
}
}
#[derive(Debug)]
struct StackSpanFields {
module_path: Option<String>,
}
#[derive(Default)]
struct StackSpanVisitor {
module_path: Option<String>,
}
impl Visit for StackSpanVisitor {
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
if field.name() == "module_path" {
self.module_path = Some(unquote_debug_string(&format!("{value:?}")));
}
}
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
if field.name() == "module_path" {
self.module_path = Some(value.to_string());
}
}
}
#[derive(Default)]
struct StackEventVisitor {
label: Option<String>,
@@ -218,6 +254,107 @@ impl Visit for StackEventVisitor {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct StackPathSegment {
module_path: Option<String>,
name: String,
}
fn scoped_stack_label<S>(ctx: &LayerContext<'_, S>, event: &Event<'_>, label: &str) -> String
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
let event_segment = StackPathSegment {
module_path: None,
name: label.to_string(),
};
let Some(scope) = ctx.event_scope(event) else {
return format_stack_path(&[], &event_segment);
};
let scope_segments = scope
.from_root()
.filter(|span| span.metadata().target() == "turso_stack")
.map(|span| {
let module_path = span
.extensions()
.get::<StackSpanFields>()
.and_then(|fields| fields.module_path.clone());
StackPathSegment {
module_path,
name: span.metadata().name().to_string(),
}
})
.collect::<Vec<_>>();
format_stack_path(&scope_segments, &event_segment)
}
fn format_stack_path(scope_segments: &[StackPathSegment], event: &StackPathSegment) -> String {
let mut path = String::new();
for segment in scope_segments {
append_stack_path_segment(&mut path, segment);
}
if scope_segments
.last()
.is_some_and(|segment| stack_segments_match(segment, event))
{
return path;
}
append_stack_path_segment(&mut path, event);
path
}
fn stack_segments_match(
scope_segment: &StackPathSegment,
event_segment: &StackPathSegment,
) -> bool {
scope_segment.name == event_segment.name
|| event_segment
.name
.strip_prefix(scope_segment.module_path.as_deref().unwrap_or_default())
.is_some_and(|suffix| suffix == format!("::{}", scope_segment.name))
}
fn append_stack_path_segment(path: &mut String, segment: &StackPathSegment) {
let rendered = render_stack_path_segment(
segment.module_path.as_deref(),
&segment.name,
path.is_empty(),
);
if !path.is_empty() && !rendered.is_empty() {
path.push_str("::");
}
path.push_str(&rendered);
}
fn render_stack_path_segment(module_path: Option<&str>, name: &str, is_first: bool) -> String {
if let Some(module_path) = module_path {
if let Some(local_name) = name.strip_prefix(&format!("{module_path}::")) {
return if is_first {
format!("{module_path}::{local_name}")
} else {
local_name.to_string()
};
}
if name.contains("::") {
return name.to_string();
}
if is_first {
format!("{module_path}::{name}")
} else {
name.to_string()
}
} else {
name.to_string()
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let args = Args::parse();
@@ -881,6 +1018,69 @@ mod tests {
));
}
#[test]
fn stack_path_does_not_repeat_same_module_for_nested_spans() {
let scope = vec![
StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "prepare_with_origin".to_string(),
},
StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "compile_cmd".to_string(),
},
];
let event = StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "compile".to_string(),
};
assert_eq!(
format_stack_path(&scope, &event),
"turso_core::connection::prepare_with_origin::compile_cmd::compile"
);
}
#[test]
fn stack_path_does_not_append_event_when_it_matches_current_span() {
let scope = vec![StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "prepare_with_origin".to_string(),
}];
let event = StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "prepare_with_origin".to_string(),
};
assert_eq!(
format_stack_path(&scope, &event),
"turso_core::connection::prepare_with_origin"
);
}
#[test]
fn stack_path_uses_tracing_style_names_when_scope_moves_to_another_module() {
let scope = vec![
StackPathSegment {
module_path: Some("turso_core::connection".to_string()),
name: "prepare_with_origin".to_string(),
},
StackPathSegment {
module_path: Some("turso_core::translate".to_string()),
name: "translate".to_string(),
},
];
let event = StackPathSegment {
module_path: Some("turso_core::translate".to_string()),
name: "dispatch".to_string(),
};
assert_eq!(
format_stack_path(&scope, &event),
"turso_core::connection::prepare_with_origin::translate::dispatch"
);
}
#[test]
fn statement_stack_used_is_peak_delta_not_sum() {
let report = build_statement_report(