imandra-ai / ocaml-opentelemetry

Instrumentation for https://opentelemetry.io
http://docs.imandra.ai/ocaml-opentelemetry/
33 stars 7 forks source link

support exceptions #50

Closed c-cube closed 1 month ago

c-cube commented 10 months ago

https://opentelemetry.io/docs/specs/semconv/exceptions/exceptions-spans/

tatchi commented 1 month ago

Would something like below be reasonable ?

let[@inline] record_exception (scope : t) (exn : unit -> exn) : 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.get_backtrace ());
          ]
    in
    scope.events <- ev :: scope.events
  )
c-cube commented 1 month ago

I think this should rather look like:

let record_exception (scope:t) (exn:exn) (bt:Printexc.raw_backtrace) : unit =
  if …

reason is, the exception is never going to be lazy; and afaik bt has to be collected first thing when the exception occurs, or else the stack frame might be clobbered by intermediate computations.