diff --git a/app/prometheus_app.ml b/app/prometheus_app.ml index 3e3382e..5a77914 100644 --- a/app/prometheus_app.ml +++ b/app/prometheus_app.ml @@ -46,11 +46,11 @@ module TextFormat_0_0_4 = struct | [] -> () | label_values -> Fmt.pf f "{%a}" output_pairs (label_names, label_values) - let output_sample ~base ~label_names ~label_values f (ext, sample) = + let output_sample ~base ~label_names ~label_values f { Sample_set.ext; value } = Fmt.pf f "%a%s%a %a@." MetricName.pp base ext (output_labels ~label_names) label_values - output_value sample + output_value value let output_metric ~name ~label_names f (label_values, samples) = List.iter (output_sample ~base:name ~label_names ~label_values f) samples @@ -83,7 +83,7 @@ module Runtime = struct } in let collect () = - LabelSetMap.singleton [] ["", fn ()] + LabelSetMap.singleton [] [Sample_set.sample (fn ())] in info, collect diff --git a/app/prometheus_unix.ml b/app/prometheus_unix.ml index 51217f3..66e852e 100644 --- a/app/prometheus_unix.ml +++ b/app/prometheus_unix.ml @@ -13,7 +13,7 @@ module Unix_runtime = struct } in let collect () = - LabelSetMap.singleton [] ["", fn ()] + LabelSetMap.singleton [] [Sample_set.sample (fn ())] in info, collect diff --git a/src/prometheus.ml b/src/prometheus.ml index 8f6c948..d67c8f2 100644 --- a/src/prometheus.ml +++ b/src/prometheus.ml @@ -79,13 +79,24 @@ end module MetricFamilyMap = Map.Make(MetricInfo) +module Sample_set = struct + type sample = { + ext : string; + value : float; + } + + type t = sample list + + let sample ?(ext="") value = { ext; value } +end + module CollectorRegistry = struct type t = { - mutable metrics : (unit -> (string * float) list LabelSetMap.t) MetricFamilyMap.t; + mutable metrics : (unit -> Sample_set.t LabelSetMap.t) MetricFamilyMap.t; mutable pre_collect : (unit -> unit) list; } - type snapshot = (string * float) list LabelSetMap.t MetricFamilyMap.t + type snapshot = Sample_set.t LabelSetMap.t MetricFamilyMap.t let create () = { metrics = MetricFamilyMap.empty; @@ -118,7 +129,7 @@ end module type CHILD = sig type t val create : unit -> t - val values : t -> (string * float) list (* extension, value *) + val values : t -> Sample_set.t val metric_type : metric_type end @@ -167,7 +178,7 @@ module Counter = struct include Metric(struct type t = float ref let create () = ref 0.0 - let values t = ["", !t] + let values t = [Sample_set.sample !t] let metric_type = Counter end) @@ -183,7 +194,7 @@ module Gauge = struct include Metric(struct type t = float ref let create () = ref 0.0 - let values t = ["", !t] + let values t = [Sample_set.sample !t] let metric_type = Gauge end) @@ -220,8 +231,8 @@ module Summary = struct let create () = { count = 0.0; sum = 0.0 } let values t = [ - "_sum", t.sum; - "_count", t.count; + Sample_set.sample ~ext:"_sum" t.sum; + Sample_set.sample ~ext:"_count" t.count; ] let metric_type = Summary end diff --git a/src/prometheus.mli b/src/prometheus.mli index ab87430..006c3a8 100644 --- a/src/prometheus.mli +++ b/src/prometheus.mli @@ -51,11 +51,27 @@ module LabelSetMap : Map.S with type key = string list module MetricFamilyMap : Map.S with type key = MetricInfo.t (** A map indexed by metric families. *) +module Sample_set : sig + type sample = { + ext : string; (** An extension to append to the base metric name. *) + value : float; + } + + type t = sample list + (** A collection of values that together represent a single sample. + For a counter, each reading is just a single value, but more complex types + require multiple values. + For example, a "summary" sample set contains "_sum" and "_count" values. + *) + + val sample : ?ext:string -> float -> sample +end + module CollectorRegistry : sig type t (** A collection of metrics to be monitored. *) - type snapshot = (string * float) list LabelSetMap.t MetricFamilyMap.t + type snapshot = Sample_set.t LabelSetMap.t MetricFamilyMap.t (** The result of reading a set of metrics. *) val create : unit -> t @@ -67,7 +83,7 @@ module CollectorRegistry : sig val collect : t -> snapshot (** Read the current value of each metric. *) - val register : t -> MetricInfo.t -> (unit -> (string * float) list LabelSetMap.t) -> unit + val register : t -> MetricInfo.t -> (unit -> Sample_set.t LabelSetMap.t) -> unit (** [register t metric collector] adds [metric] to the set of metrics being collected. It will call [collector ()] to collect the values each time [collect] is called. *)