Skip to content

Commit 0a200f0

Browse files
authored
Add metric for images per OCaml version (#272)
1 parent 0df4bc2 commit 0a200f0

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

src/index.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Platform_map = Map.Make (String)
22
module Switch_map = Map.Make (Ocaml_version)
33

44
type state = Ok | Failed | Active
5-
(* Each platform builds one non-OCaml opam image, and then an image for every version of OCaml *)
5+
66
type t = state Switch_map.t Platform_map.t * state Platform_map.t
77

88
let v : t ref = ref Platform_map.(empty, empty)

src/index.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ module Switch_map : (Map.S with type key = Ocaml_version.t)
44

55
type state = Ok | Failed | Active
66

7+
(** Each platform builds one non-OCaml opam image, and then an image for every version of OCaml.
8+
For [p : platforms], [fst p] is the state for the image of each switch for each platform,
9+
and [snd p] is the state for the non-OCaml image for each platform. *)
710
type t = state Switch_map.t Platform_map.t * state Platform_map.t
811

9-
val update_images_per_platform : platform:string -> switch:Ocaml_version.t option -> state -> unit
10-
1112
val get_images_per_platform : unit -> t
1213

14+
val update_images_per_platform : platform:string -> switch:Ocaml_version.t option -> state -> unit
15+
1316
val get_latest_build_time : unit -> float option

src/metrics.ml

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ open Prometheus
33
let namespace = "baseimages"
44
let subsystem = "pipeline"
55

6-
let family =
6+
let platform_family =
77
let help = "Number of images by platform" in
88
Gauge.v_labels ~label_names:["platform"; "state"] ~help ~namespace ~subsystem
9-
"image_state_total"
9+
"image_platform_state_total"
10+
11+
let version_family =
12+
let help = "Number of images by OCaml version" in
13+
Gauge.v_labels ~label_names:["version"; "state"] ~help ~namespace ~subsystem
14+
"image_version_state_total"
1015

1116
let last_build_time =
1217
let help = "When the images were last built, in unix time" in
@@ -19,31 +24,61 @@ let image_valid_time =
1924
type stats = { ok : int; failed : int; active : int }
2025
let stats_empty = { ok = 0; failed = 0; active = 0 }
2126

22-
let update () =
23-
let incr_stats stats = function
24-
| Index.Ok -> { stats with ok = stats.ok + 1 }
25-
| Index.Failed -> { stats with failed = stats.failed + 1 }
26-
| Index.Active -> { stats with active = stats.active + 1 }
27-
in
27+
open Index
28+
29+
let incr_stats stats = function
30+
| Ok -> { stats with ok = stats.ok + 1 }
31+
| Failed -> { stats with failed = stats.failed + 1 }
32+
| Active -> { stats with active = stats.active + 1 }
33+
34+
let update_images_per_platform ocaml_images non_ocaml_images =
2835
let f opam_map platform sm =
2936
let stats =
30-
Index.Switch_map.fold
37+
Switch_map.fold
3138
(fun _ state stats -> incr_stats stats state)
3239
sm stats_empty
3340
in
3441
let stats =
35-
Option.map (incr_stats stats) (Index.Platform_map.find_opt platform opam_map)
42+
Option.map (incr_stats stats) (Platform_map.find_opt platform opam_map)
3643
|> Option.value ~default:stats
3744
in
38-
Gauge.set (Gauge.labels family [platform; "ok"]) (float_of_int stats.ok);
39-
Gauge.set (Gauge.labels family [platform; "failed"]) (float_of_int stats.failed);
40-
Gauge.set (Gauge.labels family [platform; "active"]) (float_of_int stats.active)
45+
Gauge.set (Gauge.labels platform_family [platform; "ok"])
46+
(float_of_int stats.ok);
47+
Gauge.set (Gauge.labels platform_family [platform; "failed"])
48+
(float_of_int stats.failed);
49+
Gauge.set (Gauge.labels platform_family [platform; "active"])
50+
(float_of_int stats.active)
51+
in
52+
Platform_map.iter (f non_ocaml_images) ocaml_images
53+
54+
let update_images_per_version ocaml_images =
55+
let f v state acc =
56+
let stats =
57+
Option.value ~default:stats_empty @@ Switch_map.find_opt v acc
58+
in
59+
Switch_map.add v (incr_stats stats state) acc
4160
in
42-
let v = Index.get_images_per_platform () in
43-
Index.Platform_map.iter (f (snd v)) (fst v)
61+
let stats =
62+
Platform_map.fold (fun _ sm acc -> Switch_map.fold f sm acc)
63+
ocaml_images Switch_map.empty
64+
in
65+
Switch_map.iter (fun v stats ->
66+
let version = Ocaml_version.to_string v in
67+
Gauge.set (Gauge.labels version_family [version; "ok"])
68+
(float_of_int stats.ok);
69+
Gauge.set (Gauge.labels version_family [version; "failed"])
70+
(float_of_int stats.failed);
71+
Gauge.set (Gauge.labels version_family [version; "active"])
72+
(float_of_int stats.active))
73+
stats
74+
75+
let update () =
76+
let ocaml_images, non_ocaml_images = get_images_per_platform () in
77+
update_images_per_platform ocaml_images non_ocaml_images;
78+
update_images_per_version ocaml_images
4479

4580
let init_last_build_time () =
46-
Index.get_latest_build_time ()
81+
get_latest_build_time ()
4782
|> Option.iter (Gauge.set last_build_time);
4883
Gauge.set image_valid_time
4984
(float_of_int @@ Conf.days_between_rebuilds * 60 * 60 * 24)

0 commit comments

Comments
 (0)