feat(opentelemetry): Trace.with_ now has force_new_trace_id param

this parameter can be used to force the creation of a new context,
independent of surrounding context.
This commit is contained in:
Simon Cruanes 2022-09-19 15:01:16 -04:00
parent 8d361744ec
commit a2e273282c
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -706,20 +706,31 @@ module Trace = struct
(** 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_ ?trace_state ?service_name
let with_ ?(force_new_trace_id = false) ?trace_state ?service_name
?(attrs : (string * [< value ]) list = []) ?kind ?trace_id ?parent ?scope
?links name (f : Scope.t -> 'a) : 'a =
let scope = get_scope ?scope () in
let scope =
if force_new_trace_id then
None
else
get_scope ?scope ()
in
let trace_id =
match trace_id, scope with
| _ when force_new_trace_id -> Trace_id.create ()
| Some trace_id, _ -> trace_id
| None, Some scope -> scope.trace_id
| None, None -> Trace_id.create ()
in
let parent =
match parent, scope with
| _ when force_new_trace_id -> None
| Some span_id, _ -> Some span_id
| None, Some scope -> Some scope.span_id
| None, None -> None