Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lwt/prometheus_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions lwt/prometheus_lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lwt/test/test_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want this to be optional in the future then the label should go before the fixed parameter shouldn’t it?

@talex5 talex5 Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that only matters when it's defined, not when it's used.

However, I think perhaps we should go further and get rid of these gettime arguments completely right now. I'll try that out in a new PR...

Update: done in #68

(fun () -> clock := !clock +. 1.5; Lwt.pause ())
>>= fun () ->
Prometheus_lwt.CollectorRegistry.collect registry >|= fun collected ->
Expand Down
20 changes: 10 additions & 10 deletions src/prometheus.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/prometheus.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions tests/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down