Source file metrics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(** Metrics.

    See
    {{:https://opentelemetry.io/docs/reference/specification/overview/#metric-signal}
     the spec} *)

open Common_
open Proto
open Proto.Metrics

type t = Metrics.metric
(** A single metric, measuring some time-varying quantity or statistical
    distribution. It is composed of one or more data points that have precise
    values and time stamps. Each distinct metric should have a distinct name. *)

let pp = Proto.Metrics.pp_metric

(** Number data point, as a float *)
let float ?start_time_unix_nano ?(attrs = []) ?(now = Clock.now_main ())
    (d : float) : number_data_point =
  let attributes = attrs |> List.map Key_value.conv in
  make_number_data_point ?start_time_unix_nano ~time_unix_nano:now ~attributes
    ~value:(As_double d) ()

(** Number data point, as an int *)
let int ?start_time_unix_nano ?(attrs = []) ?(now = Clock.now_main ()) (i : int)
    : number_data_point =
  let attributes = attrs |> List.map Key_value.conv in
  make_number_data_point ?start_time_unix_nano ~time_unix_nano:now ~attributes
    ~value:(As_int (Int64.of_int i))
    ()

(** Aggregation of a scalar metric, always with the current value *)
let gauge ~name ?description ?unit_ (l : number_data_point list) : t =
  let data = Gauge (make_gauge ~data_points:l ()) in
  make_metric ~name ?description ?unit_ ~data ()

type aggregation_temporality = Metrics.aggregation_temporality =
  | Aggregation_temporality_unspecified
  | Aggregation_temporality_delta
  | Aggregation_temporality_cumulative

(** Sum of all reported measurements over a time interval *)
let sum ~name ?description ?unit_
    ?(aggregation_temporality = Aggregation_temporality_cumulative)
    ?is_monotonic (l : number_data_point list) : t =
  let data =
    Sum (make_sum ~data_points:l ?is_monotonic ~aggregation_temporality ())
  in
  make_metric ~name ?description ?unit_ ~data ()

type histogram_data_point = Metrics.histogram_data_point

(** Histogram data
    @param count number of values in population (non negative)
    @param sum sum of values in population (0 if count is 0)
    @param now the timestamp for this data point
    @param bucket_counts
      count value of histogram for each bucket. Sum of the counts must be equal
      to [count]. length must be [1+length explicit_bounds] (unless both have
      length 0)
    @param explicit_bounds strictly increasing list of bounds for the buckets *)
let histogram_data_point ?start_time_unix_nano ?(attrs = []) ?(exemplars = [])
    ~explicit_bounds ?sum ?(now = Clock.now_main ()) ~bucket_counts ~count () :
    histogram_data_point =
  let attributes = attrs |> List.map Key_value.conv in
  make_histogram_data_point ?start_time_unix_nano ~time_unix_nano:now
    ~attributes ~exemplars ~bucket_counts ~explicit_bounds ~count ?sum ()

let histogram ~name ?description ?unit_ ?aggregation_temporality
    (l : histogram_data_point list) : t =
  let data =
    Histogram (make_histogram ~data_points:l ?aggregation_temporality ())
  in
  make_metric ~name ?description ?unit_ ~data ()

let add_attrs (m : t) (attrs : Key_value.t list) : unit =
  let attrs = List.map Key_value.conv attrs in
  match m.data with
  | None -> ()
  | Some (Gauge g) ->
    List.iter
      (fun (dp : number_data_point) ->
        number_data_point_set_attributes dp (attrs @ dp.attributes))
      g.data_points
  | Some (Sum s) ->
    List.iter
      (fun (dp : number_data_point) ->
        number_data_point_set_attributes dp (attrs @ dp.attributes))
      s.data_points
  | Some (Histogram h) ->
    List.iter
      (fun (dp : histogram_data_point) ->
        histogram_data_point_set_attributes dp (attrs @ dp.attributes))
      h.data_points
  | Some (Exponential_histogram eh) ->
    List.iter
      (fun (dp : exponential_histogram_data_point) ->
        exponential_histogram_data_point_set_attributes dp
          (attrs @ dp.attributes))
      eh.data_points
  | Some (Summary s) ->
    List.iter
      (fun (dp : summary_data_point) ->
        summary_data_point_set_attributes dp (attrs @ dp.attributes))
      s.data_points

(* TODO: exponential history *)
(* TODO: summary *)
(* TODO: exemplar *)