add ?data to counter_int and counter_float

this makes sense to add metadata in, say, opentelemetry
This commit is contained in:
Simon Cruanes 2023-09-15 09:54:08 -04:00
parent ba9d3d3d20
commit 0135a613a9
4 changed files with 26 additions and 16 deletions

View file

@ -71,10 +71,10 @@ module type S = sig
val name_process : string -> unit val name_process : string -> unit
(** Give a name to the current process. *) (** Give a name to the current process. *)
val counter_int : string -> int -> unit val counter_int : data:(string * user_data) list -> string -> int -> unit
(** Integer counter. *) (** Integer counter. *)
val counter_float : string -> float -> unit val counter_float : data:(string * user_data) list -> string -> float -> unit
(** Float counter. *) (** Float counter. *)
val shutdown : unit -> unit val shutdown : unit -> unit

View file

@ -8,13 +8,15 @@ type collector = (module Collector.S)
(** Global collector. *) (** Global collector. *)
let collector : collector option A.t = A.make None let collector : collector option A.t = A.make None
let data_empty_build_ () = []
let[@inline] enabled () = let[@inline] enabled () =
match A.get collector with match A.get collector with
| None -> false | None -> false
| Some _ -> true | Some _ -> true
let with_span_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ let with_span_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__
~__LINE__ ?(data = fun () -> []) name f = ~__LINE__ ?(data = data_empty_build_) name f =
let data = data () in let data = data () in
C.with_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name f C.with_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name f
@ -28,7 +30,7 @@ let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name f =
f f
let enter_explicit_span_collector_ (module C : Collector.S) ~parent ~flavor let enter_explicit_span_collector_ (module C : Collector.S) ~parent ~flavor
?__FUNCTION__ ~__FILE__ ~__LINE__ ?(data = fun () -> []) name : ?__FUNCTION__ ~__FILE__ ~__LINE__ ?(data = data_empty_build_) name :
explicit_span = explicit_span =
let data = data () in let data = data () in
C.enter_manual_span ~parent ~flavor ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data C.enter_manual_span ~parent ~flavor ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data
@ -69,8 +71,8 @@ let[@inline] add_data_to_manual_span esp data : unit =
| Some (module C) -> C.add_data_to_manual_span esp data | Some (module C) -> C.add_data_to_manual_span esp data
) )
let message_collector_ (module C : Collector.S) ?span ?(data = fun () -> []) msg let message_collector_ (module C : Collector.S) ?span
: unit = ?(data = data_empty_build_) msg : unit =
let data = data () in let data = data () in
C.message ?span ~data msg C.message ?span ~data msg
@ -94,15 +96,19 @@ let messagef ?span ?data k =
C.message ?span ~data str) C.message ?span ~data str)
fmt) fmt)
let counter_int name n : unit = let counter_int ?(data = data_empty_build_) name n : unit =
match A.get collector with match A.get collector with
| None -> () | None -> ()
| Some (module C) -> C.counter_int name n | Some (module C) ->
let data = data () in
C.counter_int ~data name n
let counter_float name f : unit = let counter_float ?(data = data_empty_build_) name f : unit =
match A.get collector with match A.get collector with
| None -> () | None -> ()
| Some (module C) -> C.counter_float name f | Some (module C) ->
let data = data () in
C.counter_float ~data name f
let set_thread_name name : unit = let set_thread_name name : unit =
match A.get collector with match A.get collector with

View file

@ -110,12 +110,16 @@ val set_process_name : string -> unit
This might be used by the collector This might be used by the collector
to display traces in a more informative way. *) to display traces in a more informative way. *)
val counter_int : string -> int -> unit val counter_int :
?data:(unit -> (string * user_data) list) -> string -> int -> unit
(** Emit a counter of type [int]. Counters represent the evolution of some quantity (** Emit a counter of type [int]. Counters represent the evolution of some quantity
over time. *) over time.
@param data metadata for this metric (since NEXT_RELEASE) *)
val counter_float : string -> float -> unit val counter_float :
(** Emit a counter of type [float]. See {!counter_int} for more details. *) ?data:(unit -> (string * user_data) list) -> string -> float -> unit
(** Emit a counter of type [float]. See {!counter_int} for more details.
@param data metadata for this metric (since NEXT_RELEASE) *)
(** {2 Collector} *) (** {2 Collector} *)

View file

@ -447,12 +447,12 @@ let collector ~out () : collector =
let tid = get_tid_ () in let tid = get_tid_ () in
B_queue.push events (E_message { tid; time_us; msg; data }) B_queue.push events (E_message { tid; time_us; msg; data })
let counter_float name f = let counter_float ~data:_ name f =
let time_us = now_us () in let time_us = now_us () in
let tid = get_tid_ () in let tid = get_tid_ () in
B_queue.push events (E_counter { name; n = f; time_us; tid }) B_queue.push events (E_counter { name; n = f; time_us; tid })
let counter_int name i = counter_float name (float_of_int i) let counter_int ~data name i = counter_float ~data name (float_of_int i)
let name_process name : unit = B_queue.push events (E_name_process { name }) let name_process name : unit = B_queue.push events (E_name_process { name })
let name_thread name : unit = let name_thread name : unit =