From 6ccf554645bda0577c44a0a1970e754b3fe982e5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 4 Dec 2025 00:24:42 -0500 Subject: [PATCH] feat span: ambient span --- src/core/span.ml | 10 ++++++++++ src/core/span.mli | 7 +++++++ src/core/tracer.ml | 12 ++---------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/core/span.ml b/src/core/span.ml index f0135b40..5b771d58 100644 --- a/src/core/span.ml +++ b/src/core/span.ml @@ -121,3 +121,13 @@ let add_links' (self : t) (links : unit -> Span_link.t list) : unit = let set_status = span_set_status let set_kind = span_set_kind + +let k_context : t Context.key = Context.new_key () + +(** Find current span from ambient-context *) +let get_ambient () : t option = Ambient_context.get k_context + +(** [with_ambient span f] runs [f()] with the current ambient span being set to + [span] *) +let[@inline] with_ambient (span : t) (f : unit -> 'a) : 'a = + Ambient_context.with_key_bound_to k_context span (fun _ -> f ()) diff --git a/src/core/span.mli b/src/core/span.mli index 673654a9..3fcaf541 100644 --- a/src/core/span.mli +++ b/src/core/span.mli @@ -105,3 +105,10 @@ val set_status : t -> Span_status.t -> unit val set_kind : t -> Span_kind.t -> unit (** Set the span's kind. @since 0.11 *) + +val get_ambient : unit -> t option +(** Find current span from ambient-context *) + +val with_ambient : t -> (unit -> 'a) -> 'a +(** [with_ambient span f] runs [f()] with the current ambient span being set to + [span] *) diff --git a/src/core/tracer.ml b/src/core/tracer.ml index f15c6985..b0dfbc2a 100644 --- a/src/core/tracer.ml +++ b/src/core/tracer.ml @@ -55,17 +55,9 @@ let (emit [@deprecated "use an explicit tracer"]) = | None -> () | Some exp -> exp#send_trace spans -(* TODO: remove scope, use span directly *) -type scope = Scope.t = { - trace_id: Trace_id.t; - span_id: Span_id.t; - mutable items: Scope.item_list; -} -[@@deprecated "use Scope.t"] +let (add_event [@deprecated "use Span.add_event"]) = Span.add_event -let (add_event [@deprecated "use Scope.add_event"]) = Scope.add_event - -let (add_attrs [@deprecated "use Scope.add_attrs"]) = Scope.add_attrs +let (add_attrs [@deprecated "use Span.add_attrs"]) = Span.add_attrs let with_' ?(tracer = simple_main_exporter) ?(force_new_trace_id = false) ?trace_state ?(attrs : (string * [< Value.t ]) list = []) ?kind ?trace_id