include/otel_json.h

filter: Statementstmt 100% 179 / 179 Branchbran 72.5% 58 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.5% 237 / 259 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_tracer.h ↑ Summary include/otel_meter.h »
1 /* otel_json.h - OTLP/JSON.
2 *
3 * The debuggable transport, and the one people paste into a bug report. It is
4 * a supported OTLP encoding, not a convenience, so it has to be exactly
5 * right - and it is NOT a naive rendering of the protobuf tree. Four rules
6 * separate JSON that a collector accepts from JSON that merely looks correct:
7 *
8 * 1. FIELD NAMES ARE lowerCamelCase. proto3's JSON mapping renames every
9 * field: start_time_unix_nano becomes startTimeUnixNano. A payload with
10 * snake_case keys parses as a message with every field absent, which is
11 * accepted, stored, and empty.
12 *
13 * 2. trace_id AND span_id ARE HEX, not base64. proto3 maps `bytes` to
14 * base64, and OTLP explicitly overrides it for these two. Base64 ids are
15 * the single most common way a hand-written OTLP/JSON payload silently
16 * joins to nothing.
17 *
18 * 3. 64-BIT INTEGERS ARE STRINGS. proto3 maps int64/uint64/fixed64 to JSON
19 * strings, because IEEE-754 doubles cannot hold them: a nanosecond
20 * timestamp is ~1.7e18 and loses its last two digits as a JSON number.
21 * Not a portability nicety - the timestamps are wrong.
22 *
23 * 4. ENUMS ARE NAMES. SPAN_KIND_SERVER, not 2.
24 *
25 * Values are classified by otel_value_kind, the same function the protobuf
26 * encoder uses, so the two cannot disagree about whether something is an int
27 * or a string. Only the rendering differs.
28 *
29 * The output structure is built as ordinary Perl data and handed to
30 * File::Raw::JSON. This is the debug path, not the hot one; the protobuf
31 * encoder is what runs in production, and duplicating a JSON serialiser to
32 * save allocations on a path nobody benchmarks would be the wrong trade.
33 */
34  
35 #ifndef OTEL_JSON_H
36 #define OTEL_JSON_H
37  
38 #include "otel_proto.h"
39 #include "otel_value.h"
40  
41 /* A 64-bit value as a decimal STRING (rule 3). */
42 38 static SV *otel_json_u64(pTHX_ UV v) {
43 char buf[24];
44 38 int n = my_snprintf(buf, sizeof buf, "%" UVuf, v);
45 38 return newSVpvn(buf, n);
46 }
47  
48 /* Raw bytes as lowercase hex (rule 2). */
49 43 static SV *otel_json_hex(pTHX_ const char *p, STRLEN n) {
50 static const char H[] = "0123456789abcdef";
51 43 SV *out = newSV(n * 2 + 1);
52 STRLEN i;
53 char *d;
54 43 SvPOK_on(out);
55 43 d = SvPVX(out);
56 531 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 488 43 531
57 488 d[i * 2] = H[(unsigned char)p[i] >> 4];
58 488 d[i * 2 + 1] = H[(unsigned char)p[i] & 0xf];
59 }
60 43 SvCUR_set(out, n * 2);
61 43 d[n * 2] = '\0';
62 43 return out;
63 }
64  
65 13 static const char *otel_json_kind_name(IV k) {
66 13 switch (k) {
67 1 case OTEL_KIND_INTERNAL: return "SPAN_KIND_INTERNAL";
68 8 case OTEL_KIND_SERVER: return "SPAN_KIND_SERVER";
69 1 case OTEL_KIND_CLIENT: return "SPAN_KIND_CLIENT";
70 1 case OTEL_KIND_PRODUCER: return "SPAN_KIND_PRODUCER";
71 1 case OTEL_KIND_CONSUMER: return "SPAN_KIND_CONSUMER";
72 1 default: return "SPAN_KIND_UNSPECIFIED";
73 }
74 }
75  
76 10 static const char *otel_json_status_name(IV c) {
77 10 switch (c) {
78 1 case OTEL_STATUS_OK: return "STATUS_CODE_OK";
79 8 case OTEL_STATUS_ERROR: return "STATUS_CODE_ERROR";
80 1 default: return "STATUS_CODE_UNSET";
81 }
82 }
83  
84 /* ---- AnyValue ----------------------------------------------------------- *
85 * { "stringValue": "x" } / { "intValue": "42" } / { "boolValue": true } /
86 * { "arrayValue": { "values": [...] } } / { "kvlistValue": { "values": [...] } }
87 *
88 * Note intValue is a STRING here too: it is an int64 in the schema. */
89 static SV *otel_json_anyvalue(pTHX_ SV *v);
90  
91 31 static AV *otel_json_kvlist(pTHX_ HV *hv) {
92 31 AV *out = newAV();
93 31 AV *keys = otel_attr_keys(aTHX_ hv); /* sorted, as the protobuf side is */
94 31 SSize_t i, n = av_len(keys) + 1;
95 111 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 80 31 111
96 80 SV **kp = av_fetch(keys, i, 0);
97 HE *e;
98 HV *kv;
99 80 ▶ if (!(kp && *kp)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kp && *kp)) continue; 80 0 80
if (!(kp && *kp)) continue; 0 80 80
100 80 e = hv_fetch_ent(hv, *kp, 0, 0);
101 80 kv = newHV();
102 80 (void)hv_stores(kv, "key", newSVsv(*kp));
103 80 ▶ (void)hv_stores(kv, "value",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(kv, "value", 80 0 80
104 otel_json_anyvalue(aTHX_ e ? HeVAL(e) : NULL));
105 80 av_push(out, newRV_noinc((SV *)kv));
106 }
107 31 return out;
108 }
109  
110 95 static SV *otel_json_anyvalue(pTHX_ SV *v) {
111 95 HV *o = newHV();
112 95 switch (otel_value_kind(aTHX_ v)) {
113 7 case OTV_NULL:
114 7 break; /* {} - nothing set */
115 15 case OTV_BOOL:
116 /* a REF to 0 or 1, not a copy of PL_sv_yes: File::Raw::JSON's
117 * boolean convention is the scalar ref, and PL_sv_yes serialises
118 * as the number 1 - which is a different JSON type and a
119 * different thing to a collector */
120 15 (void)hv_stores(o, "boolValue",
121 newRV_noinc(newSViv(SvIVX(SvRV(v)) ? 1 : 0)));
122 15 break;
123 17 case OTV_INT:
124 17 (void)hv_stores(o, "intValue", otel_json_u64(aTHX_ (UV)SvIVX(v)));
125 17 break;
126 8 case OTV_DOUBLE:
127 8 (void)hv_stores(o, "doubleValue", newSVnv(SvNVX(v)));
128 8 break;
129 8 case OTV_ARRAY: {
130 8 AV *src = (AV *)SvRV(v);
131 8 AV *vals = newAV();
132 8 HV *wrap = newHV();
133 8 SSize_t i, n = av_len(src) + 1;
134 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 8 23
135 15 SV **e = av_fetch(src, i, 0);
136 15 ▶ av_push(vals, otel_json_anyvalue(aTHX_ (e ? *e : NULL)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(vals, otel_json_anyvalue(aTHX_ (e ? *e : NULL))); 15 0 15
137 }
138 8 (void)hv_stores(wrap, "values", newRV_noinc((SV *)vals));
139 8 (void)hv_stores(o, "arrayValue", newRV_noinc((SV *)wrap));
140 8 break;
141 }
142 8 case OTV_KVLIST: {
143 8 HV *wrap = newHV();
144 8 (void)hv_stores(wrap, "values",
145 newRV_noinc((SV *)otel_json_kvlist(aTHX_ (HV *)SvRV(v))));
146 8 (void)hv_stores(o, "kvlistValue", newRV_noinc((SV *)wrap));
147 8 break;
148 }
149 32 default: {
150 STRLEN l;
151 32 const char *s = SvPV_const(v, l);
152 32 (void)hv_stores(o, "stringValue", newSVpvn(s, l));
153 32 break;
154 }
155 }
156 95 return newRV_noinc((SV *)o);
157 }
158  
159 /* ---- helpers over the input --------------------------------------------- */
160  
161 57 static void otel_json_attrs(pTHX_ HV *dst, HV *src, const char *key) {
162 57 ▶ if (!src || !HvUSEDKEYS(src)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!src || !HvUSEDKEYS(src)) return; 23 34 57
if (!src || !HvUSEDKEYS(src)) return; 0 23 23
if (!src || !HvUSEDKEYS(src)) return; 0 23 23
163 23 (void)hv_store(dst, key, (I32)strlen(key),
164 newRV_noinc((SV *)otel_json_kvlist(aTHX_ src)), 0);
165 }
166  
167 /* copy a string field across under its lowerCamelCase name */
168 154 static void otel_json_str(pTHX_ HV *dst, const char *jkey, HV *src,
169 const char *skey) {
170 154 SV *v = otel_h(aTHX_ src, skey);
171 154 ▶ if (v) (void)hv_store(dst, jkey, (I32)strlen(jkey), newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_store(dst, jkey, (I32)strlen(jkey), newSVsv(v), 0); 45 109 154
172 154 }
173  
174 /* a nanosecond field, as a decimal string (rule 3) */
175 65 static void otel_json_nanos(pTHX_ HV *dst, const char *jkey, HV *src,
176 const char *skey) {
177 65 SV *v = otel_h(aTHX_ src, skey);
178 65 ▶ if (v) (void)hv_store(dst, jkey, (I32)strlen(jkey),
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_store(dst, jkey, (I32)strlen(jkey), 21 44 65
179 otel_json_u64(aTHX_ SvUV(v)), 0);
180 65 }
181  
182 /* an id field, as hex (rule 2), accepting bytes or hex in */
183 101 static void otel_json_id(pTHX_ HV *dst, const char *jkey, HV *src,
184 const char *skey, STRLEN want) {
185 char scratch[16];
186 STRLEN got;
187 101 const char *p = otel_id(aTHX_ src, skey, want, &got, scratch);
188 101 ▶ if (p) (void)hv_store(dst, jkey, (I32)strlen(jkey),
Branch: 1 branch
BranchTrueFalseTotal
if (p) (void)hv_store(dst, jkey, (I32)strlen(jkey), 43 58 101
189 otel_json_hex(aTHX_ p, got), 0);
190 101 }
191  
192 /* ---- the trace tree ----------------------------------------------------- */
193  
194 29 static SV *otel_json_span(pTHX_ HV *sp) {
195 29 HV *o = newHV();
196 SV *v;
197 AV *av;
198 29 ▶ if (!sp) return newRV_noinc((SV *)o);
Branch: 1 branch
BranchTrueFalseTotal
if (!sp) return newRV_noinc((SV *)o); 0 29 29
199  
200 29 otel_json_id(aTHX_ o, "traceId", sp, "trace_id", 16);
201 29 otel_json_id(aTHX_ o, "spanId", sp, "span_id", 8);
202 29 otel_json_str(aTHX_ o, "traceState", sp, "trace_state");
203 29 otel_json_id(aTHX_ o, "parentSpanId", sp, "parent_span_id", 8);
204 29 otel_json_str(aTHX_ o, "name", sp, "name");
205 29 ▶ if ((v = otel_h(aTHX_ sp, "kind")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_h(aTHX_ sp, "kind"))) 13 16 29
206 13 (void)hv_stores(o, "kind", newSVpv(otel_json_kind_name(SvIV(v)), 0));
207 29 otel_json_nanos(aTHX_ o, "startTimeUnixNano", sp, "start_time_unix_nano");
208 29 otel_json_nanos(aTHX_ o, "endTimeUnixNano", sp, "end_time_unix_nano");
209 29 otel_json_attrs(aTHX_ o, otel_h_hv(aTHX_ sp, "attributes"), "attributes");
210  
211 29 ▶ if ((av = otel_h_av(aTHX_ sp, "events"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((av = otel_h_av(aTHX_ sp, "events"))) { 7 22 29
212 7 AV *out = newAV();
213 7 SSize_t i, n = av_len(av) + 1;
214 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 7 14
215 7 SV **e = av_fetch(av, i, 0);
216 7 ▶ HV *ev = otel_hv_of(aTHX_ (e ? *e : NULL));
Branch: 1 branch
BranchTrueFalseTotal
HV *ev = otel_hv_of(aTHX_ (e ? *e : NULL)); 7 0 7
217 7 HV *jo = newHV();
218 7 ▶ if (ev) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev) { 7 0 7
219 7 otel_json_nanos(aTHX_ jo, "timeUnixNano", ev, "time_unix_nano");
220 7 otel_json_str(aTHX_ jo, "name", ev, "name");
221 7 otel_json_attrs(aTHX_ jo, otel_h_hv(aTHX_ ev, "attributes"),
222 "attributes");
223 }
224 7 av_push(out, newRV_noinc((SV *)jo));
225 }
226 7 (void)hv_stores(o, "events", newRV_noinc((SV *)out));
227 }
228  
229 29 ▶ if ((av = otel_h_av(aTHX_ sp, "links"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((av = otel_h_av(aTHX_ sp, "links"))) { 7 22 29
230 7 AV *out = newAV();
231 7 SSize_t i, n = av_len(av) + 1;
232 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 7 14
233 7 SV **e = av_fetch(av, i, 0);
234 7 ▶ HV *ln = otel_hv_of(aTHX_ (e ? *e : NULL));
Branch: 1 branch
BranchTrueFalseTotal
HV *ln = otel_hv_of(aTHX_ (e ? *e : NULL)); 7 0 7
235 7 HV *jo = newHV();
236 7 ▶ if (ln) {
Branch: 1 branch
BranchTrueFalseTotal
if (ln) { 7 0 7
237 7 otel_json_id(aTHX_ jo, "traceId", ln, "trace_id", 16);
238 7 otel_json_id(aTHX_ jo, "spanId", ln, "span_id", 8);
239 7 otel_json_str(aTHX_ jo, "traceState", ln, "trace_state");
240 7 otel_json_attrs(aTHX_ jo, otel_h_hv(aTHX_ ln, "attributes"),
241 "attributes");
242 }
243 7 av_push(out, newRV_noinc((SV *)jo));
244 }
245 7 (void)hv_stores(o, "links", newRV_noinc((SV *)out));
246 }
247  
248 {
249 29 HV *st = otel_h_hv(aTHX_ sp, "status");
250 29 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 10 19 29
251 10 HV *jo = newHV();
252 10 SV *code = otel_h(aTHX_ st, "code");
253 10 otel_json_str(aTHX_ jo, "message", st, "message");
254 10 ▶ if (code)
Branch: 1 branch
BranchTrueFalseTotal
if (code) 10 0 10
255 10 (void)hv_stores(jo, "code",
256 newSVpv(otel_json_status_name(SvIV(code)), 0));
257 10 (void)hv_stores(o, "status", newRV_noinc((SV *)jo));
258 }
259 }
260 29 return newRV_noinc((SV *)o);
261 }
262  
263 29 static SV *otel_json_traces_sv(pTHX_ HV *payload) {
264 29 HV *root = newHV();
265 29 AV *rss = otel_h_av(aTHX_ payload, "resource_spans");
266 29 AV *out = newAV();
267 29 ▶ SSize_t i, n = rss ? av_len(rss) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rss ? av_len(rss) + 1 : 0; 29 0 29
268  
269 58 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29 29 58
270 29 SV **e = av_fetch(rss, i, 0);
271 29 ▶ HV *rs = otel_hv_of(aTHX_ (e ? *e : NULL));
Branch: 1 branch
BranchTrueFalseTotal
HV *rs = otel_hv_of(aTHX_ (e ? *e : NULL)); 29 0 29
272 29 HV *jrs = newHV();
273 HV *res;
274 AV *sss;
275 29 ▶ if (!rs) { av_push(out, newRV_noinc((SV *)jrs)); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rs) { av_push(out, newRV_noinc((SV *)jrs)); continue; } 0 29 29
276  
277 29 ▶ if ((res = otel_h_hv(aTHX_ rs, "resource"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((res = otel_h_hv(aTHX_ rs, "resource"))) { 7 22 29
278 7 HV *jr = newHV();
279 7 otel_json_attrs(aTHX_ jr, otel_h_hv(aTHX_ res, "attributes"),
280 "attributes");
281 7 (void)hv_stores(jrs, "resource", newRV_noinc((SV *)jr));
282 }
283 29 ▶ if ((sss = otel_h_av(aTHX_ rs, "scope_spans"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((sss = otel_h_av(aTHX_ rs, "scope_spans"))) { 29 0 29
284 29 AV *jss = newAV();
285 29 SSize_t j, m = av_len(sss) + 1;
286 58 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 29 29 58
287 29 SV **se = av_fetch(sss, j, 0);
288 29 ▶ HV *ss = otel_hv_of(aTHX_ (se ? *se : NULL));
Branch: 1 branch
BranchTrueFalseTotal
HV *ss = otel_hv_of(aTHX_ (se ? *se : NULL)); 29 0 29
289 29 HV *jo = newHV();
290 HV *sc;
291 AV *spans;
292 29 ▶ if (ss && (sc = otel_h_hv(aTHX_ ss, "scope"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (ss && (sc = otel_h_hv(aTHX_ ss, "scope"))) { 29 0 29
if (ss && (sc = otel_h_hv(aTHX_ ss, "scope"))) { 7 22 29
293 7 HV *jsc = newHV();
294 7 otel_json_str(aTHX_ jsc, "name", sc, "name");
295 7 otel_json_str(aTHX_ jsc, "version", sc, "version");
296 7 otel_json_attrs(aTHX_ jsc,
297 otel_h_hv(aTHX_ sc, "attributes"), "attributes");
298 7 (void)hv_stores(jo, "scope", newRV_noinc((SV *)jsc));
299 }
300 29 ▶ if (ss && (spans = otel_h_av(aTHX_ ss, "spans"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (ss && (spans = otel_h_av(aTHX_ ss, "spans"))) { 29 0 29
if (ss && (spans = otel_h_av(aTHX_ ss, "spans"))) { 29 0 29
301 29 AV *jsp = newAV();
302 29 SSize_t k, c = av_len(spans) + 1;
303 58 ▶ for (k = 0; k < c; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < c; k++) { 29 29 58
304 29 SV **pe = av_fetch(spans, k, 0);
305 29 ▶ av_push(jsp, otel_json_span(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
av_push(jsp, otel_json_span(aTHX_ 29 0 29
306 otel_hv_of(aTHX_ (pe ? *pe : NULL))));
307 }
308 29 (void)hv_stores(jo, "spans", newRV_noinc((SV *)jsp));
309 }
310 29 ▶ if (ss) otel_json_str(aTHX_ jo, "schemaUrl", ss, "schema_url");
Branch: 1 branch
BranchTrueFalseTotal
if (ss) otel_json_str(aTHX_ jo, "schemaUrl", ss, "schema_url"); 29 0 29
311 29 av_push(jss, newRV_noinc((SV *)jo));
312 }
313 29 (void)hv_stores(jrs, "scopeSpans", newRV_noinc((SV *)jss));
314 }
315 29 otel_json_str(aTHX_ jrs, "schemaUrl", rs, "schema_url");
316 29 av_push(out, newRV_noinc((SV *)jrs));
317 }
318 29 (void)hv_stores(root, "resourceSpans", newRV_noinc((SV *)out));
319 29 return newRV_noinc((SV *)root);
320 }
321  
322 #endif /* OTEL_JSON_H */
« include/otel_tracer.h ↑ Summary include/otel_meter.h »