From af600455a826b39127261c35aba61761d1b512c1 Mon Sep 17 00:00:00 2001 From: Marc Delagrammatikas Date: Fri, 8 Mar 2024 17:53:19 -0800 Subject: [PATCH] Clean up after ourselves instead of right before setting the status I didn't realize `Span.end_span` does _not_ change the current span, which is why when the parent catches the reraise it had the wrong span as the current span. We're starting the span in a place where we can hold on to it so we can manually update the current span to the parent so that callers that _aren't_ using the decorator have the correct current span. --- lib/open_telemetry_decorator.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/open_telemetry_decorator.ex b/lib/open_telemetry_decorator.ex index c12fab0..8c9eb4b 100644 --- a/lib/open_telemetry_decorator.ex +++ b/lib/open_telemetry_decorator.ex @@ -54,6 +54,7 @@ defmodule OpenTelemetryDecorator do |> Map.take(unquote(dynamic_links)) |> Map.values() + parent_span = Tracer.current_span_ctx() span = Tracer.start_span(unquote(span_name), links: links) Tracer.set_current_span(span) @@ -80,12 +81,12 @@ defmodule OpenTelemetryDecorator do result rescue e -> - Tracer.set_current_span(span) Tracer.record_exception(e) Tracer.set_status(OpenTelemetry.status(:error)) reraise e, __STACKTRACE__ after Span.end_span(span) + Tracer.set_current_span(parent_span) end end rescue