package opentelemetry

  1. Overview
  2. Docs

Source file common.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
[@@@ocaml.warning "-27-30-39"]

type any_value =
  | String_value of string
  | Bool_value of bool
  | Int_value of int64
  | Double_value of float
  | Array_value of array_value
  | Kvlist_value of key_value_list
  | Bytes_value of bytes

and array_value = {
  values : any_value list;
}

and key_value_list = {
  values : key_value list;
}

and key_value = {
  key : string;
  value : any_value option;
}

type instrumentation_scope = {
  name : string;
  version : string;
  attributes : key_value list;
  dropped_attributes_count : int32;
}

let rec default_any_value () : any_value = String_value ("")

and default_array_value 
  ?values:((values:any_value list) = [])
  () : array_value  = {
  values;
}

and default_key_value_list 
  ?values:((values:key_value list) = [])
  () : key_value_list  = {
  values;
}

and default_key_value 
  ?key:((key:string) = "")
  ?value:((value:any_value option) = None)
  () : key_value  = {
  key;
  value;
}

let rec default_instrumentation_scope 
  ?name:((name:string) = "")
  ?version:((version:string) = "")
  ?attributes:((attributes:key_value list) = [])
  ?dropped_attributes_count:((dropped_attributes_count:int32) = 0l)
  () : instrumentation_scope  = {
  name;
  version;
  attributes;
  dropped_attributes_count;
}

type array_value_mutable = {
  mutable values : any_value list;
}

let default_array_value_mutable () : array_value_mutable = {
  values = [];
}

type key_value_list_mutable = {
  mutable values : key_value list;
}

let default_key_value_list_mutable () : key_value_list_mutable = {
  values = [];
}

type key_value_mutable = {
  mutable key : string;
  mutable value : any_value option;
}

let default_key_value_mutable () : key_value_mutable = {
  key = "";
  value = None;
}

type instrumentation_scope_mutable = {
  mutable name : string;
  mutable version : string;
  mutable attributes : key_value list;
  mutable dropped_attributes_count : int32;
}

let default_instrumentation_scope_mutable () : instrumentation_scope_mutable = {
  name = "";
  version = "";
  attributes = [];
  dropped_attributes_count = 0l;
}


(** {2 Make functions} *)


let rec make_array_value 
  ~(values:any_value list)
  () : array_value  = {
  values;
}

and make_key_value_list 
  ~(values:key_value list)
  () : key_value_list  = {
  values;
}

and make_key_value 
  ~(key:string)
  ?value:((value:any_value option) = None)
  () : key_value  = {
  key;
  value;
}

let rec make_instrumentation_scope 
  ~(name:string)
  ~(version:string)
  ~(attributes:key_value list)
  ~(dropped_attributes_count:int32)
  () : instrumentation_scope  = {
  name;
  version;
  attributes;
  dropped_attributes_count;
}

[@@@ocaml.warning "-27-30-39"]

(** {2 Formatters} *)

let rec pp_any_value fmt (v:any_value) =
  match v with
  | String_value x -> Format.fprintf fmt "@[<hv2>String_value(@,%a)@]" Pbrt.Pp.pp_string x
  | Bool_value x -> Format.fprintf fmt "@[<hv2>Bool_value(@,%a)@]" Pbrt.Pp.pp_bool x
  | Int_value x -> Format.fprintf fmt "@[<hv2>Int_value(@,%a)@]" Pbrt.Pp.pp_int64 x
  | Double_value x -> Format.fprintf fmt "@[<hv2>Double_value(@,%a)@]" Pbrt.Pp.pp_float x
  | Array_value x -> Format.fprintf fmt "@[<hv2>Array_value(@,%a)@]" pp_array_value x
  | Kvlist_value x -> Format.fprintf fmt "@[<hv2>Kvlist_value(@,%a)@]" pp_key_value_list x
  | Bytes_value x -> Format.fprintf fmt "@[<hv2>Bytes_value(@,%a)@]" Pbrt.Pp.pp_bytes x

and pp_array_value fmt (v:array_value) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "values" (Pbrt.Pp.pp_list pp_any_value) fmt v.values;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

and pp_key_value_list fmt (v:key_value_list) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "values" (Pbrt.Pp.pp_list pp_key_value) fmt v.values;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

and pp_key_value fmt (v:key_value) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "key" Pbrt.Pp.pp_string fmt v.key;
    Pbrt.Pp.pp_record_field ~first:false "value" (Pbrt.Pp.pp_option pp_any_value) fmt v.value;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_instrumentation_scope fmt (v:instrumentation_scope) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "name" Pbrt.Pp.pp_string fmt v.name;
    Pbrt.Pp.pp_record_field ~first:false "version" Pbrt.Pp.pp_string fmt v.version;
    Pbrt.Pp.pp_record_field ~first:false "attributes" (Pbrt.Pp.pp_list pp_key_value) fmt v.attributes;
    Pbrt.Pp.pp_record_field ~first:false "dropped_attributes_count" Pbrt.Pp.pp_int32 fmt v.dropped_attributes_count;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Encoding} *)

let rec encode_pb_any_value (v:any_value) encoder = 
  begin match v with
  | String_value x ->
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  | Bool_value x ->
    Pbrt.Encoder.bool x encoder;
    Pbrt.Encoder.key 2 Pbrt.Varint encoder; 
  | Int_value x ->
    Pbrt.Encoder.int64_as_varint x encoder;
    Pbrt.Encoder.key 3 Pbrt.Varint encoder; 
  | Double_value x ->
    Pbrt.Encoder.float_as_bits64 x encoder;
    Pbrt.Encoder.key 4 Pbrt.Bits64 encoder; 
  | Array_value x ->
    Pbrt.Encoder.nested encode_pb_array_value x encoder;
    Pbrt.Encoder.key 5 Pbrt.Bytes encoder; 
  | Kvlist_value x ->
    Pbrt.Encoder.nested encode_pb_key_value_list x encoder;
    Pbrt.Encoder.key 6 Pbrt.Bytes encoder; 
  | Bytes_value x ->
    Pbrt.Encoder.bytes x encoder;
    Pbrt.Encoder.key 7 Pbrt.Bytes encoder; 
  end

and encode_pb_array_value (v:array_value) encoder = 
  Pbrt.List_util.rev_iter_with (fun x encoder -> 
    Pbrt.Encoder.nested encode_pb_any_value x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  ) v.values encoder;
  ()

and encode_pb_key_value_list (v:key_value_list) encoder = 
  Pbrt.List_util.rev_iter_with (fun x encoder -> 
    Pbrt.Encoder.nested encode_pb_key_value x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  ) v.values encoder;
  ()

and encode_pb_key_value (v:key_value) encoder = 
  Pbrt.Encoder.string v.key encoder;
  Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  begin match v.value with
  | Some x -> 
    Pbrt.Encoder.nested encode_pb_any_value x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  ()

let rec encode_pb_instrumentation_scope (v:instrumentation_scope) encoder = 
  Pbrt.Encoder.string v.name encoder;
  Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  Pbrt.Encoder.string v.version encoder;
  Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  Pbrt.List_util.rev_iter_with (fun x encoder -> 
    Pbrt.Encoder.nested encode_pb_key_value x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  ) v.attributes encoder;
  Pbrt.Encoder.int32_as_varint v.dropped_attributes_count encoder;
  Pbrt.Encoder.key 4 Pbrt.Varint encoder; 
  ()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Decoding} *)

let rec decode_pb_any_value d = 
  let rec loop () = 
    let ret:any_value = match Pbrt.Decoder.key d with
      | None -> Pbrt.Decoder.malformed_variant "any_value"
      | Some (1, _) -> (String_value (Pbrt.Decoder.string d) : any_value) 
      | Some (2, _) -> (Bool_value (Pbrt.Decoder.bool d) : any_value) 
      | Some (3, _) -> (Int_value (Pbrt.Decoder.int64_as_varint d) : any_value) 
      | Some (4, _) -> (Double_value (Pbrt.Decoder.float_as_bits64 d) : any_value) 
      | Some (5, _) -> (Array_value (decode_pb_array_value (Pbrt.Decoder.nested d)) : any_value) 
      | Some (6, _) -> (Kvlist_value (decode_pb_key_value_list (Pbrt.Decoder.nested d)) : any_value) 
      | Some (7, _) -> (Bytes_value (Pbrt.Decoder.bytes d) : any_value) 
      | Some (n, payload_kind) -> (
        Pbrt.Decoder.skip d payload_kind; 
        loop () 
      )
    in
    ret
  in
  loop ()

and decode_pb_array_value d =
  let v = default_array_value_mutable () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      v.values <- List.rev v.values;
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.values <- (decode_pb_any_value (Pbrt.Decoder.nested d)) :: v.values;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(array_value), field(1)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  ({
    values = v.values;
  } : array_value)

and decode_pb_key_value_list d =
  let v = default_key_value_list_mutable () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      v.values <- List.rev v.values;
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.values <- (decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.values;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(key_value_list), field(1)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  ({
    values = v.values;
  } : key_value_list)

and decode_pb_key_value d =
  let v = default_key_value_mutable () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.key <- Pbrt.Decoder.string d;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(key_value), field(1)" pk
    | Some (2, Pbrt.Bytes) -> begin
      v.value <- Some (decode_pb_any_value (Pbrt.Decoder.nested d));
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(key_value), field(2)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  ({
    key = v.key;
    value = v.value;
  } : key_value)

let rec decode_pb_instrumentation_scope d =
  let v = default_instrumentation_scope_mutable () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      v.attributes <- List.rev v.attributes;
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.name <- Pbrt.Decoder.string d;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(instrumentation_scope), field(1)" pk
    | Some (2, Pbrt.Bytes) -> begin
      v.version <- Pbrt.Decoder.string d;
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(instrumentation_scope), field(2)" pk
    | Some (3, Pbrt.Bytes) -> begin
      v.attributes <- (decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.attributes;
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(instrumentation_scope), field(3)" pk
    | Some (4, Pbrt.Varint) -> begin
      v.dropped_attributes_count <- Pbrt.Decoder.int32_as_varint d;
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(instrumentation_scope), field(4)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  ({
    name = v.name;
    version = v.version;
    attributes = v.attributes;
    dropped_attributes_count = v.dropped_attributes_count;
  } : instrumentation_scope)
OCaml

Innovation. Community. Security.