Skip to content

Commit

Permalink
add ?data to counter_int and counter_float
Browse files Browse the repository at this point in the history
this makes sense to add metadata in, say, opentelemetry
  • Loading branch information
c-cube committed Sep 17, 2023
1 parent ba9d3d3 commit 0135a61
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/core/collector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ module type S = sig
val name_process : string -> unit
(** 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. *)

val counter_float : string -> float -> unit
val counter_float : data:(string * user_data) list -> string -> float -> unit
(** Float counter. *)

val shutdown : unit -> unit
Expand Down
22 changes: 14 additions & 8 deletions src/core/trace_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ type collector = (module Collector.S)
(** Global collector. *)
let collector : collector option A.t = A.make None

let data_empty_build_ () = []

let[@inline] enabled () =
match A.get collector with
| None -> false
| Some _ -> true

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
C.with_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name f

Expand All @@ -28,7 +30,7 @@ let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name f =
f

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 =
let data = data () in
C.enter_manual_span ~parent ~flavor ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data
Expand Down Expand Up @@ -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
)

let message_collector_ (module C : Collector.S) ?span ?(data = fun () -> []) msg
: unit =
let message_collector_ (module C : Collector.S) ?span
?(data = data_empty_build_) msg : unit =
let data = data () in
C.message ?span ~data msg

Expand All @@ -94,15 +96,19 @@ let messagef ?span ?data k =
C.message ?span ~data str)
fmt)

let counter_int name n : unit =
let counter_int ?(data = data_empty_build_) name n : unit =
match A.get collector with
| 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
| 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 =
match A.get collector with
Expand Down
12 changes: 8 additions & 4 deletions src/core/trace_core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ val set_process_name : string -> unit
This might be used by the collector
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
over time. *)
over time.
@param data metadata for this metric (since NEXT_RELEASE) *)

val counter_float : string -> float -> unit
(** Emit a counter of type [float]. See {!counter_int} for more details. *)
val counter_float :
?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} *)

Expand Down
4 changes: 2 additions & 2 deletions src/tef/trace_tef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,12 @@ let collector ~out () : collector =
let tid = get_tid_ () in
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 tid = get_tid_ () in
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_thread name : unit =
Expand Down

0 comments on commit 0135a61

Please sign in to comment.