Source file tally.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
type t = {
  name : string ;
  measure : unit -> ((string * string) list * (string * int) list) list
}

let name { name ; _ } = name

let equal { name ; _ } { name = name' ; _ } =
  String.equal name name'

let _sources = ref []

let tally name' =
  List.find_opt (fun { name ; _ } -> String.equal name name') !_sources

let tallies () = !_sources

let measure_tally t =
  List.map (fun (tags, fields) -> t.name, tags, fields) (t.measure ())

let measure () = List.concat_map measure_tally !_sources

let v name measure =
  let src = { name ; measure } in
  if List.exists (equal src) !_sources then
    invalid_arg "source with same name already exists";
  _sources := src :: !_sources;
  src

let gc =
  let measure () =
    let gc = Gc.quick_stat () in
    [
      [],
      [
        "minor_collections", gc.Gc.minor_collections;
        "major_collections", gc.major_collections;
        "heap_words", gc.heap_words;
        "live_words", gc.live_words;
        "live_blocks", gc.live_blocks;
        "free_words", gc.free_words;
        "fragments", gc.fragments;
        "compactions", gc.compactions;
        "top_heap_words", gc.top_heap_words;
        "stack_size", gc.stack_size;
        "forced_major_collections", gc.forced_major_collections;
        "live_stacks_words", gc.live_stacks_words;
      ]
    ]
  in
  v "gc" measure

let logs =
  let measure () =
    [
      [],
      [
        "warnings", Logs.warn_count ();
        "errors", Logs.err_count ();
      ]
    ]
  in
  v "logs" measure

let encode_influx = Influx.encode_line_protocol