From b720ea0d20cc69e9b26043bbae45d41eb9ce8012 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:08:54 -0400 Subject: [PATCH] fix otel.trace: record exception in `with_span` close #71 --- src/trace/opentelemetry_trace.ml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/trace/opentelemetry_trace.ml b/src/trace/opentelemetry_trace.ml index c71f42a..8e2c6ec 100644 --- a/src/trace/opentelemetry_trace.ml +++ b/src/trace/opentelemetry_trace.ml @@ -200,11 +200,19 @@ module Internal = struct in Otel.Scope.with_ambient_scope sb.scope @@ fun () -> - let rv = cb otrace_id in + match cb otrace_id with + | res -> + let otel_span = exit_span' otrace_id sb in + Otel.Trace.emit [ otel_span ]; + res + | exception e -> + let bt = Printexc.get_raw_backtrace () in + + Otel.Scope.record_exception sb.scope e bt; + let otel_span = exit_span' otrace_id sb in + Otel.Trace.emit [ otel_span ]; - let otel_span = exit_span' otrace_id sb in - Otel.Trace.emit [ otel_span ]; - rv + Printexc.raise_with_backtrace e bt let enter_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name : Trace_core.span =