|
1 | | -[@@@alert "-deprecated"] |
| 1 | +(* The Lwt collectors for Prometheus metrics. *) |
| 2 | + |
| 3 | +open Prometheus |
2 | 4 |
|
3 | | -(* This transition module reexports the core operations under their |
4 | | - new names to allow the core package to drop the Lwt dependency |
5 | | - in a future release. This interface will not change. *) |
6 | 5 | module CollectorRegistry = struct |
7 | | - type t = Prometheus.CollectorRegistry.t |
| 6 | + type t = { |
| 7 | + core : Prometheus.CollectorRegistry.t; |
| 8 | + mutable metrics_lwt : (unit -> Sample_set.t LabelSetMap.t Lwt.t) MetricFamilyMap.t; |
| 9 | + mutable pre_collect_lwt : (unit -> unit Lwt.t) list; |
| 10 | + } |
| 11 | + |
| 12 | + let of_registry core = { |
| 13 | + core; |
| 14 | + metrics_lwt = MetricFamilyMap.empty; |
| 15 | + pre_collect_lwt = []; |
| 16 | + } |
| 17 | + |
| 18 | + let default = of_registry Prometheus.CollectorRegistry.default |
8 | 19 |
|
9 | | - let of_registry core = core |
| 20 | + let core t = t.core |
10 | 21 |
|
11 | | - let default = Prometheus.CollectorRegistry.default |
| 22 | + let register_pre_collect t f = t.pre_collect_lwt <- f :: t.pre_collect_lwt |
12 | 23 |
|
13 | | - let core t = t |
| 24 | + let register t info collector = |
| 25 | + if MetricFamilyMap.mem info t.metrics_lwt then |
| 26 | + failwith (Format.asprintf "%a already registered" MetricName.pp info.MetricInfo.name); |
| 27 | + t.metrics_lwt <- MetricFamilyMap.add info collector t.metrics_lwt |
14 | 28 |
|
15 | | - let collect = Prometheus.CollectorRegistry.collect |
| 29 | + open Lwt.Infix |
16 | 30 |
|
17 | | - let register = Prometheus.CollectorRegistry.register_lwt |
| 31 | + let map_p m = |
| 32 | + MetricFamilyMap.fold (fun k f acc -> (k, f ()) :: acc) m [] |
| 33 | + |> Lwt_list.fold_left_s |
| 34 | + (fun acc (k, v) -> v >|= fun v -> MetricFamilyMap.add k v acc) |
| 35 | + MetricFamilyMap.empty |
18 | 36 |
|
19 | | - let register_pre_collect = Prometheus.CollectorRegistry.register_pre_collect_lwt |
| 37 | + let collect t = |
| 38 | + Lwt_list.iter_p (fun f -> f ()) t.pre_collect_lwt >>= fun () -> |
| 39 | + let sync = Prometheus.CollectorRegistry.collect t.core in |
| 40 | + map_p t.metrics_lwt >|= fun metrics_lwt -> |
| 41 | + MetricFamilyMap.merge |
| 42 | + (fun _ v1 v2 -> |
| 43 | + match v1 with |
| 44 | + | Some v1 -> Some v1 |
| 45 | + | None -> v2) |
| 46 | + sync metrics_lwt |
20 | 47 | end |
21 | 48 |
|
22 | 49 | module Gauge = struct |
23 | | - let track_in_progress = Prometheus.Gauge.track_inprogress |
| 50 | + let track_in_progress t fn = |
| 51 | + Prometheus.Gauge.inc_one t; |
| 52 | + Lwt.finalize fn (fun () -> Prometheus.Gauge.dec_one t; Lwt.return_unit) |
24 | 53 |
|
25 | | - let set_time = Prometheus.Gauge.time |
| 54 | + let set_time t gettimeofday fn = |
| 55 | + let start = gettimeofday () in |
| 56 | + Lwt.finalize fn |
| 57 | + (fun () -> |
| 58 | + let finish = gettimeofday () in |
| 59 | + Prometheus.Gauge.set t (finish -. start); |
| 60 | + Lwt.return_unit) |
26 | 61 | end |
27 | 62 |
|
28 | 63 | module Summary = struct |
29 | | - let observe_time = Prometheus.Summary.time |
| 64 | + let observe_time t gettimeofday fn = |
| 65 | + let start = gettimeofday () in |
| 66 | + Lwt.finalize fn |
| 67 | + (fun () -> |
| 68 | + let finish = gettimeofday () in |
| 69 | + Prometheus.Summary.observe t (finish -. start); |
| 70 | + Lwt.return_unit) |
30 | 71 | end |
31 | 72 |
|
32 | 73 | module Histogram (H : Prometheus.HISTOGRAM) = struct |
33 | | - let observe_time = H.time |
| 74 | + let observe_time t gettimeofday fn = |
| 75 | + let start = gettimeofday () in |
| 76 | + Lwt.finalize fn |
| 77 | + (fun () -> |
| 78 | + let finish = gettimeofday () in |
| 79 | + H.observe t (finish -. start); |
| 80 | + Lwt.return_unit) |
34 | 81 | end |
0 commit comments