diff --git a/lwt/prometheus_lwt.ml b/lwt/prometheus_lwt.ml index d0cd169..1744766 100644 --- a/lwt/prometheus_lwt.ml +++ b/lwt/prometheus_lwt.ml @@ -22,13 +22,13 @@ end module Gauge = struct let track_in_progress = Prometheus.Gauge.track_inprogress - let set_time = Prometheus.Gauge.time + let set_time ~gettime t fn = Prometheus.Gauge.time t gettime fn end module Summary = struct - let observe_time = Prometheus.Summary.time + let observe_time ~gettime t fn = Prometheus.Summary.time t gettime fn end module Histogram (H : Prometheus.HISTOGRAM) = struct - let observe_time = H.time + let observe_time ~gettime t fn = H.time t gettime fn end diff --git a/lwt/prometheus_lwt.mli b/lwt/prometheus_lwt.mli index 355c981..7bb59a7 100644 --- a/lwt/prometheus_lwt.mli +++ b/lwt/prometheus_lwt.mli @@ -33,17 +33,17 @@ module Gauge : sig val track_in_progress : Prometheus.Gauge.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t (** [track_in_progress t f] increases [t] by one while [f ()] is running. *) - val set_time : Prometheus.Gauge.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t - (** [set_time t gettime f] calls [gettime ()] before and after executing [f ()] and + val set_time : gettime:(unit -> float) -> Prometheus.Gauge.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t + (** [set_time ~gettime t f] calls [gettime ()] before and after executing [f ()] and sets [t] to the difference. *) end module Summary : sig - val observe_time : Prometheus.Summary.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t - (** [observe_time t gettime f] observes the duration of [f ()]. *) + val observe_time : gettime:(unit -> float) -> Prometheus.Summary.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t + (** [observe_time ~gettime t f] observes the duration of [f ()]. *) end module Histogram (H : Prometheus.HISTOGRAM) : sig - val observe_time : H.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t - (** [observe_time t gettime f] observes the duration of [f ()]. *) + val observe_time : gettime:(unit -> float) -> H.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t + (** [observe_time ~gettime t f] observes the duration of [f ()]. *) end diff --git a/lwt/test/test_lwt.ml b/lwt/test/test_lwt.ml index 328937f..6d768fa 100644 --- a/lwt/test/test_lwt.ml +++ b/lwt/test/test_lwt.ml @@ -46,7 +46,7 @@ let test_timers () = let gauge = Gauge.v ~registry:core ~help:"Time taken" "gauge_time" in let clock = ref 0.0 in let gettime () = !clock in - Prometheus_lwt.Gauge.set_time gauge gettime + Prometheus_lwt.Gauge.set_time gauge ~gettime (fun () -> clock := !clock +. 1.5; Lwt.pause ()) >>= fun () -> Prometheus_lwt.CollectorRegistry.collect registry >|= fun collected -> diff --git a/src/prometheus.ml b/src/prometheus.ml index 4b28582..e960aec 100644 --- a/src/prometheus.ml +++ b/src/prometheus.ml @@ -266,11 +266,11 @@ module Gauge = struct Lwt.return_unit ) - let set_time t gettimeofday fn = - let start = gettimeofday () in + let set_time ~gettime t fn = + let start = gettime () in Fun.protect fn ~finally:(fun () -> - let finish = gettimeofday () in + let finish = gettime () in set t (finish -. start) ) end @@ -309,11 +309,11 @@ module Summary = struct Lwt.return_unit ) - let observe_time t gettimeofday fn = - let start = gettimeofday () in + let observe_time ~gettime t fn = + let start = gettime () in Fun.protect fn ~finally:(fun () -> - let finish = gettimeofday () in + let finish = gettime () in observe t (finish -. start) ) end @@ -365,7 +365,7 @@ module type HISTOGRAM = sig include METRIC val observe : t -> float -> unit val time : t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t [@@deprecated] - val observe_time : t -> (unit -> float) -> (unit -> 'a) -> 'a + val observe_time : gettime:(unit -> float) -> t -> (unit -> 'a) -> 'a end let bucket_label = LabelName.v "le" @@ -422,11 +422,11 @@ module Histogram (Buckets : BUCKETS) = struct Lwt.return_unit ) - let observe_time t gettimeofday fn = - let start = gettimeofday () in + let observe_time ~gettime t fn = + let start = gettime () in Fun.protect fn ~finally:(fun () -> - let finish = gettimeofday () in + let finish = gettime () in observe t (finish -. start) ) end diff --git a/src/prometheus.mli b/src/prometheus.mli index 14e1aa1..5836cdb 100644 --- a/src/prometheus.mli +++ b/src/prometheus.mli @@ -180,8 +180,8 @@ module Gauge : sig (** [time t gettime f] calls [gettime ()] before and after executing [f ()] and increases the metric by the difference. *) - val set_time : t -> (unit -> float) -> (unit -> 'a) -> 'a - (** [set_time t gettime f] calls [gettime ()] before and after executing [f ()] and + val set_time : gettime:(unit -> float) -> t -> (unit -> 'a) -> 'a + (** [set_time ~gettime t f] calls [gettime ()] before and after executing [f ()] and sets [t] to the difference. *) end (** A gauge is a metric that represents a single numerical value that can arbitrarily go up and down. *) @@ -197,8 +197,8 @@ module Summary : sig (** [time t gettime f] calls [gettime ()] before and after executing [f ()] and observes the difference. *) - val observe_time : t -> (unit -> float) -> (unit -> 'a) -> 'a - (** [observe_time t gettime f] calls [gettime ()] before and after executing [f ()] and + val observe_time : gettime:(unit -> float) -> t -> (unit -> 'a) -> 'a + (** [observe_time ~gettime t f] calls [gettime ()] before and after executing [f ()] and observes the difference. *) end (** A summary is a metric that records both the number of readings and their total. @@ -235,8 +235,8 @@ module type HISTOGRAM = sig (** [time t gettime f] calls [gettime ()] before and after executing [f ()] and observes the difference. *) - val observe_time : t -> (unit -> float) -> (unit -> 'a) -> 'a - (** [observe_time t gettime f] calls [gettime ()] before and after executing [f ()] and + val observe_time : gettime:(unit -> float) -> t -> (unit -> 'a) -> 'a + (** [observe_time ~gettime t f] calls [gettime ()] before and after executing [f ()] and observes the difference. *) end diff --git a/tests/test.ml b/tests/test.ml index 9848dec..8f08250 100644 --- a/tests/test.ml +++ b/tests/test.ml @@ -99,11 +99,11 @@ let test_sync_timers () = let summary = Summary.v ~registry ~help:"Time taken" "summary_time" in let clock = ref 0.0 in let gettime () = !clock in - Gauge.set_time gauge gettime (fun () -> clock := !clock +. 1.3); - Gauge.set_time gauge gettime (fun () -> clock := !clock +. 1.5); + Gauge.set_time gauge ~gettime (fun () -> clock := !clock +. 1.3); + Gauge.set_time gauge ~gettime (fun () -> clock := !clock +. 1.5); Gauge.track_in_progress gauge (fun () -> ()); - Summary.observe_time summary gettime (fun () -> clock := !clock +. 0.5); - Summary.observe_time summary gettime (fun () -> clock := !clock +. 1.5); + Summary.observe_time summary ~gettime (fun () -> clock := !clock +. 0.5); + Summary.observe_time summary ~gettime (fun () -> clock := !clock +. 1.5); CollectorRegistry.collect registry >|= fun collected -> let output = Fmt.to_to_string TextFormat_0_0_4.output collected in Alcotest.(check string) "Text output"