xs/encode.xs

filter: Statementstmt 85.2% 29 / 34 Branchbran 55.5% 20 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.0% 49 / 70 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_consume.h ↑ Summary include/otel_b3.h »
1 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Encode
2  
3 PROTOTYPES: DISABLE
4  
5 # ExportTraceServiceRequest as protobuf bytes. Takes the payload hashref
6 # documented in Punk::OpenTelemetry::OTLP and returns a byte string.
7 SV *
8 traces_protobuf(payload)
9 SV *payload
10 CODE:
11 {
12 11 ▶ if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 11 0 11
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 11 0 11
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 0 11 11
13 0 croak("Punk::OpenTelemetry::Encode::traces_protobuf: "
14 "expected a hashref payload");
15 11 RETVAL = otel_encode_traces(aTHX_ (HV *)SvRV(payload));
16 }
17 OUTPUT:
18 RETVAL
19  
20 # The size the encoder MEASURED for the same payload, without writing it.
21 # Every message is length-delimited, so the encoder walks twice - once to
22 # measure, once to write - and the two must agree exactly. This exposes the
23 # measuring half so a test can assert that agreement directly rather than
24 # inferring it from bytes that happen to parse.
25 UV
26 traces_protobuf_size(payload)
27 SV *payload
28 CODE:
29 {
30 HV *h;
31 AV *rss;
32 SSize_t i, cnt;
33 2 UV total = 0;
34 2 ▶ if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 2 0 2
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 2 0 2
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 0 2 2
35 0 croak("Punk::OpenTelemetry::Encode::traces_protobuf_size: "
36 "expected a hashref payload");
37 2 h = (HV *)SvRV(payload);
38 2 rss = otel_h_av(aTHX_ h, "resource_spans");
39 2 ▶ cnt = rss ? av_len(rss) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
cnt = rss ? av_len(rss) + 1 : 0; 2 0 2
40 4 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 2 2 4
41 2 SV **e = av_fetch(rss, i, 0);
42 2 ▶ total += otel_pb_msg_size(PB_EXPORT_TRACE_RESOURCE_SPANS,
Branch: 1 branch
BranchTrueFalseTotal
total += otel_pb_msg_size(PB_EXPORT_TRACE_RESOURCE_SPANS, 2 0 2
43 otel_resourcespans_size(aTHX_
44 otel_hv_of(aTHX_ (e ? *e : NULL))));
45 }
46 2 ▶ RETVAL = total;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = total; 0 2 2
47 }
48 OUTPUT:
49 RETVAL
50  
51 # The same request as OTLP/JSON bytes. A supported transport, and the one
52 # people paste into a bug report - so it is not a naive rendering of the
53 # protobuf tree: field names are lowerCamelCase, trace and span ids are hex
54 # rather than base64, 64-bit integers are strings, and enums are names. See
55 # otel_json.h for why each of those is load-bearing.
56 SV *
57 traces_json(payload)
58 SV *payload
59 CODE:
60 {
61 SV *tree;
62 7 ▶ if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 7 0 7
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 7 0 7
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 0 7 7
63 0 croak("Punk::OpenTelemetry::Encode::traces_json: "
64 "expected a hashref payload");
65 frj_opts o;
66 7 Zero(&o, 1, frj_opts);
67 /* sorted keys: JSON object order carries no meaning to a collector,
68 * but it carries a great deal to a person diffing two payloads, a
69 * golden vector, and anything that caches or fingerprints a body.
70 * The protobuf side is byte-reproducible for the same reason. */
71 7 o.sort_keys = 1;
72 7 tree = sv_2mortal(otel_json_traces_sv(aTHX_ (HV *)SvRV(payload)));
73 7 RETVAL = otel_frj(aTHX)->encode(aTHX_ tree, &o);
74 }
75 OUTPUT:
76 RETVAL
77  
78 # The OTLP/JSON tree as Perl data, before it is serialised. The tests assert
79 # the mapping rules against this rather than against parsed JSON, so a failure
80 # names the field that is wrong instead of a diff of two long strings.
81 SV *
82 traces_json_tree(payload)
83 SV *payload
84 CODE:
85 {
86 20 ▶ if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 20 0 20
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 20 0 20
if (!(payload && SvROK(payload) && SvTYPE(SvRV(payload)) == SVt_PVHV)) 0 20 20
87 0 croak("Punk::OpenTelemetry::Encode::traces_json_tree: "
88 "expected a hashref payload");
89 20 RETVAL = otel_json_traces_sv(aTHX_ (HV *)SvRV(payload));
90 }
91 OUTPUT:
92 RETVAL
93  
94 # ---- the primitives, exposed for the golden vectors -------------------------
95 # Not public API. A varint is the whole of protobuf that can be wrong in a way
96 # every higher level inherits, so it is tested directly rather than only
97 # through a message that happens to decode.
98  
99 SV *
100 _varint(v)
101 UV v
102 CODE:
103 {
104 otel_buf b;
105 10 otel_buf_init(&b, 16);
106 10 otel_pb_varint(&b, v);
107 10 RETVAL = newSVpvn(b.buf, b.len);
108 10 otel_buf_free(&b);
109 }
110 OUTPUT:
111 RETVAL
112  
113 UV
114 _varint_size(v)
115 UV v
116 CODE:
117 9 RETVAL = (UV)otel_pb_varint_size(v);
118 OUTPUT:
119 RETVAL
120  
121 # One AnyValue body, for the scalar-shape tests: the same code every attribute
122 # in every signal goes through, reachable without building a whole span.
123 SV *
124 _anyvalue(v)
125 SV *v
126 CODE:
127 {
128 otel_buf b;
129 28 size_t want = otel_anyvalue_body_size(aTHX_ v);
130 28 ▶ otel_buf_init(&b, want ? want : 16);
Branch: 1 branch
BranchTrueFalseTotal
otel_buf_init(&b, want ? want : 16); 26 2 28
131 28 otel_anyvalue_body_write(aTHX_ &b, v);
132 28 ▶ if (b.len != want)
Branch: 1 branch
BranchTrueFalseTotal
if (b.len != want) 0 28 28
133 0 croak("Punk::OpenTelemetry: AnyValue wrote %lu bytes, measured "
134 "%lu", (unsigned long)b.len, (unsigned long)want);
135 28 RETVAL = newSVpvn(b.buf, b.len);
136 28 otel_buf_free(&b);
137 }
138 OUTPUT:
139 RETVAL
« include/otel_consume.h ↑ Summary include/otel_b3.h »