Skip to content

Commit

Permalink
Add Sample_set module
Browse files Browse the repository at this point in the history
Using a list of tuples was a bit confusing.
  • Loading branch information
Thomas Leonard committed Nov 20, 2017
1 parent 85c3b41 commit 29e7d12
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
6 changes: 3 additions & 3 deletions app/prometheus_app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,7 +83,7 @@ module Runtime = struct
}
in
let collect () =
LabelSetMap.singleton [] ["", fn ()]
LabelSetMap.singleton [] [Sample_set.sample (fn ())]
in
info, collect

Expand Down
2 changes: 1 addition & 1 deletion app/prometheus_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Unix_runtime = struct
}
in
let collect () =
LabelSetMap.singleton [] ["", fn ()]
LabelSetMap.singleton [] [Sample_set.sample (fn ())]
in
info, collect

Expand Down
25 changes: 18 additions & 7 deletions src/prometheus.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/prometheus.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. *)

Expand Down

0 comments on commit 29e7d12

Please sign in to comment.