Skip to content

Commit 9218dce

Browse files
author
Andrii Sultanov
committed
prometheus: Add CollectorRegistry.unregister functions
Sources of metrics can sometimes disappear at runtime (a VM being destroyed, disk being unplugged), and rather than continuing to present the last value, it is useful to remove them altogether. Other Prometheus client libraries do implement similar functions as well, see https://docs.rs/prometheus/latest/prometheus/struct.Registry.html#method.unregister for example. Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
1 parent fb4f8b1 commit 9218dce

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/prometheus.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,16 @@ module CollectorRegistry = struct
121121
ensure_not_registered t info;
122122
t.metrics <- MetricFamilyMap.add info collector t.metrics
123123

124+
let unregister t info =
125+
t.metrics <- MetricFamilyMap.remove info t.metrics
126+
124127
let register_lwt t info collector =
125128
ensure_not_registered t info;
126129
t.metrics_lwt <- MetricFamilyMap.add info collector t.metrics_lwt
127130

131+
let unregister_lwt t info =
132+
t.metrics_lwt <- MetricFamilyMap.remove info t.metrics_lwt
133+
128134
open Lwt.Infix
129135

130136
let map_p m =

src/prometheus.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,16 @@ module CollectorRegistry : sig
8989
(** [register t metric collector] adds [metric] to the set of metrics being collected.
9090
It will call [collector ()] to collect the values each time [collect] is called. *)
9191

92+
val unregister : t -> MetricInfo.t -> unit
93+
(** [unregister t metric] removes [metric] from the set of metrics being collected. *)
94+
9295
val register_lwt : t -> MetricInfo.t -> (unit -> Sample_set.t LabelSetMap.t Lwt.t) -> unit
9396
(** [register_lwt t metric collector] is the same as [register t metrics collector]
9497
but [collector] returns [Sample_set.t LabelSetMap.t Lwt.t]. *)
9598

99+
val unregister_lwt : t -> MetricInfo.t -> unit
100+
(** [unregister_lwt t metric] removes [metric] from the set of metrics being collected. *)
101+
96102
val register_pre_collect : t -> (unit -> unit) -> unit
97103
(** [register_pre_collect t fn] arranges for [fn ()] to be called at the start
98104
of each collection. This is useful if one expensive call provides

0 commit comments

Comments
 (0)