Source file trace.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
[@@@ocaml.warning "-23-27-30-39-44"]

type span_span_kind =
  | Span_kind_unspecified 
  | Span_kind_internal 
  | Span_kind_server 
  | Span_kind_client 
  | Span_kind_producer 
  | Span_kind_consumer 

type span_event = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 3 fields *)
  mutable time_unix_nano : int64;
  mutable name : string;
  mutable attributes : Common.key_value list;
  mutable dropped_attributes_count : int32;
}

type span_link = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 5 fields *)
  mutable trace_id : bytes;
  mutable span_id : bytes;
  mutable trace_state : string;
  mutable attributes : Common.key_value list;
  mutable dropped_attributes_count : int32;
  mutable flags : int32;
}

type status_status_code =
  | Status_code_unset 
  | Status_code_ok 
  | Status_code_error 

type status = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 2 fields *)
  mutable message : string;
  mutable code : status_status_code;
}

type span = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 12 fields *)
  mutable trace_id : bytes;
  mutable span_id : bytes;
  mutable trace_state : string;
  mutable parent_span_id : bytes;
  mutable flags : int32;
  mutable name : string;
  mutable kind : span_span_kind;
  mutable start_time_unix_nano : int64;
  mutable end_time_unix_nano : int64;
  mutable attributes : Common.key_value list;
  mutable dropped_attributes_count : int32;
  mutable events : span_event list;
  mutable dropped_events_count : int32;
  mutable links : span_link list;
  mutable dropped_links_count : int32;
  mutable status : status option;
}

type scope_spans = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 1 fields *)
  mutable scope : Common.instrumentation_scope option;
  mutable spans : span list;
  mutable schema_url : string;
}

type resource_spans = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 1 fields *)
  mutable resource : Resource.resource option;
  mutable scope_spans : scope_spans list;
  mutable schema_url : string;
}

type traces_data = {
  mutable resource_spans : resource_spans list;
}

type span_flags =
  | Span_flags_do_not_use 
  | Span_flags_trace_flags_mask 
  | Span_flags_context_has_is_remote_mask 
  | Span_flags_context_is_remote_mask 

let default_span_span_kind () = (Span_kind_unspecified:span_span_kind)

let default_span_event (): span_event =
{
  _presence=Pbrt.Bitfield.empty;
  time_unix_nano=0L;
  name="";
  attributes=[];
  dropped_attributes_count=0l;
}

let default_span_link (): span_link =
{
  _presence=Pbrt.Bitfield.empty;
  trace_id=Bytes.create 0;
  span_id=Bytes.create 0;
  trace_state="";
  attributes=[];
  dropped_attributes_count=0l;
  flags=0l;
}

let default_status_status_code () = (Status_code_unset:status_status_code)

let default_status (): status =
{
  _presence=Pbrt.Bitfield.empty;
  message="";
  code=default_status_status_code ();
}

let default_span (): span =
{
  _presence=Pbrt.Bitfield.empty;
  trace_id=Bytes.create 0;
  span_id=Bytes.create 0;
  trace_state="";
  parent_span_id=Bytes.create 0;
  flags=0l;
  name="";
  kind=default_span_span_kind ();
  start_time_unix_nano=0L;
  end_time_unix_nano=0L;
  attributes=[];
  dropped_attributes_count=0l;
  events=[];
  dropped_events_count=0l;
  links=[];
  dropped_links_count=0l;
  status=None;
}

let default_scope_spans (): scope_spans =
{
  _presence=Pbrt.Bitfield.empty;
  scope=None;
  spans=[];
  schema_url="";
}

let default_resource_spans (): resource_spans =
{
  _presence=Pbrt.Bitfield.empty;
  resource=None;
  scope_spans=[];
  schema_url="";
}

let default_traces_data (): traces_data =
{
  resource_spans=[];
}

let default_span_flags () = (Span_flags_do_not_use:span_flags)


(** {2 Make functions} *)

let[@inline] span_event_has_time_unix_nano (self:span_event) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] span_event_has_name (self:span_event) : bool = (Pbrt.Bitfield.get self._presence 1)
let[@inline] span_event_has_dropped_attributes_count (self:span_event) : bool = (Pbrt.Bitfield.get self._presence 2)

let[@inline] span_event_set_time_unix_nano (self:span_event) (x:int64) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.time_unix_nano <- x
let[@inline] span_event_set_name (self:span_event) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.name <- x
let[@inline] span_event_set_attributes (self:span_event) (x:Common.key_value list) : unit =
  self.attributes <- x
let[@inline] span_event_set_dropped_attributes_count (self:span_event) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 2); self.dropped_attributes_count <- x

let copy_span_event (self:span_event) : span_event =
  { self with time_unix_nano = self.time_unix_nano }

let make_span_event 
  ?(time_unix_nano:int64 option)
  ?(name:string option)
  ?(attributes=[])
  ?(dropped_attributes_count:int32 option)
  () : span_event  =
  let _res = default_span_event () in
  (match time_unix_nano with
  | None -> ()
  | Some v -> span_event_set_time_unix_nano _res v);
  (match name with
  | None -> ()
  | Some v -> span_event_set_name _res v);
  span_event_set_attributes _res attributes;
  (match dropped_attributes_count with
  | None -> ()
  | Some v -> span_event_set_dropped_attributes_count _res v);
  _res

let[@inline] span_link_has_trace_id (self:span_link) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] span_link_has_span_id (self:span_link) : bool = (Pbrt.Bitfield.get self._presence 1)
let[@inline] span_link_has_trace_state (self:span_link) : bool = (Pbrt.Bitfield.get self._presence 2)
let[@inline] span_link_has_dropped_attributes_count (self:span_link) : bool = (Pbrt.Bitfield.get self._presence 3)
let[@inline] span_link_has_flags (self:span_link) : bool = (Pbrt.Bitfield.get self._presence 4)

let[@inline] span_link_set_trace_id (self:span_link) (x:bytes) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.trace_id <- x
let[@inline] span_link_set_span_id (self:span_link) (x:bytes) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.span_id <- x
let[@inline] span_link_set_trace_state (self:span_link) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 2); self.trace_state <- x
let[@inline] span_link_set_attributes (self:span_link) (x:Common.key_value list) : unit =
  self.attributes <- x
let[@inline] span_link_set_dropped_attributes_count (self:span_link) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 3); self.dropped_attributes_count <- x
let[@inline] span_link_set_flags (self:span_link) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 4); self.flags <- x

let copy_span_link (self:span_link) : span_link =
  { self with trace_id = self.trace_id }

let make_span_link 
  ?(trace_id:bytes option)
  ?(span_id:bytes option)
  ?(trace_state:string option)
  ?(attributes=[])
  ?(dropped_attributes_count:int32 option)
  ?(flags:int32 option)
  () : span_link  =
  let _res = default_span_link () in
  (match trace_id with
  | None -> ()
  | Some v -> span_link_set_trace_id _res v);
  (match span_id with
  | None -> ()
  | Some v -> span_link_set_span_id _res v);
  (match trace_state with
  | None -> ()
  | Some v -> span_link_set_trace_state _res v);
  span_link_set_attributes _res attributes;
  (match dropped_attributes_count with
  | None -> ()
  | Some v -> span_link_set_dropped_attributes_count _res v);
  (match flags with
  | None -> ()
  | Some v -> span_link_set_flags _res v);
  _res

let[@inline] status_has_message (self:status) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] status_has_code (self:status) : bool = (Pbrt.Bitfield.get self._presence 1)

let[@inline] status_set_message (self:status) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.message <- x
let[@inline] status_set_code (self:status) (x:status_status_code) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.code <- x

let copy_status (self:status) : status =
  { self with message = self.message }

let make_status 
  ?(message:string option)
  ?(code:status_status_code option)
  () : status  =
  let _res = default_status () in
  (match message with
  | None -> ()
  | Some v -> status_set_message _res v);
  (match code with
  | None -> ()
  | Some v -> status_set_code _res v);
  _res

let[@inline] span_has_trace_id (self:span) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] span_has_span_id (self:span) : bool = (Pbrt.Bitfield.get self._presence 1)
let[@inline] span_has_trace_state (self:span) : bool = (Pbrt.Bitfield.get self._presence 2)
let[@inline] span_has_parent_span_id (self:span) : bool = (Pbrt.Bitfield.get self._presence 3)
let[@inline] span_has_flags (self:span) : bool = (Pbrt.Bitfield.get self._presence 4)
let[@inline] span_has_name (self:span) : bool = (Pbrt.Bitfield.get self._presence 5)
let[@inline] span_has_kind (self:span) : bool = (Pbrt.Bitfield.get self._presence 6)
let[@inline] span_has_start_time_unix_nano (self:span) : bool = (Pbrt.Bitfield.get self._presence 7)
let[@inline] span_has_end_time_unix_nano (self:span) : bool = (Pbrt.Bitfield.get self._presence 8)
let[@inline] span_has_dropped_attributes_count (self:span) : bool = (Pbrt.Bitfield.get self._presence 9)
let[@inline] span_has_dropped_events_count (self:span) : bool = (Pbrt.Bitfield.get self._presence 10)
let[@inline] span_has_dropped_links_count (self:span) : bool = (Pbrt.Bitfield.get self._presence 11)

let[@inline] span_set_trace_id (self:span) (x:bytes) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.trace_id <- x
let[@inline] span_set_span_id (self:span) (x:bytes) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.span_id <- x
let[@inline] span_set_trace_state (self:span) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 2); self.trace_state <- x
let[@inline] span_set_parent_span_id (self:span) (x:bytes) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 3); self.parent_span_id <- x
let[@inline] span_set_flags (self:span) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 4); self.flags <- x
let[@inline] span_set_name (self:span) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 5); self.name <- x
let[@inline] span_set_kind (self:span) (x:span_span_kind) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 6); self.kind <- x
let[@inline] span_set_start_time_unix_nano (self:span) (x:int64) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 7); self.start_time_unix_nano <- x
let[@inline] span_set_end_time_unix_nano (self:span) (x:int64) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 8); self.end_time_unix_nano <- x
let[@inline] span_set_attributes (self:span) (x:Common.key_value list) : unit =
  self.attributes <- x
let[@inline] span_set_dropped_attributes_count (self:span) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 9); self.dropped_attributes_count <- x
let[@inline] span_set_events (self:span) (x:span_event list) : unit =
  self.events <- x
let[@inline] span_set_dropped_events_count (self:span) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 10); self.dropped_events_count <- x
let[@inline] span_set_links (self:span) (x:span_link list) : unit =
  self.links <- x
let[@inline] span_set_dropped_links_count (self:span) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 11); self.dropped_links_count <- x
let[@inline] span_set_status (self:span) (x:status) : unit =
  self.status <- Some x

let copy_span (self:span) : span =
  { self with trace_id = self.trace_id }

let make_span 
  ?(trace_id:bytes option)
  ?(span_id:bytes option)
  ?(trace_state:string option)
  ?(parent_span_id:bytes option)
  ?(flags:int32 option)
  ?(name:string option)
  ?(kind:span_span_kind option)
  ?(start_time_unix_nano:int64 option)
  ?(end_time_unix_nano:int64 option)
  ?(attributes=[])
  ?(dropped_attributes_count:int32 option)
  ?(events=[])
  ?(dropped_events_count:int32 option)
  ?(links=[])
  ?(dropped_links_count:int32 option)
  ?(status:status option)
  () : span  =
  let _res = default_span () in
  (match trace_id with
  | None -> ()
  | Some v -> span_set_trace_id _res v);
  (match span_id with
  | None -> ()
  | Some v -> span_set_span_id _res v);
  (match trace_state with
  | None -> ()
  | Some v -> span_set_trace_state _res v);
  (match parent_span_id with
  | None -> ()
  | Some v -> span_set_parent_span_id _res v);
  (match flags with
  | None -> ()
  | Some v -> span_set_flags _res v);
  (match name with
  | None -> ()
  | Some v -> span_set_name _res v);
  (match kind with
  | None -> ()
  | Some v -> span_set_kind _res v);
  (match start_time_unix_nano with
  | None -> ()
  | Some v -> span_set_start_time_unix_nano _res v);
  (match end_time_unix_nano with
  | None -> ()
  | Some v -> span_set_end_time_unix_nano _res v);
  span_set_attributes _res attributes;
  (match dropped_attributes_count with
  | None -> ()
  | Some v -> span_set_dropped_attributes_count _res v);
  span_set_events _res events;
  (match dropped_events_count with
  | None -> ()
  | Some v -> span_set_dropped_events_count _res v);
  span_set_links _res links;
  (match dropped_links_count with
  | None -> ()
  | Some v -> span_set_dropped_links_count _res v);
  (match status with
  | None -> ()
  | Some v -> span_set_status _res v);
  _res

let[@inline] scope_spans_has_schema_url (self:scope_spans) : bool = (Pbrt.Bitfield.get self._presence 0)

let[@inline] scope_spans_set_scope (self:scope_spans) (x:Common.instrumentation_scope) : unit =
  self.scope <- Some x
let[@inline] scope_spans_set_spans (self:scope_spans) (x:span list) : unit =
  self.spans <- x
let[@inline] scope_spans_set_schema_url (self:scope_spans) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.schema_url <- x

let copy_scope_spans (self:scope_spans) : scope_spans =
  { self with scope = self.scope }

let make_scope_spans 
  ?(scope:Common.instrumentation_scope option)
  ?(spans=[])
  ?(schema_url:string option)
  () : scope_spans  =
  let _res = default_scope_spans () in
  (match scope with
  | None -> ()
  | Some v -> scope_spans_set_scope _res v);
  scope_spans_set_spans _res spans;
  (match schema_url with
  | None -> ()
  | Some v -> scope_spans_set_schema_url _res v);
  _res

let[@inline] resource_spans_has_schema_url (self:resource_spans) : bool = (Pbrt.Bitfield.get self._presence 0)

let[@inline] resource_spans_set_resource (self:resource_spans) (x:Resource.resource) : unit =
  self.resource <- Some x
let[@inline] resource_spans_set_scope_spans (self:resource_spans) (x:scope_spans list) : unit =
  self.scope_spans <- x
let[@inline] resource_spans_set_schema_url (self:resource_spans) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.schema_url <- x

let copy_resource_spans (self:resource_spans) : resource_spans =
  { self with resource = self.resource }

let make_resource_spans 
  ?(resource:Resource.resource option)
  ?(scope_spans=[])
  ?(schema_url:string option)
  () : resource_spans  =
  let _res = default_resource_spans () in
  (match resource with
  | None -> ()
  | Some v -> resource_spans_set_resource _res v);
  resource_spans_set_scope_spans _res scope_spans;
  (match schema_url with
  | None -> ()
  | Some v -> resource_spans_set_schema_url _res v);
  _res


let[@inline] traces_data_set_resource_spans (self:traces_data) (x:resource_spans list) : unit =
  self.resource_spans <- x

let copy_traces_data (self:traces_data) : traces_data =
  { self with resource_spans = self.resource_spans }

let make_traces_data 
  ?(resource_spans=[])
  () : traces_data  =
  let _res = default_traces_data () in
  traces_data_set_resource_spans _res resource_spans;
  _res

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

(** {2 Formatters} *)

let rec pp_span_span_kind fmt (v:span_span_kind) =
  match v with
  | Span_kind_unspecified -> Format.fprintf fmt "Span_kind_unspecified"
  | Span_kind_internal -> Format.fprintf fmt "Span_kind_internal"
  | Span_kind_server -> Format.fprintf fmt "Span_kind_server"
  | Span_kind_client -> Format.fprintf fmt "Span_kind_client"
  | Span_kind_producer -> Format.fprintf fmt "Span_kind_producer"
  | Span_kind_consumer -> Format.fprintf fmt "Span_kind_consumer"

let rec pp_span_event fmt (v:span_event) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (span_event_has_time_unix_nano v)) ~first:true "time_unix_nano" Pbrt.Pp.pp_int64 fmt v.time_unix_nano;
    Pbrt.Pp.pp_record_field ~absent:(not (span_event_has_name v)) ~first:false "name" Pbrt.Pp.pp_string fmt v.name;
    Pbrt.Pp.pp_record_field ~first:false "attributes" (Pbrt.Pp.pp_list Common.pp_key_value) fmt v.attributes;
    Pbrt.Pp.pp_record_field ~absent:(not (span_event_has_dropped_attributes_count v)) ~first:false "dropped_attributes_count" Pbrt.Pp.pp_int32 fmt v.dropped_attributes_count;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_span_link fmt (v:span_link) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (span_link_has_trace_id v)) ~first:true "trace_id" Pbrt.Pp.pp_bytes fmt v.trace_id;
    Pbrt.Pp.pp_record_field ~absent:(not (span_link_has_span_id v)) ~first:false "span_id" Pbrt.Pp.pp_bytes fmt v.span_id;
    Pbrt.Pp.pp_record_field ~absent:(not (span_link_has_trace_state v)) ~first:false "trace_state" Pbrt.Pp.pp_string fmt v.trace_state;
    Pbrt.Pp.pp_record_field ~first:false "attributes" (Pbrt.Pp.pp_list Common.pp_key_value) fmt v.attributes;
    Pbrt.Pp.pp_record_field ~absent:(not (span_link_has_dropped_attributes_count v)) ~first:false "dropped_attributes_count" Pbrt.Pp.pp_int32 fmt v.dropped_attributes_count;
    Pbrt.Pp.pp_record_field ~absent:(not (span_link_has_flags v)) ~first:false "flags" Pbrt.Pp.pp_int32 fmt v.flags;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_status_status_code fmt (v:status_status_code) =
  match v with
  | Status_code_unset -> Format.fprintf fmt "Status_code_unset"
  | Status_code_ok -> Format.fprintf fmt "Status_code_ok"
  | Status_code_error -> Format.fprintf fmt "Status_code_error"

let rec pp_status fmt (v:status) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (status_has_message v)) ~first:true "message" Pbrt.Pp.pp_string fmt v.message;
    Pbrt.Pp.pp_record_field ~absent:(not (status_has_code v)) ~first:false "code" pp_status_status_code fmt v.code;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_span fmt (v:span) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_trace_id v)) ~first:true "trace_id" Pbrt.Pp.pp_bytes fmt v.trace_id;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_span_id v)) ~first:false "span_id" Pbrt.Pp.pp_bytes fmt v.span_id;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_trace_state v)) ~first:false "trace_state" Pbrt.Pp.pp_string fmt v.trace_state;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_parent_span_id v)) ~first:false "parent_span_id" Pbrt.Pp.pp_bytes fmt v.parent_span_id;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_flags v)) ~first:false "flags" Pbrt.Pp.pp_int32 fmt v.flags;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_name v)) ~first:false "name" Pbrt.Pp.pp_string fmt v.name;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_kind v)) ~first:false "kind" pp_span_span_kind fmt v.kind;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_start_time_unix_nano v)) ~first:false "start_time_unix_nano" Pbrt.Pp.pp_int64 fmt v.start_time_unix_nano;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_end_time_unix_nano v)) ~first:false "end_time_unix_nano" Pbrt.Pp.pp_int64 fmt v.end_time_unix_nano;
    Pbrt.Pp.pp_record_field ~first:false "attributes" (Pbrt.Pp.pp_list Common.pp_key_value) fmt v.attributes;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_dropped_attributes_count v)) ~first:false "dropped_attributes_count" Pbrt.Pp.pp_int32 fmt v.dropped_attributes_count;
    Pbrt.Pp.pp_record_field ~first:false "events" (Pbrt.Pp.pp_list pp_span_event) fmt v.events;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_dropped_events_count v)) ~first:false "dropped_events_count" Pbrt.Pp.pp_int32 fmt v.dropped_events_count;
    Pbrt.Pp.pp_record_field ~first:false "links" (Pbrt.Pp.pp_list pp_span_link) fmt v.links;
    Pbrt.Pp.pp_record_field ~absent:(not (span_has_dropped_links_count v)) ~first:false "dropped_links_count" Pbrt.Pp.pp_int32 fmt v.dropped_links_count;
    Pbrt.Pp.pp_record_field ~first:false "status" (Pbrt.Pp.pp_option pp_status) fmt v.status;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_scope_spans fmt (v:scope_spans) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "scope" (Pbrt.Pp.pp_option Common.pp_instrumentation_scope) fmt v.scope;
    Pbrt.Pp.pp_record_field ~first:false "spans" (Pbrt.Pp.pp_list pp_span) fmt v.spans;
    Pbrt.Pp.pp_record_field ~absent:(not (scope_spans_has_schema_url v)) ~first:false "schema_url" Pbrt.Pp.pp_string fmt v.schema_url;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_resource_spans fmt (v:resource_spans) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "resource" (Pbrt.Pp.pp_option Resource.pp_resource) fmt v.resource;
    Pbrt.Pp.pp_record_field ~first:false "scope_spans" (Pbrt.Pp.pp_list pp_scope_spans) fmt v.scope_spans;
    Pbrt.Pp.pp_record_field ~absent:(not (resource_spans_has_schema_url v)) ~first:false "schema_url" Pbrt.Pp.pp_string fmt v.schema_url;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_traces_data fmt (v:traces_data) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "resource_spans" (Pbrt.Pp.pp_list pp_resource_spans) fmt v.resource_spans;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_span_flags fmt (v:span_flags) =
  match v with
  | Span_flags_do_not_use -> Format.fprintf fmt "Span_flags_do_not_use"
  | Span_flags_trace_flags_mask -> Format.fprintf fmt "Span_flags_trace_flags_mask"
  | Span_flags_context_has_is_remote_mask -> Format.fprintf fmt "Span_flags_context_has_is_remote_mask"
  | Span_flags_context_is_remote_mask -> Format.fprintf fmt "Span_flags_context_is_remote_mask"

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

(** {2 Protobuf Encoding} *)

let rec encode_pb_span_span_kind (v:span_span_kind) encoder =
  match v with
  | Span_kind_unspecified -> Pbrt.Encoder.int_as_varint (0) encoder
  | Span_kind_internal -> Pbrt.Encoder.int_as_varint 1 encoder
  | Span_kind_server -> Pbrt.Encoder.int_as_varint 2 encoder
  | Span_kind_client -> Pbrt.Encoder.int_as_varint 3 encoder
  | Span_kind_producer -> Pbrt.Encoder.int_as_varint 4 encoder
  | Span_kind_consumer -> Pbrt.Encoder.int_as_varint 5 encoder

let rec encode_pb_span_event (v:span_event) encoder = 
  if span_event_has_time_unix_nano v then (
    Pbrt.Encoder.int64_as_bits64 v.time_unix_nano encoder;
    Pbrt.Encoder.key 1 Pbrt.Bits64 encoder; 
  );
  if span_event_has_name v then (
    Pbrt.Encoder.string v.name encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested Common.encode_pb_key_value x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  ) v.attributes encoder;
  if span_event_has_dropped_attributes_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_attributes_count encoder;
    Pbrt.Encoder.key 4 Pbrt.Varint encoder; 
  );
  ()

let rec encode_pb_span_link (v:span_link) encoder = 
  if span_link_has_trace_id v then (
    Pbrt.Encoder.bytes v.trace_id encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  );
  if span_link_has_span_id v then (
    Pbrt.Encoder.bytes v.span_id encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  if span_link_has_trace_state v then (
    Pbrt.Encoder.string v.trace_state encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested Common.encode_pb_key_value x encoder;
    Pbrt.Encoder.key 4 Pbrt.Bytes encoder; 
  ) v.attributes encoder;
  if span_link_has_dropped_attributes_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_attributes_count encoder;
    Pbrt.Encoder.key 5 Pbrt.Varint encoder; 
  );
  if span_link_has_flags v then (
    Pbrt.Encoder.int32_as_bits32 v.flags encoder;
    Pbrt.Encoder.key 6 Pbrt.Bits32 encoder; 
  );
  ()

let rec encode_pb_status_status_code (v:status_status_code) encoder =
  match v with
  | Status_code_unset -> Pbrt.Encoder.int_as_varint (0) encoder
  | Status_code_ok -> Pbrt.Encoder.int_as_varint 1 encoder
  | Status_code_error -> Pbrt.Encoder.int_as_varint 2 encoder

let rec encode_pb_status (v:status) encoder = 
  if status_has_message v then (
    Pbrt.Encoder.string v.message encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  if status_has_code v then (
    encode_pb_status_status_code v.code encoder;
    Pbrt.Encoder.key 3 Pbrt.Varint encoder; 
  );
  ()

let rec encode_pb_span (v:span) encoder = 
  if span_has_trace_id v then (
    Pbrt.Encoder.bytes v.trace_id encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  );
  if span_has_span_id v then (
    Pbrt.Encoder.bytes v.span_id encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  if span_has_trace_state v then (
    Pbrt.Encoder.string v.trace_state encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  );
  if span_has_parent_span_id v then (
    Pbrt.Encoder.bytes v.parent_span_id encoder;
    Pbrt.Encoder.key 4 Pbrt.Bytes encoder; 
  );
  if span_has_flags v then (
    Pbrt.Encoder.int32_as_bits32 v.flags encoder;
    Pbrt.Encoder.key 16 Pbrt.Bits32 encoder; 
  );
  if span_has_name v then (
    Pbrt.Encoder.string v.name encoder;
    Pbrt.Encoder.key 5 Pbrt.Bytes encoder; 
  );
  if span_has_kind v then (
    encode_pb_span_span_kind v.kind encoder;
    Pbrt.Encoder.key 6 Pbrt.Varint encoder; 
  );
  if span_has_start_time_unix_nano v then (
    Pbrt.Encoder.int64_as_bits64 v.start_time_unix_nano encoder;
    Pbrt.Encoder.key 7 Pbrt.Bits64 encoder; 
  );
  if span_has_end_time_unix_nano v then (
    Pbrt.Encoder.int64_as_bits64 v.end_time_unix_nano encoder;
    Pbrt.Encoder.key 8 Pbrt.Bits64 encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested Common.encode_pb_key_value x encoder;
    Pbrt.Encoder.key 9 Pbrt.Bytes encoder; 
  ) v.attributes encoder;
  if span_has_dropped_attributes_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_attributes_count encoder;
    Pbrt.Encoder.key 10 Pbrt.Varint encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_span_event x encoder;
    Pbrt.Encoder.key 11 Pbrt.Bytes encoder; 
  ) v.events encoder;
  if span_has_dropped_events_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_events_count encoder;
    Pbrt.Encoder.key 12 Pbrt.Varint encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_span_link x encoder;
    Pbrt.Encoder.key 13 Pbrt.Bytes encoder; 
  ) v.links encoder;
  if span_has_dropped_links_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_links_count encoder;
    Pbrt.Encoder.key 14 Pbrt.Varint encoder; 
  );
  begin match v.status with
  | Some x -> 
    Pbrt.Encoder.nested encode_pb_status x encoder;
    Pbrt.Encoder.key 15 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  ()

let rec encode_pb_scope_spans (v:scope_spans) encoder = 
  begin match v.scope with
  | Some x -> 
    Pbrt.Encoder.nested Common.encode_pb_instrumentation_scope x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_span x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  ) v.spans encoder;
  if scope_spans_has_schema_url v then (
    Pbrt.Encoder.string v.schema_url encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  );
  ()

let rec encode_pb_resource_spans (v:resource_spans) encoder = 
  begin match v.resource with
  | Some x -> 
    Pbrt.Encoder.nested Resource.encode_pb_resource x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_scope_spans x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  ) v.scope_spans encoder;
  if resource_spans_has_schema_url v then (
    Pbrt.Encoder.string v.schema_url encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  );
  ()

let rec encode_pb_traces_data (v:traces_data) encoder = 
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_resource_spans x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  ) v.resource_spans encoder;
  ()

let rec encode_pb_span_flags (v:span_flags) encoder =
  match v with
  | Span_flags_do_not_use -> Pbrt.Encoder.int_as_varint (0) encoder
  | Span_flags_trace_flags_mask -> Pbrt.Encoder.int_as_varint 255 encoder
  | Span_flags_context_has_is_remote_mask -> Pbrt.Encoder.int_as_varint 256 encoder
  | Span_flags_context_is_remote_mask -> Pbrt.Encoder.int_as_varint 512 encoder

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

(** {2 Protobuf Decoding} *)

let rec decode_pb_span_span_kind d : span_span_kind = 
  match Pbrt.Decoder.int_as_varint d with
  | 0 -> Span_kind_unspecified
  | 1 -> Span_kind_internal
  | 2 -> Span_kind_server
  | 3 -> Span_kind_client
  | 4 -> Span_kind_producer
  | 5 -> Span_kind_consumer
  | _ -> Pbrt.Decoder.malformed_variant "span_span_kind"

let rec decode_pb_span_event d =
  let v = default_span_event () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      span_event_set_attributes v (List.rev v.attributes);
    ); continue__ := false
    | Some (1, Pbrt.Bits64) -> begin
      span_event_set_time_unix_nano v (Pbrt.Decoder.int64_as_bits64 d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_event" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      span_event_set_name v (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_event" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      span_event_set_attributes v ((Common.decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.attributes);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_event" 3 pk
    | Some (4, Pbrt.Varint) -> begin
      span_event_set_dropped_attributes_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_event" 4 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : span_event)

let rec decode_pb_span_link d =
  let v = default_span_link () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      span_link_set_attributes v (List.rev v.attributes);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      span_link_set_trace_id v (Pbrt.Decoder.bytes d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      span_link_set_span_id v (Pbrt.Decoder.bytes d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      span_link_set_trace_state v (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 3 pk
    | Some (4, Pbrt.Bytes) -> begin
      span_link_set_attributes v ((Common.decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.attributes);
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 4 pk
    | Some (5, Pbrt.Varint) -> begin
      span_link_set_dropped_attributes_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (5, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 5 pk
    | Some (6, Pbrt.Bits32) -> begin
      span_link_set_flags v (Pbrt.Decoder.int32_as_bits32 d);
    end
    | Some (6, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span_link" 6 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : span_link)

let rec decode_pb_status_status_code d : status_status_code = 
  match Pbrt.Decoder.int_as_varint d with
  | 0 -> Status_code_unset
  | 1 -> Status_code_ok
  | 2 -> Status_code_error
  | _ -> Pbrt.Decoder.malformed_variant "status_status_code"

let rec decode_pb_status d =
  let v = default_status () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
    ); continue__ := false
    | Some (2, Pbrt.Bytes) -> begin
      status_set_message v (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "status" 2 pk
    | Some (3, Pbrt.Varint) -> begin
      status_set_code v (decode_pb_status_status_code d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "status" 3 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : status)

let rec decode_pb_span d =
  let v = default_span () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      span_set_links v (List.rev v.links);
      span_set_events v (List.rev v.events);
      span_set_attributes v (List.rev v.attributes);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      span_set_trace_id v (Pbrt.Decoder.bytes d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      span_set_span_id v (Pbrt.Decoder.bytes d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      span_set_trace_state v (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 3 pk
    | Some (4, Pbrt.Bytes) -> begin
      span_set_parent_span_id v (Pbrt.Decoder.bytes d);
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 4 pk
    | Some (16, Pbrt.Bits32) -> begin
      span_set_flags v (Pbrt.Decoder.int32_as_bits32 d);
    end
    | Some (16, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 16 pk
    | Some (5, Pbrt.Bytes) -> begin
      span_set_name v (Pbrt.Decoder.string d);
    end
    | Some (5, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 5 pk
    | Some (6, Pbrt.Varint) -> begin
      span_set_kind v (decode_pb_span_span_kind d);
    end
    | Some (6, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 6 pk
    | Some (7, Pbrt.Bits64) -> begin
      span_set_start_time_unix_nano v (Pbrt.Decoder.int64_as_bits64 d);
    end
    | Some (7, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 7 pk
    | Some (8, Pbrt.Bits64) -> begin
      span_set_end_time_unix_nano v (Pbrt.Decoder.int64_as_bits64 d);
    end
    | Some (8, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 8 pk
    | Some (9, Pbrt.Bytes) -> begin
      span_set_attributes v ((Common.decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.attributes);
    end
    | Some (9, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 9 pk
    | Some (10, Pbrt.Varint) -> begin
      span_set_dropped_attributes_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (10, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 10 pk
    | Some (11, Pbrt.Bytes) -> begin
      span_set_events v ((decode_pb_span_event (Pbrt.Decoder.nested d)) :: v.events);
    end
    | Some (11, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 11 pk
    | Some (12, Pbrt.Varint) -> begin
      span_set_dropped_events_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (12, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 12 pk
    | Some (13, Pbrt.Bytes) -> begin
      span_set_links v ((decode_pb_span_link (Pbrt.Decoder.nested d)) :: v.links);
    end
    | Some (13, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 13 pk
    | Some (14, Pbrt.Varint) -> begin
      span_set_dropped_links_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (14, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 14 pk
    | Some (15, Pbrt.Bytes) -> begin
      span_set_status v (decode_pb_status (Pbrt.Decoder.nested d));
    end
    | Some (15, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "span" 15 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : span)

let rec decode_pb_scope_spans d =
  let v = default_scope_spans () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      scope_spans_set_spans v (List.rev v.spans);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      scope_spans_set_scope v (Common.decode_pb_instrumentation_scope (Pbrt.Decoder.nested d));
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "scope_spans" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      scope_spans_set_spans v ((decode_pb_span (Pbrt.Decoder.nested d)) :: v.spans);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "scope_spans" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      scope_spans_set_schema_url v (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "scope_spans" 3 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : scope_spans)

let rec decode_pb_resource_spans d =
  let v = default_resource_spans () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      resource_spans_set_scope_spans v (List.rev v.scope_spans);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      resource_spans_set_resource v (Resource.decode_pb_resource (Pbrt.Decoder.nested d));
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "resource_spans" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      resource_spans_set_scope_spans v ((decode_pb_scope_spans (Pbrt.Decoder.nested d)) :: v.scope_spans);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "resource_spans" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      resource_spans_set_schema_url v (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "resource_spans" 3 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : resource_spans)

let rec decode_pb_traces_data d =
  let v = default_traces_data () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      traces_data_set_resource_spans v (List.rev v.resource_spans);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      traces_data_set_resource_spans v ((decode_pb_resource_spans (Pbrt.Decoder.nested d)) :: v.resource_spans);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "traces_data" 1 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : traces_data)

let rec decode_pb_span_flags d : span_flags = 
  match Pbrt.Decoder.int_as_varint d with
  | 0 -> Span_flags_do_not_use
  | 255 -> Span_flags_trace_flags_mask
  | 256 -> Span_flags_context_has_is_remote_mask
  | 512 -> Span_flags_context_is_remote_mask
  | _ -> Pbrt.Decoder.malformed_variant "span_flags"

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

(** {2 Protobuf YoJson Encoding} *)

let rec encode_json_span_span_kind (v:span_span_kind) = 
  match v with
  | Span_kind_unspecified -> `String "SPAN_KIND_UNSPECIFIED"
  | Span_kind_internal -> `String "SPAN_KIND_INTERNAL"
  | Span_kind_server -> `String "SPAN_KIND_SERVER"
  | Span_kind_client -> `String "SPAN_KIND_CLIENT"
  | Span_kind_producer -> `String "SPAN_KIND_PRODUCER"
  | Span_kind_consumer -> `String "SPAN_KIND_CONSUMER"

let rec encode_json_span_event (v:span_event) = 
  let assoc = ref [] in
  if span_event_has_time_unix_nano v then (
    assoc := ("timeUnixNano", Pbrt_yojson.make_string (Int64.to_string v.time_unix_nano)) :: !assoc;
  );
  if span_event_has_name v then (
    assoc := ("name", Pbrt_yojson.make_string v.name) :: !assoc;
  );
  assoc := (
    let l = v.attributes |> List.map Common.encode_json_key_value in
    ("attributes", `List l) :: !assoc 
  );
  if span_event_has_dropped_attributes_count v then (
    assoc := ("droppedAttributesCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_attributes_count)) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_span_link (v:span_link) = 
  let assoc = ref [] in
  if span_link_has_trace_id v then (
    assoc := ("traceId", Pbrt_yojson.make_bytes v.trace_id) :: !assoc;
  );
  if span_link_has_span_id v then (
    assoc := ("spanId", Pbrt_yojson.make_bytes v.span_id) :: !assoc;
  );
  if span_link_has_trace_state v then (
    assoc := ("traceState", Pbrt_yojson.make_string v.trace_state) :: !assoc;
  );
  assoc := (
    let l = v.attributes |> List.map Common.encode_json_key_value in
    ("attributes", `List l) :: !assoc 
  );
  if span_link_has_dropped_attributes_count v then (
    assoc := ("droppedAttributesCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_attributes_count)) :: !assoc;
  );
  if span_link_has_flags v then (
    assoc := ("flags", Pbrt_yojson.make_int (Int32.to_int v.flags)) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_status_status_code (v:status_status_code) = 
  match v with
  | Status_code_unset -> `String "STATUS_CODE_UNSET"
  | Status_code_ok -> `String "STATUS_CODE_OK"
  | Status_code_error -> `String "STATUS_CODE_ERROR"

let rec encode_json_status (v:status) = 
  let assoc = ref [] in
  if status_has_message v then (
    assoc := ("message", Pbrt_yojson.make_string v.message) :: !assoc;
  );
  if status_has_code v then (
    assoc := ("code", encode_json_status_status_code v.code) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_span (v:span) = 
  let assoc = ref [] in
  if span_has_trace_id v then (
    assoc := ("traceId", Pbrt_yojson.make_bytes v.trace_id) :: !assoc;
  );
  if span_has_span_id v then (
    assoc := ("spanId", Pbrt_yojson.make_bytes v.span_id) :: !assoc;
  );
  if span_has_trace_state v then (
    assoc := ("traceState", Pbrt_yojson.make_string v.trace_state) :: !assoc;
  );
  if span_has_parent_span_id v then (
    assoc := ("parentSpanId", Pbrt_yojson.make_bytes v.parent_span_id) :: !assoc;
  );
  if span_has_flags v then (
    assoc := ("flags", Pbrt_yojson.make_int (Int32.to_int v.flags)) :: !assoc;
  );
  if span_has_name v then (
    assoc := ("name", Pbrt_yojson.make_string v.name) :: !assoc;
  );
  if span_has_kind v then (
    assoc := ("kind", encode_json_span_span_kind v.kind) :: !assoc;
  );
  if span_has_start_time_unix_nano v then (
    assoc := ("startTimeUnixNano", Pbrt_yojson.make_string (Int64.to_string v.start_time_unix_nano)) :: !assoc;
  );
  if span_has_end_time_unix_nano v then (
    assoc := ("endTimeUnixNano", Pbrt_yojson.make_string (Int64.to_string v.end_time_unix_nano)) :: !assoc;
  );
  assoc := (
    let l = v.attributes |> List.map Common.encode_json_key_value in
    ("attributes", `List l) :: !assoc 
  );
  if span_has_dropped_attributes_count v then (
    assoc := ("droppedAttributesCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_attributes_count)) :: !assoc;
  );
  assoc := (
    let l = v.events |> List.map encode_json_span_event in
    ("events", `List l) :: !assoc 
  );
  if span_has_dropped_events_count v then (
    assoc := ("droppedEventsCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_events_count)) :: !assoc;
  );
  assoc := (
    let l = v.links |> List.map encode_json_span_link in
    ("links", `List l) :: !assoc 
  );
  if span_has_dropped_links_count v then (
    assoc := ("droppedLinksCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_links_count)) :: !assoc;
  );
  assoc := (match v.status with
    | None -> !assoc
    | Some v -> ("status", encode_json_status v) :: !assoc);
  `Assoc !assoc

let rec encode_json_scope_spans (v:scope_spans) = 
  let assoc = ref [] in
  assoc := (match v.scope with
    | None -> !assoc
    | Some v -> ("scope", Common.encode_json_instrumentation_scope v) :: !assoc);
  assoc := (
    let l = v.spans |> List.map encode_json_span in
    ("spans", `List l) :: !assoc 
  );
  if scope_spans_has_schema_url v then (
    assoc := ("schemaUrl", Pbrt_yojson.make_string v.schema_url) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_resource_spans (v:resource_spans) = 
  let assoc = ref [] in
  assoc := (match v.resource with
    | None -> !assoc
    | Some v -> ("resource", Resource.encode_json_resource v) :: !assoc);
  assoc := (
    let l = v.scope_spans |> List.map encode_json_scope_spans in
    ("scopeSpans", `List l) :: !assoc 
  );
  if resource_spans_has_schema_url v then (
    assoc := ("schemaUrl", Pbrt_yojson.make_string v.schema_url) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_traces_data (v:traces_data) = 
  let assoc = ref [] in
  assoc := (
    let l = v.resource_spans |> List.map encode_json_resource_spans in
    ("resourceSpans", `List l) :: !assoc 
  );
  `Assoc !assoc

let rec encode_json_span_flags (v:span_flags) = 
  match v with
  | Span_flags_do_not_use -> `String "SPAN_FLAGS_DO_NOT_USE"
  | Span_flags_trace_flags_mask -> `String "SPAN_FLAGS_TRACE_FLAGS_MASK"
  | Span_flags_context_has_is_remote_mask -> `String "SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK"
  | Span_flags_context_is_remote_mask -> `String "SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK"

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

(** {2 JSON Decoding} *)

let rec decode_json_span_span_kind json =
  match json with
  | `String "SPAN_KIND_UNSPECIFIED" -> (Span_kind_unspecified : span_span_kind)
  | `String "SPAN_KIND_INTERNAL" -> (Span_kind_internal : span_span_kind)
  | `String "SPAN_KIND_SERVER" -> (Span_kind_server : span_span_kind)
  | `String "SPAN_KIND_CLIENT" -> (Span_kind_client : span_span_kind)
  | `String "SPAN_KIND_PRODUCER" -> (Span_kind_producer : span_span_kind)
  | `String "SPAN_KIND_CONSUMER" -> (Span_kind_consumer : span_span_kind)
  | _ -> Pbrt_yojson.E.malformed_variant "span_span_kind"

let rec decode_json_span_event d =
  let v = default_span_event () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("timeUnixNano", json_value) -> 
      span_event_set_time_unix_nano v (Pbrt_yojson.int64 json_value "span_event" "time_unix_nano")
    | ("name", json_value) -> 
      span_event_set_name v (Pbrt_yojson.string json_value "span_event" "name")
    | ("attributes", `List l) -> begin
      span_event_set_attributes v @@ List.map (function
        | json_value -> (Common.decode_json_key_value json_value)
      ) l;
    end
    | ("droppedAttributesCount", json_value) -> 
      span_event_set_dropped_attributes_count v (Pbrt_yojson.int32 json_value "span_event" "dropped_attributes_count")
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    time_unix_nano = v.time_unix_nano;
    name = v.name;
    attributes = v.attributes;
    dropped_attributes_count = v.dropped_attributes_count;
  } : span_event)

let rec decode_json_span_link d =
  let v = default_span_link () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("traceId", json_value) -> 
      span_link_set_trace_id v (Pbrt_yojson.bytes json_value "span_link" "trace_id")
    | ("spanId", json_value) -> 
      span_link_set_span_id v (Pbrt_yojson.bytes json_value "span_link" "span_id")
    | ("traceState", json_value) -> 
      span_link_set_trace_state v (Pbrt_yojson.string json_value "span_link" "trace_state")
    | ("attributes", `List l) -> begin
      span_link_set_attributes v @@ List.map (function
        | json_value -> (Common.decode_json_key_value json_value)
      ) l;
    end
    | ("droppedAttributesCount", json_value) -> 
      span_link_set_dropped_attributes_count v (Pbrt_yojson.int32 json_value "span_link" "dropped_attributes_count")
    | ("flags", json_value) -> 
      span_link_set_flags v (Pbrt_yojson.int32 json_value "span_link" "flags")
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    trace_id = v.trace_id;
    span_id = v.span_id;
    trace_state = v.trace_state;
    attributes = v.attributes;
    dropped_attributes_count = v.dropped_attributes_count;
    flags = v.flags;
  } : span_link)

let rec decode_json_status_status_code json =
  match json with
  | `String "STATUS_CODE_UNSET" -> (Status_code_unset : status_status_code)
  | `String "STATUS_CODE_OK" -> (Status_code_ok : status_status_code)
  | `String "STATUS_CODE_ERROR" -> (Status_code_error : status_status_code)
  | _ -> Pbrt_yojson.E.malformed_variant "status_status_code"

let rec decode_json_status d =
  let v = default_status () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("message", json_value) -> 
      status_set_message v (Pbrt_yojson.string json_value "status" "message")
    | ("code", json_value) -> 
      status_set_code v ((decode_json_status_status_code json_value))
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    message = v.message;
    code = v.code;
  } : status)

let rec decode_json_span d =
  let v = default_span () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("traceId", json_value) -> 
      span_set_trace_id v (Pbrt_yojson.bytes json_value "span" "trace_id")
    | ("spanId", json_value) -> 
      span_set_span_id v (Pbrt_yojson.bytes json_value "span" "span_id")
    | ("traceState", json_value) -> 
      span_set_trace_state v (Pbrt_yojson.string json_value "span" "trace_state")
    | ("parentSpanId", json_value) -> 
      span_set_parent_span_id v (Pbrt_yojson.bytes json_value "span" "parent_span_id")
    | ("flags", json_value) -> 
      span_set_flags v (Pbrt_yojson.int32 json_value "span" "flags")
    | ("name", json_value) -> 
      span_set_name v (Pbrt_yojson.string json_value "span" "name")
    | ("kind", json_value) -> 
      span_set_kind v ((decode_json_span_span_kind json_value))
    | ("startTimeUnixNano", json_value) -> 
      span_set_start_time_unix_nano v (Pbrt_yojson.int64 json_value "span" "start_time_unix_nano")
    | ("endTimeUnixNano", json_value) -> 
      span_set_end_time_unix_nano v (Pbrt_yojson.int64 json_value "span" "end_time_unix_nano")
    | ("attributes", `List l) -> begin
      span_set_attributes v @@ List.map (function
        | json_value -> (Common.decode_json_key_value json_value)
      ) l;
    end
    | ("droppedAttributesCount", json_value) -> 
      span_set_dropped_attributes_count v (Pbrt_yojson.int32 json_value "span" "dropped_attributes_count")
    | ("events", `List l) -> begin
      span_set_events v @@ List.map (function
        | json_value -> (decode_json_span_event json_value)
      ) l;
    end
    | ("droppedEventsCount", json_value) -> 
      span_set_dropped_events_count v (Pbrt_yojson.int32 json_value "span" "dropped_events_count")
    | ("links", `List l) -> begin
      span_set_links v @@ List.map (function
        | json_value -> (decode_json_span_link json_value)
      ) l;
    end
    | ("droppedLinksCount", json_value) -> 
      span_set_dropped_links_count v (Pbrt_yojson.int32 json_value "span" "dropped_links_count")
    | ("status", json_value) -> 
      span_set_status v (decode_json_status json_value)
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    trace_id = v.trace_id;
    span_id = v.span_id;
    trace_state = v.trace_state;
    parent_span_id = v.parent_span_id;
    flags = v.flags;
    name = v.name;
    kind = v.kind;
    start_time_unix_nano = v.start_time_unix_nano;
    end_time_unix_nano = v.end_time_unix_nano;
    attributes = v.attributes;
    dropped_attributes_count = v.dropped_attributes_count;
    events = v.events;
    dropped_events_count = v.dropped_events_count;
    links = v.links;
    dropped_links_count = v.dropped_links_count;
    status = v.status;
  } : span)

let rec decode_json_scope_spans d =
  let v = default_scope_spans () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("scope", json_value) -> 
      scope_spans_set_scope v (Common.decode_json_instrumentation_scope json_value)
    | ("spans", `List l) -> begin
      scope_spans_set_spans v @@ List.map (function
        | json_value -> (decode_json_span json_value)
      ) l;
    end
    | ("schemaUrl", json_value) -> 
      scope_spans_set_schema_url v (Pbrt_yojson.string json_value "scope_spans" "schema_url")
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    scope = v.scope;
    spans = v.spans;
    schema_url = v.schema_url;
  } : scope_spans)

let rec decode_json_resource_spans d =
  let v = default_resource_spans () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("resource", json_value) -> 
      resource_spans_set_resource v (Resource.decode_json_resource json_value)
    | ("scopeSpans", `List l) -> begin
      resource_spans_set_scope_spans v @@ List.map (function
        | json_value -> (decode_json_scope_spans json_value)
      ) l;
    end
    | ("schemaUrl", json_value) -> 
      resource_spans_set_schema_url v (Pbrt_yojson.string json_value "resource_spans" "schema_url")
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    resource = v.resource;
    scope_spans = v.scope_spans;
    schema_url = v.schema_url;
  } : resource_spans)

let rec decode_json_traces_data d =
  let v = default_traces_data () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("resourceSpans", `List l) -> begin
      traces_data_set_resource_spans v @@ List.map (function
        | json_value -> (decode_json_resource_spans json_value)
      ) l;
    end
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    resource_spans = v.resource_spans;
  } : traces_data)

let rec decode_json_span_flags json =
  match json with
  | `String "SPAN_FLAGS_DO_NOT_USE" -> (Span_flags_do_not_use : span_flags)
  | `String "SPAN_FLAGS_TRACE_FLAGS_MASK" -> (Span_flags_trace_flags_mask : span_flags)
  | `String "SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK" -> (Span_flags_context_has_is_remote_mask : span_flags)
  | `String "SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK" -> (Span_flags_context_is_remote_mask : span_flags)
  | _ -> Pbrt_yojson.E.malformed_variant "span_flags"