use Aswitch in main library

This commit is contained in:
Simon Cruanes 2025-12-08 15:36:04 -05:00
parent 18b653a896
commit 3026ad41ad
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 37 additions and 15 deletions

View file

@ -13,13 +13,16 @@ open struct
end end
(** Remove current exporter, if any. (** Remove current exporter, if any.
@param on_done see {!Main_exporter.shutdown}, @since 0.12 *) @param on_done
called when this operation is done, including shutting down the exporter,
if any *)
let remove ~on_done () : unit = let remove ~on_done () : unit =
match Atomic.exchange exporter None with match Atomic.exchange exporter None with
| None -> on_done () | None -> on_done ()
| Some exp -> | Some exp ->
Aswitch.on_turn_off (Exporter.active exp) on_done;
tick exp; tick exp;
shutdown exp ~on_done shutdown exp
(** Is there a configured exporter? *) (** Is there a configured exporter? *)
let present () : bool = Option.is_some (Atomic.get exporter) let present () : bool = Option.is_some (Atomic.get exporter)
@ -56,22 +59,28 @@ module Util = struct
{ Emitter.enabled; closed; emit; tick; flush_and_close } { Emitter.enabled; closed; emit; tick; flush_and_close }
end end
(** Aswitch of the current exporter, or {!Aswitch.dummy} *)
let[@inline] active () : Aswitch.t =
match get () with
| None -> Aswitch.dummy
| Some e -> e.active ()
(** This exporter uses the current "main exporter" using [get()] at every (** This exporter uses the current "main exporter" using [get()] at every
invocation. It is useful as a fallback or to port existing applications that invocation. It is useful as a fallback or to port existing applications that
expect a global singleton backend^W exporter. expect a global singleton backend^W exporter.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
let dynamic_forward_to_main_exporter : Exporter.t = let dynamic_forward_to_main_exporter : Exporter.t =
let open Exporter in
let emit_logs = let emit_logs =
Util.dynamic_forward_to_main_exporter () ~get_emitter:(fun e -> e.emit_logs) Util.dynamic_forward_to_main_exporter ()
~get_emitter:Exporter.(fun e -> e.emit_logs)
in in
let emit_metrics = let emit_metrics =
Util.dynamic_forward_to_main_exporter () ~get_emitter:(fun e -> Util.dynamic_forward_to_main_exporter ()
e.emit_metrics) ~get_emitter:Exporter.(fun e -> e.emit_metrics)
in in
let emit_spans = let emit_spans =
Util.dynamic_forward_to_main_exporter () ~get_emitter:(fun e -> Util.dynamic_forward_to_main_exporter ()
e.emit_spans) ~get_emitter:Exporter.(fun e -> e.emit_spans)
in in
let on_tick f = let on_tick f =
match get () with match get () with
@ -83,8 +92,16 @@ let dynamic_forward_to_main_exporter : Exporter.t =
| None -> () | None -> ()
| Some exp -> exp.tick () | Some exp -> exp.tick ()
in in
let shutdown ~on_done () = on_done () in let shutdown () = () in
{ Exporter.emit_metrics; emit_spans; emit_logs; on_tick; tick; shutdown } {
Exporter.active;
emit_metrics;
emit_spans;
emit_logs;
on_tick;
tick;
shutdown;
}
(** Set the global exporter *) (** Set the global exporter *)
let set (exp : t) : unit = let set (exp : t) : unit =
@ -110,6 +127,7 @@ let with_setup_debug_backend ?(on_done = ignore) (exp : t) ?(enable = true) () f
= =
if enable then ( if enable then (
set exp; set exp;
Fun.protect f ~finally:(fun () -> shutdown exp ~on_done) Aswitch.on_turn_off (Exporter.active exp) on_done;
Fun.protect f ~finally:(fun () -> Exporter.shutdown exp)
) else ) else
Fun.protect f ~finally:(fun () -> on_done ()) Fun.protect f ~finally:(fun () -> on_done ())

View file

@ -3,10 +3,6 @@
module Core = Opentelemetry_core module Core = Opentelemetry_core
(** Core types and definitions *) (** Core types and definitions *)
module Alist = Alist
(** Atomic list, for internal usage
@since 0.7 *)
module Interval_limiter = Interval_limiter module Interval_limiter = Interval_limiter
(** Utility to limit the frequency of some event (** Utility to limit the frequency of some event
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
@ -87,9 +83,17 @@ module Logs = Logger [@@deprecated "use Logger"]
(** {2 Utils} *) (** {2 Utils} *)
module Any_signal = Any_signal module Any_signal = Any_signal
module Any_signal_l = Any_signal_l
module Trace_context = Trace_context module Trace_context = Trace_context
module Gc_metrics = Gc_metrics module Gc_metrics = Gc_metrics
module Aswitch = Aswitch
(** @since NEXT_RELEASE *)
module Alist = Alist
(** Atomic list, for internal usage
@since 0.7 *)
(* *) (* *)
module GC_metrics = Gc_metrics module GC_metrics = Gc_metrics