fix metrics_callbacks' API to make it easier

This commit is contained in:
Simon Cruanes 2025-12-05 23:51:59 -05:00
parent ee40e445d1
commit c24dbebbf3
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 19 additions and 1 deletions

View file

@ -22,10 +22,20 @@ let add_to_exporter (exp : Exporter.t) (self : t) =
in
Exporter.on_tick exp on_tick
let with_set_added_to_exporter (exp : Exporter.t) (f : t -> 'a) : 'a =
let set = create () in
add_to_exporter exp set;
f set
let with_set_added_to_main_exporter (f : t -> unit) : unit =
match Main_exporter.get () with
| None -> ()
| Some exp -> with_set_added_to_exporter exp f
module Main_set = struct
let cur_set_ : t option Atomic.t = Atomic.make None
let rec get () =
let rec get () : t =
match Atomic.get cur_set_ with
| Some s -> s
| None ->

View file

@ -19,6 +19,14 @@ val add_metrics_cb : t -> (unit -> Metrics.t list) -> unit
val add_to_exporter : Exporter.t -> t -> unit
(** Make sure we export metrics at every [tick] of the exporter *)
val with_set_added_to_exporter : Exporter.t -> (t -> 'a) -> 'a
(** [with_set_added_to_exporter exp f] creates a set, adds it to the exporter,
and calls [f] on it *)
val with_set_added_to_main_exporter : (t -> unit) -> unit
(** If there is a main exporter, add a set to it and call [f set], else do not
call [f] at all *)
module Main_set : sig
val get : unit -> t
(** The global set *)