From afe9b332157c4c7b037470095095e5568e099719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ml=C3=A1dek?= Date: Wed, 17 Apr 2024 14:08:26 +0200 Subject: [PATCH] fix: set span end time when it exits Setting end time when a span is closed makes it impossible for child spans to live longer than their parents if we want to keep the relationship in `tracing` itself. --- src/layer.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index 83bed1d..43d2193 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -970,13 +970,17 @@ where } fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + + if let Some(otel_data) = extensions.get_mut::() { + otel_data.builder.end_time = Some(crate::time::now()); + } + if !self.tracked_inactivity { return; } - let span = ctx.span(id).expect("Span not found, this is a bug"); - let mut extensions = span.extensions_mut(); - if let Some(timings) = extensions.get_mut::() { let now = Instant::now(); timings.busy += (now - timings.last).as_nanos() as i64; @@ -1165,9 +1169,7 @@ where } // Assign end time, build and start span, drop span to export - builder - .with_end_time(crate::time::now()) - .start_with_context(&self.tracer, &parent_cx); + builder.start_with_context(&self.tracer, &parent_cx); } }