1- open Prometheus
2-
3- let failf fmt =
4- Fmt. kstr failwith fmt
5-
6- module TextFormat_0_0_4 = struct
7- let re_unquoted_escapes = Re. compile @@ Re. set " \\\n "
8- let re_quoted_escapes = Re. compile @@ Re. set " \"\\\n "
9-
10- let quote g =
11- match Re.Group. get g 0 with
12- | "\\ " -> " \\\\ "
13- | "\n " -> " \\ n"
14- | "\" " -> " \\\" "
15- | x -> failf " Unexpected match %S" x
16-
17- let output_metric_type f = function
18- | Counter -> Fmt. string f " counter"
19- | Gauge -> Fmt. string f " gauge"
20- | Summary -> Fmt. string f " summary"
21- | Histogram -> Fmt. string f " histogram"
22-
23- let output_unquoted f s =
24- Fmt. string f @@ Re. replace re_unquoted_escapes ~f: quote s
25-
26- let output_quoted f s =
27- Fmt. string f @@ Re. replace re_quoted_escapes ~f: quote s
28-
29- (* Fmt.float by default prints floats using scientific exponential
30- * notation, which loses significant data on e.g. timestamp:
31- * Fmt.str "%a" Fmt.float 1575363850.57 --> 1.57536e+09 *)
32- let float_fmt f =
33- Fmt. pf f " %f"
34-
35- let output_value f v =
36- match classify_float v with
37- | FP_normal | FP_subnormal | FP_zero -> float_fmt f v
38- | FP_infinite when v > 0.0 -> Fmt. string f " +Inf"
39- | FP_infinite -> Fmt. string f " -Inf"
40- | FP_nan -> Fmt. string f " Nan"
41-
42- let output_pairs f (label_names , label_values ) =
43- let cont = ref false in
44- let output_pair name value =
45- if ! cont then Fmt. string f " , "
46- else cont := true ;
47- Fmt. pf f " %a=\" %a\" " LabelName. pp name output_quoted value
48- in
49- List. iter2 output_pair label_names label_values
50-
51- let output_labels ~label_names f = function
52- | [] -> ()
53- | label_values -> Fmt. pf f " {%a}" output_pairs (label_names, label_values)
54-
55- let output_sample ~base ~label_names ~label_values f { Sample_set. ext; value; bucket } =
56- let label_names, label_values = match bucket with
57- | None -> label_names, label_values
58- | Some (label_name , label_value ) ->
59- let label_value_str = Fmt. str " %a" output_value label_value in
60- label_name :: label_names, label_value_str :: label_values
61- in
62- Fmt. pf f " %a%s%a %a@."
63- MetricName. pp base ext
64- (output_labels ~label_names ) label_values
65- output_value value
66-
67- let output_metric ~name ~label_names f (label_values , samples ) =
68- List. iter (output_sample ~base: name ~label_names ~label_values f) samples
69-
70- let output f =
71- MetricFamilyMap. iter (fun metric samples ->
72- let {MetricInfo. name; metric_type; help; label_names} = metric in
73- Fmt. pf f
74- " # HELP %a %a@.\
75- # TYPE %a %a@.\
76- %a"
77- MetricName. pp name output_unquoted help
78- MetricName. pp name output_metric_type metric_type
79- (LabelSetMap. pp ~sep: Fmt. nop (output_metric ~name ~label_names )) samples
80- )
81- end
82-
83- module Runtime = struct
84- let current = ref (Gc. quick_stat () )
85- let update () =
86- current := Gc. quick_stat ()
87-
88- let simple_metric ~metric_type ~help name fn =
89- let info = {
90- MetricInfo.
91- name = MetricName. v name;
92- help;
93- metric_type;
94- label_names = [] ;
95- }
96- in
97- let collect () =
98- LabelSetMap. singleton [] [Sample_set. sample (fn () )]
99- in
100- info, collect
101-
102- let ocaml_gc_allocated_bytes =
103- simple_metric ~metric_type: Counter " ocaml_gc_allocated_bytes" Gc. allocated_bytes
104- ~help: " Total number of bytes allocated since the program was started."
105-
106- let ocaml_gc_major_words =
107- simple_metric ~metric_type: Counter " ocaml_gc_major_words" (fun () -> (! current).Gc. major_words)
108- ~help: " Number of words allocated in the major heap since the program was started."
109-
110- let ocaml_gc_minor_collections =
111- simple_metric ~metric_type: Counter " ocaml_gc_minor_collections" (fun () -> float_of_int (! current).Gc. minor_collections)
112- ~help: " Number of minor collection cycles completed since the program was started."
113-
114- let ocaml_gc_major_collections =
115- simple_metric ~metric_type: Counter " ocaml_gc_major_collections" (fun () -> float_of_int (! current).Gc. major_collections)
116- ~help: " Number of major collection cycles completed since the program was started."
117-
118- let ocaml_gc_heap_words =
119- simple_metric ~metric_type: Gauge " ocaml_gc_heap_words" (fun () -> float_of_int (! current).Gc. heap_words)
120- ~help: " Total size of the major heap, in words."
121-
122- let ocaml_gc_compactions =
123- simple_metric ~metric_type: Counter " ocaml_gc_compactions" (fun () -> float_of_int (! current).Gc. compactions)
124- ~help: " Number of heap compactions since the program was started."
125-
126- let ocaml_gc_top_heap_words =
127- simple_metric ~metric_type: Counter " ocaml_gc_top_heap_words" (fun () -> float_of_int (! current).Gc. top_heap_words)
128- ~help: " Maximum size reached by the major heap, in words."
129-
130- let process_cpu_seconds_total =
131- simple_metric ~metric_type: Counter " process_cpu_seconds_total" Sys. time
132- ~help: " Total user and system CPU time spent in seconds."
133-
134- let metrics = [
135- ocaml_gc_allocated_bytes;
136- ocaml_gc_major_words;
137- ocaml_gc_minor_collections;
138- ocaml_gc_major_collections;
139- ocaml_gc_heap_words;
140- ocaml_gc_compactions;
141- ocaml_gc_top_heap_words;
142- process_cpu_seconds_total;
143- ]
144- end
1+ include Prometheus_reporter
1452
1463open Lwt.Infix
1474
@@ -157,9 +14,3 @@ module Cohttp(Server : Cohttp_lwt.S.Server) = struct
15714 Server. respond_string ~status: `OK ~headers ~body ()
15815 | _ -> Server. respond_error ~status: `Bad_request ~body: " Bad request" ()
15916end
160-
161- let () =
162- CollectorRegistry. (register_pre_collect default) Runtime. update;
163- let add (info , collector ) =
164- CollectorRegistry. (register default) info collector in
165- List. iter add Runtime. metrics
0 commit comments