feat span: ambient span

This commit is contained in:
Simon Cruanes 2025-12-04 00:24:42 -05:00
parent e79df14a90
commit 6ccf554645
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 19 additions and 10 deletions

View file

@ -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 ())

View file

@ -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] *)

View file

@ -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