mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-08 20:07:55 -04:00
lwt: Share impl details with non-lwt with_
This commit is contained in:
parent
a64565f104
commit
d668f5c472
2 changed files with 31 additions and 45 deletions
|
|
@ -12,45 +12,21 @@ module Metrics_callbacks = Metrics_callbacks
|
||||||
module Trace_context = Trace_context
|
module Trace_context = Trace_context
|
||||||
|
|
||||||
module Trace = struct
|
module Trace = struct
|
||||||
open Proto.Trace
|
|
||||||
include Trace
|
include Trace
|
||||||
|
|
||||||
(** Sync span guard *)
|
(** Sync span guard *)
|
||||||
let with_ ?trace_state ?service_name ?(attrs = []) ?kind ?trace_id ?parent
|
let with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind
|
||||||
?scope ?links name (f : Scope.t -> 'a Lwt.t) : 'a Lwt.t =
|
?trace_id ?parent ?scope ?links name (cb : Scope.t -> 'a Lwt.t) : 'a Lwt.t
|
||||||
let trace_id =
|
=
|
||||||
match trace_id, scope with
|
let thunk, finally =
|
||||||
| Some trace_id, _ -> trace_id
|
with_' ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind
|
||||||
| None, Some scope -> scope.trace_id
|
?trace_id ?parent ?scope ?links name cb
|
||||||
| None, None -> Trace_id.create ()
|
|
||||||
in
|
|
||||||
let parent =
|
|
||||||
match parent, scope with
|
|
||||||
| Some span_id, _ -> Some span_id
|
|
||||||
| None, Some scope -> Some scope.span_id
|
|
||||||
| None, None -> None
|
|
||||||
in
|
|
||||||
let start_time = Timestamp_ns.now_unix_ns () in
|
|
||||||
let span_id = Span_id.create () in
|
|
||||||
let scope = { trace_id; span_id; events = []; attrs } in
|
|
||||||
let finally ok =
|
|
||||||
let status =
|
|
||||||
match ok with
|
|
||||||
| Ok () -> default_status ~code:Status_code_ok ()
|
|
||||||
| Error e -> default_status ~code:Status_code_error ~message:e ()
|
|
||||||
in
|
|
||||||
let span, _ =
|
|
||||||
Span.create ?kind ~trace_id ?parent ?links ~id:span_id ?trace_state
|
|
||||||
~attrs:scope.attrs ~events:scope.events ~start_time
|
|
||||||
~end_time:(Timestamp_ns.now_unix_ns ())
|
|
||||||
~status name
|
|
||||||
in
|
|
||||||
emit ?service_name [ span ]
|
|
||||||
in
|
in
|
||||||
|
|
||||||
try%lwt
|
try%lwt
|
||||||
let* x = f scope in
|
let* rv = thunk () in
|
||||||
let () = finally (Ok ()) in
|
let () = finally (Ok ()) in
|
||||||
Lwt.return x
|
Lwt.return rv
|
||||||
with e ->
|
with e ->
|
||||||
let () = finally (Error (Printexc.to_string e)) in
|
let () = finally (Error (Printexc.to_string e)) in
|
||||||
Lwt.fail e
|
Lwt.fail e
|
||||||
|
|
|
||||||
|
|
@ -716,17 +716,9 @@ module Trace = struct
|
||||||
|
|
||||||
let add_attrs = Scope.add_attrs [@@deprecated "use Scope.add_attrs"]
|
let add_attrs = Scope.add_attrs [@@deprecated "use Scope.add_attrs"]
|
||||||
|
|
||||||
(** Sync span guard.
|
let with_' ?(force_new_trace_id = false) ?trace_state ?service_name
|
||||||
|
|
||||||
@param force_new_trace_id if true (default false), the span will not use a
|
|
||||||
surrounding context, or [scope], or [trace_id], but will always
|
|
||||||
create a fresh new trace ID.
|
|
||||||
|
|
||||||
{b NOTE} be careful not to call this inside a Gc alarm, as it can
|
|
||||||
cause deadlocks. *)
|
|
||||||
let with_ ?(force_new_trace_id = false) ?trace_state ?service_name
|
|
||||||
?(attrs : (string * [< value ]) list = []) ?kind ?trace_id ?parent ?scope
|
?(attrs : (string * [< value ]) list = []) ?kind ?trace_id ?parent ?scope
|
||||||
?links name (f : Scope.t -> 'a) : 'a =
|
?links name cb =
|
||||||
let scope =
|
let scope =
|
||||||
if force_new_trace_id then
|
if force_new_trace_id then
|
||||||
None
|
None
|
||||||
|
|
@ -770,10 +762,28 @@ module Trace = struct
|
||||||
in
|
in
|
||||||
emit ?service_name [ span ]
|
emit ?service_name [ span ]
|
||||||
in
|
in
|
||||||
|
let thunk () = cb scope in
|
||||||
|
thunk, finally
|
||||||
|
|
||||||
|
(** Sync span guard.
|
||||||
|
|
||||||
|
@param force_new_trace_id if true (default false), the span will not use a
|
||||||
|
surrounding context, or [scope], or [trace_id], but will always
|
||||||
|
create a fresh new trace ID.
|
||||||
|
|
||||||
|
{b NOTE} be careful not to call this inside a Gc alarm, as it can
|
||||||
|
cause deadlocks. *)
|
||||||
|
let with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind
|
||||||
|
?trace_id ?parent ?scope ?links name (cb : Scope.t -> 'a) : 'a =
|
||||||
|
let thunk, finally =
|
||||||
|
with_' ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind
|
||||||
|
?trace_id ?parent ?scope ?links name cb
|
||||||
|
in
|
||||||
|
|
||||||
try
|
try
|
||||||
let x = f scope in
|
let rv = thunk () in
|
||||||
finally (Ok ());
|
finally (Ok ());
|
||||||
x
|
rv
|
||||||
with e ->
|
with e ->
|
||||||
finally (Error (Printexc.to_string e));
|
finally (Error (Printexc.to_string e));
|
||||||
raise e
|
raise e
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue