From 05ef03b39ddf55d01ea6c8cee1b36f9694a2103a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 5 Mar 2026 10:22:57 -0500 Subject: [PATCH] Span.record_exception must also set the span status to error --- src/core/span.ml | 8 ++++++-- src/core/span.mli | 2 ++ src/lib/trace_provider.ml | 7 +------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/core/span.ml b/src/core/span.ml index be88e1ec..21b8de84 100644 --- a/src/core/span.ml +++ b/src/core/span.ml @@ -103,17 +103,21 @@ let add_event' self ev : unit = let record_exception (self : t) (exn : exn) (bt : Printexc.raw_backtrace) : unit = if is_not_dummy self then ( + let exn_msg = Printexc.to_string exn in let ev = Event.make "exception" ~attrs: [ - "exception.message", `String (Printexc.to_string exn); + "exception.message", `String exn_msg; "exception.type", `String (Printexc.exn_slot_name exn); ( "exception.stacktrace", `String (Printexc.raw_backtrace_to_string bt) ); ] in - add_event self ev + add_event self ev; + + let status = make_status ~code:Status_code_error ~message:exn_msg () in + span_set_status self status ) let add_attrs (self : t) (attrs : Key_value.t list) : unit = diff --git a/src/core/span.mli b/src/core/span.mli index e3160930..14a617de 100644 --- a/src/core/span.mli +++ b/src/core/span.mli @@ -94,6 +94,8 @@ val add_event' : t -> (unit -> Event.t) -> unit it if there is an instrumentation backend. *) val record_exception : t -> exn -> Printexc.raw_backtrace -> unit +(** Record an exception occurring inside the span. This creates a span event + {b and} also sets the span status to error. *) val add_links : t -> Span_link.t list -> unit diff --git a/src/lib/trace_provider.ml b/src/lib/trace_provider.ml index 479cd447..84641c30 100644 --- a/src/lib/trace_provider.ml +++ b/src/lib/trace_provider.ml @@ -94,12 +94,7 @@ let with_thunk_and_finally (self : Tracer.t) ?(force_new_trace_id = false) | None -> (match res with | Ok () -> () - | Error (e, bt) -> - Span.record_exception span e bt; - let status = - make_status ~code:Status_code_error ~message:(Printexc.to_string e) () - in - Span.set_status span status)); + | Error (e, bt) -> Span.record_exception span e bt)); Emitter.emit self.emit [ span ] in