From a2e273282c088e2606b32b8fdae131b07488865a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 19 Sep 2022 15:01:16 -0400 Subject: [PATCH] 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. --- src/opentelemetry.ml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/opentelemetry.ml b/src/opentelemetry.ml index f79afc2d..d1ec0b64 100644 --- a/src/opentelemetry.ml +++ b/src/opentelemetry.ml @@ -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