Skip to content

Commit

Permalink
CP-49134: tracing: do not destroy stacktrace (#6117)
Browse files Browse the repository at this point in the history
Reraise with the original stacktrace, this requires using the raw
backtrace instead of the string one.

I had this change sitting in a branch since around Apr 25 2024, although
the branch itself might need more testing, this is a simple fix that we
should probably get in sooner.
  • Loading branch information
robhoes authored Nov 18, 2024
2 parents 8d63a49 + f0003e6 commit ab66db0
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions ocaml/libs/tracing/tracing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ module Span = struct
| exn, stacktrace -> (
let msg = Printexc.to_string exn in
let exn_type = Printexc.exn_slot_name exn in
let stacktrace = Printexc.raw_backtrace_to_string stacktrace in
let _description =
Some
(Printf.sprintf "Error: %s Type: %s Backtrace: %s" msg exn_type
Expand Down Expand Up @@ -720,10 +721,10 @@ let with_tracing ?(attributes = []) ?(parent = None) ~name f =
ignore @@ Tracer.finish span ;
result
with exn ->
let backtrace = Printexc.get_backtrace () in
let backtrace = Printexc.get_raw_backtrace () in
let error = (exn, backtrace) in
ignore @@ Tracer.finish span ~error ;
raise exn
Printexc.raise_with_backtrace exn backtrace
)
| Error e ->
warn "Failed to start tracing: %s" (Printexc.to_string e) ;
Expand Down
4 changes: 3 additions & 1 deletion ocaml/libs/tracing/tracing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ module Tracer : sig
*)

val finish :
?error:exn * string -> Span.t option -> (Span.t option, exn) result
?error:exn * Printexc.raw_backtrace
-> Span.t option
-> (Span.t option, exn) result

val span_hashtbl_is_empty : unit -> bool

Expand Down
2 changes: 1 addition & 1 deletion ocaml/tests/test_observer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ let test_tracing_exn_backtraces () =
let (_ : int) = test_a () in
()
with e -> (
let stacktrace = Printexc.get_backtrace () in
let stacktrace = Printexc.get_raw_backtrace () in
let x = Tracer.finish ~error:(e, stacktrace) x in
match x with
| Ok (Some span) ->
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ let with_tracing ?originator ~__context name f =
result
with exn ->
let backtrace = Printexc.get_raw_backtrace () in
let error = (exn, Printexc.raw_backtrace_to_string backtrace) in
let error = (exn, backtrace) in
ignore @@ Tracer.finish span ~error ;
Printexc.raise_with_backtrace exn backtrace
)
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ val get_client_ip : t -> string option

val get_user_agent : t -> string option

val complete_tracing : ?error:exn * string -> t -> unit
val complete_tracing : ?error:exn * Printexc.raw_backtrace -> t -> unit

val tracing_of : t -> Tracing.Span.t option

Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/taskHelper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ let cancel ~__context =
cancel_this ~__context ~self

let failed ~__context exn =
let backtrace = Printexc.get_backtrace () in
let backtrace = Printexc.get_raw_backtrace () in
let@ () = finally_complete_tracing ~error:(exn, backtrace) __context in
let code, params = ExnHelper.error_of_exn exn in
let@ self = operate_on_db_task ~__context in
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xenopsd/lib/xenops_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ let with_tracing ~name ~task f =
Xenops_task.set_tracing task parent ;
result
with exn ->
let backtrace = Printexc.get_backtrace () in
let backtrace = Printexc.get_raw_backtrace () in
let error = (exn, backtrace) in
ignore @@ Tracer.finish span ~error ;
raise exn
Expand Down

0 comments on commit ab66db0

Please sign in to comment.