Merge pull request #63 from tatchi/add-record-exception

add record_exception
This commit is contained in:
Simon Cruanes 2024-09-27 10:16:40 -04:00 committed by GitHub
commit 9680d61a36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 8 deletions

View file

@ -776,6 +776,21 @@ module Scope = struct
let[@inline] add_event (scope : t) (ev : unit -> Event.t) : unit =
if Collector.has_backend () then scope.events <- ev () :: scope.events
let[@inline] record_exception (scope : t) (exn : exn)
(bt : Printexc.raw_backtrace) : unit =
if Collector.has_backend () then (
let ev =
Event.make "exception"
~attrs:
[
"message", `String (Printexc.to_string exn);
"type", `String (Printexc.exn_slot_name exn);
"stacktrace", `String (Printexc.raw_backtrace_to_string bt);
]
in
scope.events <- ev :: scope.events
)
(** Add an attr to the scope. It will be aggregated into the span.
Note that this takes a function that produces attributes, and will only
@ -1032,11 +1047,9 @@ module Trace = struct
| Ok () -> default_status ~code:Status_code_ok ()
| Error (e, bt) ->
(* add backtrace *)
add_event scope (fun () ->
Event.make "error"
~attrs:
[ "backtrace", `String (Printexc.raw_backtrace_to_string bt) ]);
default_status ~code:Status_code_error ~message:e ()
Scope.record_exception scope e bt;
default_status ~code:Status_code_error ~message:(Printexc.to_string e)
()
in
let span, _ =
(* TODO: should the attrs passed to with_ go on the Span
@ -1084,7 +1097,7 @@ module Trace = struct
rv
with e ->
let bt = Printexc.get_raw_backtrace () in
finally (Error (Printexc.to_string e, bt));
finally (Error (e, bt));
raise e
end

View file

@ -11,6 +11,10 @@ module GC_metrics = GC_metrics
module Metrics_callbacks = Metrics_callbacks
module Trace_context = Trace_context
external reraise : exn -> 'a = "%reraise"
(** This is equivalent to [Lwt.reraise]. We inline it here so we don't force
to use Lwt's latest version *)
module Trace = struct
include Trace
@ -29,8 +33,8 @@ module Trace = struct
Lwt.return rv
with e ->
let bt = Printexc.get_raw_backtrace () in
let () = finally (Error (Printexc.to_string e, bt)) in
Lwt.fail e
let () = finally (Error (e, bt)) in
reraise e
end
module Metrics = struct