helpers to emit in *_provider

This commit is contained in:
Simon Cruanes 2026-03-03 15:14:32 -05:00
parent 14e892454c
commit fa14ddf1f8
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 15 additions and 5 deletions

View file

@ -42,6 +42,10 @@ let get_logger ?name ?version ?(attrs : (string * [< Value.t ]) list = [])
(** A Logger.t that lazily reads the global at emit time *) (** A Logger.t that lazily reads the global at emit time *)
let default_logger : Logger.t = get_logger () let default_logger : Logger.t = get_logger ()
(** Emit log with current logger *)
let[@inline] emit (log : Log_record.t) : unit =
Emitter.emit (get ()).emit [ log ]
open Log_record open Log_record
(** Create log record and emit it on [logger] *) (** Create log record and emit it on [logger] *)

View file

@ -39,6 +39,9 @@ let get_meter ?name ?version ?(attrs : (string * [< Value.t ]) list = [])
clock = { Clock.now = (fun () -> Clock.now (Clock.Main.get ())) }; clock = { Clock.now = (fun () -> Clock.now (Clock.Main.get ())) };
} }
(** Emit with current meter *)
let[@inline] emit (m : Metrics.t) : unit = Emitter.emit (get ()).emit [ m ]
(** A Meter.t that lazily reads the global at emit time *) (** A Meter.t that lazily reads the global at emit time *)
let default_meter : Meter.t = get_meter () let default_meter : Meter.t = get_meter ()

View file

@ -41,16 +41,15 @@ let[@inline] active () : Aswitch.t =
| None -> Aswitch.dummy | None -> Aswitch.dummy
| Some exp -> Exporter.active exp | Some exp -> Exporter.active exp
let add_on_tick_callback (f : unit -> unit) : unit = let add_on_tick_callback : (unit -> unit) -> unit = Globals.add_on_tick_callback
Globals.add_on_tick_callback f
let run_tick_callbacks () : unit = Globals.run_tick_callbacks () let run_tick_callbacks : unit -> unit = Globals.run_tick_callbacks
(** Tick all providers and run all registered callbacks. Call this periodically (** Tick all providers and run all registered callbacks. Call this periodically
(e.g. every 500ms) to drive metrics collection, GC metrics, and batch (e.g. every 500ms) to drive metrics collection, GC metrics, and batch
timeout flushing. This is the single function client libraries should call timeout flushing. This is the single function client libraries should call
from their ticker. *) from their ticker. *)
let tick () : unit = Globals.run_tick_callbacks () let tick : unit -> unit = Globals.run_tick_callbacks
let set ?batch_traces ?batch_metrics ?batch_logs let set ?batch_traces ?batch_metrics ?batch_logs
?(batch_timeout = Mtime.Span.(2_000 * ms)) (exp : Exporter.t) : unit = ?(batch_timeout = Mtime.Span.(2_000 * ms)) (exp : Exporter.t) : unit =

View file

@ -5,10 +5,14 @@ open struct
let provider_ : Tracer.t Atomic.t = Atomic.make Tracer.dummy let provider_ : Tracer.t Atomic.t = Atomic.make Tracer.dummy
end end
(** Get current tracer. *)
let get () : Tracer.t = Atomic.get provider_ let get () : Tracer.t = Atomic.get provider_
(** Set current tracer *)
let set (t : Tracer.t) : unit = Atomic.set provider_ t let set (t : Tracer.t) : unit = Atomic.set provider_ t
(** Replace current tracer by the dummy one. All spans will be discarded from
now on. *)
let clear () : unit = Atomic.set provider_ Tracer.dummy let clear () : unit = Atomic.set provider_ Tracer.dummy
(** Get a tracer pre-configured with a fixed set of attributes added to every (** Get a tracer pre-configured with a fixed set of attributes added to every
@ -44,7 +48,7 @@ let get_tracer ?name ?version ?(attrs : (string * [< Value.t ]) list = [])
let default_tracer : Tracer.t = get_tracer () let default_tracer : Tracer.t = get_tracer ()
(** Emit a span directly via the current global tracer *) (** Emit a span directly via the current global tracer *)
let emit (span : Span.t) : unit = Emitter.emit default_tracer.emit [ span ] let[@inline] emit (span : Span.t) : unit = Emitter.emit (get ()).emit [ span ]
(** Helper to implement {!with_} and similar functions *) (** Helper to implement {!with_} and similar functions *)
let with_thunk_and_finally (self : Tracer.t) ?(force_new_trace_id = false) let with_thunk_and_finally (self : Tracer.t) ?(force_new_trace_id = false)