include/punk_observe/po_json_in.h

filter: Statementstmt 74.9% 227 / 303 Branchbran 45.4% 131 / 288 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.5% 358 / 591 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/ingest.xs ↑ Summary include/punk_observe/po_attr.h »
1 /* po_json_in.h - OTLP/JSON to the same po_rec.
2 *
3 * OTLP/JSON is a supported encoding, not a convenience, and it is NOT a naive
4 * rendering of the protobuf tree. Punk::OpenTelemetry's otel_json.h:7-25
5 * enumerates the four rules its ENCODER follows; this file reverses them, and
6 * each one has a failure mode worth naming:
7 *
8 * 1. Keys are lowerCamelCase. `startTimeUnixNano`, not
9 * `start_time_unix_nano`. A payload with snake_case keys parses as a
10 * message with every field absent - accepted, stored, and empty.
11 * 2. trace_id and span_id are HEX, not base64. proto3 maps `bytes` to
12 * base64 and OTLP explicitly overrides it for these two. This reader
13 * accepts BOTH and normalises to the same sixteen raw bytes, because a
14 * trace whose ids are spelled two ways silently splits in half and
15 * presents as data simply being missing.
16 * 3. 64-bit integers are STRINGS. A nanosecond timestamp is ~1.8e18 and
17 * loses its last two digits as a JSON number. Read as a string here, and
18 * a number is accepted only where it cannot have lost anything.
19 * 4. Enums are NAMES - `SPAN_KIND_SERVER`, not 2. Numbers are tolerated
20 * too, because real exporters emit them.
21 *
22 * The input is an already-parsed Perl structure from File::Raw::JSON. That is
23 * deliberate and it follows the SDK's own reasoning about the same transport:
24 * this is the debug path, not the hot one. The protobuf receiver is what runs
25 * in production, and duplicating a JSON parser to save allocations on a path
26 * nobody benchmarks would be the wrong trade. The WALK is still C, so no SV
27 * is allocated per record.
28 */
29 #ifndef PO_JSON_IN_H
30 #define PO_JSON_IN_H
31  
32 #include "punk_observe/po_compat.h"
33 #include "punk_observe/po_rec.h"
34 #include "punk_observe/po_attr.h"
35 #include "punk_observe/po_otlp_in.h"
36  
37 /* ---- small SV helpers ---------------------------------------------------- */
38  
39 1328 static SV *po_j_get(pTHX_ HV *h, const char *k) {
40 1328 SV **p = hv_fetch(h, k, (I32)strlen(k), 0);
41 1328 ▶ return (p && SvOK(*p)) ? *p : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (p && SvOK(*p)) ? *p : NULL; 949 379 1328
return (p && SvOK(*p)) ? *p : NULL; 949 0 949
42 }
43  
44 615 static HV *po_j_hv(SV *sv) {
45 615 ▶ if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return NULL; 574 41 615
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return NULL; 574 0 574
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return NULL; 0 574 574
46 574 return (HV *)SvRV(sv);
47 }
48  
49 360 static AV *po_j_av(SV *sv) {
50 360 ▶ if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return NULL; 253 107 360
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return NULL; 253 0 253
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return NULL; 0 253 253
51 253 return (AV *)SvRV(sv);
52 }
53  
54 /* Rule 3. A string is parsed as a string; a numeric SV is trusted ONLY when
55 * it is an integer slot, never an NV - taking SvUV of an NV-backed SV is
56 * exactly the silent truncation the rule exists to prevent. */
57 172 static po_u64 po_j_u64(pTHX_ SV *sv) {
58 172 po_u64 v = 0;
59 172 ▶ if (!sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 0; 62 110 172
60 110 ▶ if (po_sv_to_u64(aTHX_ sv, &v)) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (po_sv_to_u64(aTHX_ sv, &v)) return v; 110 0 110
61 0 return 0;
62 }
63  
64 62 static IV po_j_iv(pTHX_ SV *sv) {
65 62 ▶ if (!sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 0; 0 62 62
66 62 ▶ if (SvIOK(sv)) return SvIV(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) return SvIV(sv); 62 0 62
67 0 ▶ if (SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(sv)) { 0 0 0
68 0 STRLEN l; const char *p = SvPV(sv, l);
69 0 return (IV)atol(p);
70 }
71 0 return 0;
72 }
73  
74 /* Rule 2. Accept hex (the spec) or base64 (what hand-rolled clients send) and
75 * normalise both to raw bytes. Returns the byte count written. */
76 1056 static int po_j_hexval(char c) {
77 1056 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 1056 0 1056
if (c >= '0' && c <= '9') return c - '0'; 1008 48 1056
78 48 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 48 0 48
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 48 0 48
79 0 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 0 0 0
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 0 0 0
80 0 return -1;
81 }
82  
83 2 static size_t po_j_b64(const char *p, size_t n, uint8_t *out, size_t outmax) {
84 static const char T[] =
85 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
86 2 uint32_t acc = 0; int bits = 0; size_t o = 0; size_t i;
87 38 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 36 2 38
88 const char *q;
89 36 ▶ if (p[i] == '=' || p[i] == '\n' || p[i] == '\r') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (p[i] == '=' || p[i] == '\n' || p[i] == '\r') continue; 33 3 36
if (p[i] == '=' || p[i] == '\n' || p[i] == '\r') continue; 33 0 33
if (p[i] == '=' || p[i] == '\n' || p[i] == '\r') continue; 0 33 33
90 33 q = strchr(T, p[i]);
91 33 ▶ if (!q) return 0; /* not base64 either */
Branch: 1 branch
BranchTrueFalseTotal
if (!q) return 0; /* not base64 either */ 0 33 33
92 33 acc = (acc << 6) | (uint32_t)(q - T);
93 33 bits += 6;
94 33 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 24 9 33
95 24 bits -= 8;
96 24 ▶ if (o >= outmax) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o >= outmax) return 0; 0 24 24
97 24 out[o++] = (uint8_t)((acc >> bits) & 0xFF);
98 }
99 }
100 2 return o;
101 }
102  
103 159 static size_t po_j_id(pTHX_ SV *sv, uint8_t *out, size_t want) {
104 STRLEN len; const char *p;
105 size_t i;
106 159 ▶ if (!sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 0; 135 24 159
107 24 p = SvPV(sv, len);
108 24 ▶ if (len == want * 2) { /* hex, the spec's spelling */
Branch: 1 branch
BranchTrueFalseTotal
if (len == want * 2) { /* hex, the spec's spelling */ 22 2 24
109 22 int ok = 1;
110 550 ▶ for (i = 0; i < len; i++) if (po_j_hexval(p[i]) < 0) { ok = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (po_j_hexval(p[i]) < 0) { ok = 0; break; } 0 528 528
for (i = 0; i < len; i++) if (po_j_hexval(p[i]) < 0) { ok = 0; break; } 528 22 550
111 22 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 22 0 22
112 286 ▶ for (i = 0; i < want; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < want; i++) 264 22 286
113 264 out[i] = (uint8_t)((po_j_hexval(p[i * 2]) << 4)
114 264 | po_j_hexval(p[i * 2 + 1]));
115 22 return want;
116 }
117 }
118 { /* base64, which is what a hand-rolled client sends */
119 2 size_t n = po_j_b64(p, len, out, want);
120 2 ▶ if (n == want) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n == want) return n; 2 0 2
121 }
122 0 return 0; /* wrong length: OMIT, never pad */
123 }
124  
125 /* Rule 4. Accept the name or the number. */
126 13 static int po_j_enum(pTHX_ SV *sv, const char *const *names, int n) {
127 STRLEN len; const char *p; int i;
128 13 ▶ if (!sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 0; 8 5 13
129 5 ▶ if (SvIOK(sv) && !SvPOK(sv)) return (int)SvIV(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvPOK(sv)) return (int)SvIV(sv); 2 3 5
if (SvIOK(sv) && !SvPOK(sv)) return (int)SvIV(sv); 2 0 2
130 3 p = SvPV(sv, len);
131 10 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 10 0 10
132 10 ▶ if (names[i] && strlen(names[i]) == len && memcmp(p, names[i], len) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (names[i] && strlen(names[i]) == len && memcmp(p, names[i], len) == 0) 10 0 10
if (names[i] && strlen(names[i]) == len && memcmp(p, names[i], len) == 0) 5 5 10
if (names[i] && strlen(names[i]) == len && memcmp(p, names[i], len) == 0) 3 2 5
133 3 return i;
134 0 ▶ if (len && p[0] >= '0' && p[0] <= '9') return (int)atol(p);
Branch: 3 branches
BranchTrueFalseTotal
if (len && p[0] >= '0' && p[0] <= '9') return (int)atol(p); 0 0 0
if (len && p[0] >= '0' && p[0] <= '9') return (int)atol(p); 0 0 0
if (len && p[0] >= '0' && p[0] <= '9') return (int)atol(p); 0 0 0
135 0 return 0;
136 }
137  
138 static const char *const PO_J_KIND[] = {
139 "SPAN_KIND_UNSPECIFIED", "SPAN_KIND_INTERNAL", "SPAN_KIND_SERVER",
140 "SPAN_KIND_CLIENT", "SPAN_KIND_PRODUCER", "SPAN_KIND_CONSUMER"
141 };
142 static const char *const PO_J_STATUS[] = {
143 "STATUS_CODE_UNSET", "STATUS_CODE_OK", "STATUS_CODE_ERROR"
144 };
145  
146 /* ---- attributes ----------------------------------------------------------- */
147  
148 static void po_j_value(pTHX_ SV *v, po_attrs *s, const char *key, size_t klen,
149 po_arena *ar, int depth);
150  
151 /* One JSON AnyValue: { "stringValue": "x" } and friends. */
152 75 static void po_j_anyvalue(pTHX_ HV *h, po_attrs *s,
153 const char *key, size_t klen,
154 po_arena *ar, int depth) {
155 SV *v;
156 po_attr *a;
157  
158 /* a->sp BORROWS from the decoded SV, never from the arena.
159 *
160 * The arena reallocs as it grows, so a pointer into it dangles the moment
161 * a later attribute is added - and the corruption would appear only in
162 * batches large enough to trigger a realloc, which is to say in
163 * production and not in a small test. The decoded Perl structure is alive
164 * for the whole walk and its PV buffers are stable, exactly as the
165 * protobuf path borrows from the stable request buffer.
166 * po_attrs_encode does the one copy, at the end. */
167 75 ▶ if ((v = po_j_get(aTHX_ h, "stringValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "stringValue"))) { 75 0 75
168 75 STRLEN l; const char *p = SvPV(v, l);
169 75 ▶ if ((a = po_attrs_push(s, key, klen))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) { 75 0 75
170 75 a->tag = PO_AV_STRING;
171 75 a->sp = (const uint8_t *)p;
172 75 a->slen = (size_t)l;
173 }
174 75 return;
175 }
176 0 ▶ if ((v = po_j_get(aTHX_ h, "boolValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "boolValue"))) { 0 0 0
177 0 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 0 0 0
178 0 { a->tag = PO_AV_BOOL; a->u = SvTRUE(v) ? 1 : 0; }
179 0 return;
180 }
181 0 ▶ if ((v = po_j_get(aTHX_ h, "intValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "intValue"))) { 0 0 0
182 0 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 0 0 0
183 0 { a->tag = PO_AV_INT; a->u = po_j_u64(aTHX_ v); }
184 0 return;
185 }
186 0 ▶ if ((v = po_j_get(aTHX_ h, "doubleValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "doubleValue"))) { 0 0 0
187 0 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 0 0 0
188 0 { a->tag = PO_AV_DOUBLE; a->d = SvNV(v); }
189 0 return;
190 }
191 0 ▶ if ((v = po_j_get(aTHX_ h, "bytesValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "bytesValue"))) { 0 0 0
192 0 STRLEN l; const char *p = SvPV(v, l);
193 0 ▶ if ((a = po_attrs_push(s, key, klen))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) { 0 0 0
194 0 a->tag = PO_AV_BYTES;
195 0 a->sp = (const uint8_t *)p;
196 0 a->slen = (size_t)l;
197 }
198 0 return;
199 }
200 0 ▶ if ((v = po_j_get(aTHX_ h, "arrayValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "arrayValue"))) { 0 0 0
201 0 HV *av_h = po_j_hv(v);
202 0 ▶ AV *vals = av_h ? po_j_av(po_j_get(aTHX_ av_h, "values")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *vals = av_h ? po_j_av(po_j_get(aTHX_ av_h, "values")) : NULL; 0 0 0
203 SSize_t i, n;
204 0 ▶ if (!vals || depth >= PO_ATTR_MAX_DEPTH) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!vals || depth >= PO_ATTR_MAX_DEPTH) return; 0 0 0
if (!vals || depth >= PO_ATTR_MAX_DEPTH) return; 0 0 0
205 0 n = av_len(vals) + 1;
206 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
207 0 SV **e = av_fetch(vals, i, 0);
208 0 char k[PO_ATTR_KEYMAX]; char num[16]; size_t nl = 0, kl;
209 0 IV t = i;
210 0 ▶ if (t == 0) num[nl++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (t == 0) num[nl++] = '0'; 0 0 0
211 0 else { char tmp[16]; size_t tn = 0;
212 0 ▶ while (t) { tmp[tn++] = (char)('0' + (int)(t % 10)); t /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (t) { tmp[tn++] = (char)('0' + (int)(t % 10)); t /= 10; } 0 0 0
213 0 ▶ while (tn) num[nl++] = tmp[--tn]; }
Branch: 1 branch
BranchTrueFalseTotal
while (tn) num[nl++] = tmp[--tn]; } 0 0 0
214 0 kl = po_attr_join(k, sizeof(k), key, klen, num, nl);
215 0 ▶ if (!kl || !e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!kl || !e) continue; 0 0 0
if (!kl || !e) continue; 0 0 0
216 0 po_j_value(aTHX_ *e, s, k, kl, ar, depth + 1);
217 }
218 0 return;
219 }
220 0 ▶ if ((v = po_j_get(aTHX_ h, "kvlistValue"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "kvlistValue"))) { 0 0 0
221 0 HV *kv_h = po_j_hv(v);
222 0 ▶ AV *vals = kv_h ? po_j_av(po_j_get(aTHX_ kv_h, "values")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *vals = kv_h ? po_j_av(po_j_get(aTHX_ kv_h, "values")) : NULL; 0 0 0
223 SSize_t i, n;
224 0 ▶ if (!vals || depth >= PO_ATTR_MAX_DEPTH) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!vals || depth >= PO_ATTR_MAX_DEPTH) return; 0 0 0
if (!vals || depth >= PO_ATTR_MAX_DEPTH) return; 0 0 0
225 0 n = av_len(vals) + 1;
226 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
227 0 SV **e = av_fetch(vals, i, 0);
228 0 ▶ HV *kv = e ? po_j_hv(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *kv = e ? po_j_hv(*e) : NULL; 0 0 0
229 SV *ks, *vs;
230 char k[PO_ATTR_KEYMAX]; size_t kl;
231 STRLEN sl; const char *sp;
232 0 ▶ if (!kv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!kv) continue; 0 0 0
233 0 ks = po_j_get(aTHX_ kv, "key");
234 0 vs = po_j_get(aTHX_ kv, "value");
235 0 ▶ if (!ks || !vs) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!ks || !vs) continue; 0 0 0
if (!ks || !vs) continue; 0 0 0
236 0 sp = SvPV(ks, sl);
237 0 kl = po_attr_join(k, sizeof(k), key, klen, sp, (size_t)sl);
238 0 ▶ if (!kl) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) continue; 0 0 0
239 0 po_j_value(aTHX_ vs, s, k, kl, ar, depth + 1);
240 }
241 0 return;
242 }
243 }
244  
245 75 static void po_j_value(pTHX_ SV *v, po_attrs *s, const char *key, size_t klen,
246 po_arena *ar, int depth) {
247 75 HV *h = po_j_hv(v);
248 75 ▶ if (h) po_j_anyvalue(aTHX_ h, s, key, klen, ar, depth);
Branch: 1 branch
BranchTrueFalseTotal
if (h) po_j_anyvalue(aTHX_ h, s, key, klen, ar, depth); 75 0 75
249 75 }
250  
251 /* A JSON attributes array: [ { "key": "k", "value": {...} }, ... ] */
252 154 static void po_j_attrs(pTHX_ SV *sv, po_attrs *s, po_arena *ar) {
253 154 AV *av = po_j_av(sv);
254 SSize_t i, n;
255 154 ▶ if (!av) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return; 78 76 154
256 76 n = av_len(av) + 1;
257 151 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 75 76 151
258 75 SV **e = av_fetch(av, i, 0);
259 75 ▶ HV *kv = e ? po_j_hv(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *kv = e ? po_j_hv(*e) : NULL; 75 0 75
260 SV *k, *v;
261 STRLEN kl; const char *kp;
262 75 ▶ if (!kv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!kv) continue; 0 75 75
263 75 k = po_j_get(aTHX_ kv, "key");
264 75 v = po_j_get(aTHX_ kv, "value");
265 75 ▶ if (!k || !v) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !v) continue; 75 0 75
if (!k || !v) continue; 0 75 75
266 75 kp = SvPV(k, kl);
267 75 po_j_value(aTHX_ v, s, kp, (size_t)kl, ar, 0);
268 }
269 }
270  
271 /* ---- the three signals ---------------------------------------------------- */
272  
273 typedef void (*po_j_leaf)(pTHX_ HV *, po_batch *, po_attrs *);
274  
275 11 static void po_j_span(pTHX_ HV *h, po_batch *b, po_attrs *inherited) {
276 po_rec *rec;
277 11 po_attrs at = *inherited;
278 uint8_t tid[16], sid[8], pid[8];
279 size_t tn, sn, pn;
280 po_u64 t_start, t_end;
281 SV *v;
282  
283 11 tn = po_j_id(aTHX_ po_j_get(aTHX_ h, "traceId"), tid, 16);
284 11 sn = po_j_id(aTHX_ po_j_get(aTHX_ h, "spanId"), sid, 8);
285 11 pn = po_j_id(aTHX_ po_j_get(aTHX_ h, "parentSpanId"), pid, 8);
286  
287 11 t_start = po_j_u64(aTHX_ po_j_get(aTHX_ h, "startTimeUnixNano"));
288 11 t_end = po_j_u64(aTHX_ po_j_get(aTHX_ h, "endTimeUnixNano"));
289  
290 11 po_j_attrs(aTHX_ po_j_get(aTHX_ h, "attributes"), &at, &b->arena);
291  
292 11 ▶ if (!(rec = po_batch_next(b))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return; 0 11 11
293 11 rec->kind = PO_SPAN;
294 11 rec->t_unix_nano = t_start;
295  
296 11 ▶ if (tn == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (tn == 16) { 11 0 11
297 11 po_id16(tid, 16, &rec->trace_id_hi, &rec->trace_id_lo);
298 11 ▶ if (!po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo)) { 0 11 11
if (!po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo)) { 0 0 0
299 0 b->n--; /* un-take the record */
300 0 b->dropped_bad_trace++;
301 0 return;
302 }
303 11 rec->flags |= PO_F_HAS_TRACE;
304 }
305 11 ▶ if (sn == 8) rec->span_id = po_id8(sid, 8);
Branch: 1 branch
BranchTrueFalseTotal
if (sn == 8) rec->span_id = po_id8(sid, 8); 11 0 11
306 11 ▶ if (pn == 8) { rec->parent_span_id = po_id8(pid, 8);
Branch: 1 branch
BranchTrueFalseTotal
if (pn == 8) { rec->parent_span_id = po_id8(pid, 8); 0 11 11
307 0 rec->flags |= PO_F_HAS_PARENT; }
308  
309 11 ▶ if (t_end >= t_start) rec->dur_nano = t_end - t_start;
Branch: 1 branch
BranchTrueFalseTotal
if (t_end >= t_start) rec->dur_nano = t_end - t_start; 11 0 11
310 0 else { rec->dur_nano = 0; rec->flags |= PO_F_CLAMPED_DUR;
311 0 b->clamped_durations++; }
312  
313 {
314 11 int kind = po_j_enum(aTHX_ po_j_get(aTHX_ h, "kind"), PO_J_KIND, 6);
315 11 int st = 0;
316 11 HV *sh = po_j_hv(po_j_get(aTHX_ h, "status"));
317 11 ▶ if (sh) st = po_j_enum(aTHX_ po_j_get(aTHX_ sh, "code"), PO_J_STATUS, 3);
Branch: 1 branch
BranchTrueFalseTotal
if (sh) st = po_j_enum(aTHX_ po_j_get(aTHX_ sh, "code"), PO_J_STATUS, 3); 1 10 11
318 11 po_rec_set_aux(rec, (uint8_t)kind, (uint8_t)st);
319 }
320  
321 11 ▶ if ((v = po_j_get(aTHX_ h, "name"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "name"))) { 11 0 11
322 11 STRLEN l; const char *p = SvPV(v, l);
323 11 rec->body_off = po_arena_put(&b->arena, p, (size_t)l);
324 11 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; } 0 11 11
325 11 rec->body_len = (uint32_t)l;
326 }
327 11 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
328 11 ▶ if (rec->attr_off == PO_ARENA_ERR) b->err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) b->err = 1; 0 11 11
329 }
330  
331 62 static void po_j_log(pTHX_ HV *h, po_batch *b, po_attrs *inherited) {
332 po_rec *rec;
333 62 po_attrs at = *inherited;
334 uint8_t tid[16], sid[8];
335 size_t tn, sn;
336 po_u64 t, tobs;
337 SV *v;
338  
339 62 tn = po_j_id(aTHX_ po_j_get(aTHX_ h, "traceId"), tid, 16);
340 62 sn = po_j_id(aTHX_ po_j_get(aTHX_ h, "spanId"), sid, 8);
341 62 t = po_j_u64(aTHX_ po_j_get(aTHX_ h, "timeUnixNano"));
342 62 tobs = po_j_u64(aTHX_ po_j_get(aTHX_ h, "observedTimeUnixNano"));
343  
344 62 po_j_attrs(aTHX_ po_j_get(aTHX_ h, "attributes"), &at, &b->arena);
345  
346 62 ▶ if (!(rec = po_batch_next(b))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return; 0 62 62
347 62 rec->kind = PO_LOG;
348 62 ▶ rec->t_unix_nano = t ? t : tobs;
Branch: 1 branch
BranchTrueFalseTotal
rec->t_unix_nano = t ? t : tobs; 62 0 62
349 {
350 62 IV sev = po_j_iv(aTHX_ po_j_get(aTHX_ h, "severityNumber"));
351 62 ▶ rec->severity = (uint16_t)(sev < 0 ? 0 : (sev > 24 ? 24 : sev));
Branch: 2 branches
BranchTrueFalseTotal
rec->severity = (uint16_t)(sev < 0 ? 0 : (sev > 24 ? 24 : sev)); 62 0 62
rec->severity = (uint16_t)(sev < 0 ? 0 : (sev > 24 ? 24 : sev)); 62 0 62
352 }
353 62 ▶ if (tn == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (tn == 16) { 0 62 62
354 0 po_id16(tid, 16, &rec->trace_id_hi, &rec->trace_id_lo);
355 0 ▶ if (po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo))
Branch: 2 branches
BranchTrueFalseTotal
if (po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo)) 0 0 0
if (po_trace_id_valid(rec->trace_id_hi, rec->trace_id_lo)) 0 0 0
356 0 rec->flags |= PO_F_HAS_TRACE;
357 }
358 62 ▶ if (sn == 8) rec->span_id = po_id8(sid, 8);
Branch: 1 branch
BranchTrueFalseTotal
if (sn == 8) rec->span_id = po_id8(sid, 8); 0 62 62
359  
360 62 ▶ if ((v = po_j_get(aTHX_ h, "body"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = po_j_get(aTHX_ h, "body"))) { 62 0 62
361 62 HV *bh = po_j_hv(v);
362 62 ▶ SV *s = bh ? po_j_get(aTHX_ bh, "stringValue") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *s = bh ? po_j_get(aTHX_ bh, "stringValue") : NULL; 62 0 62
363 62 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 62 0 62
364 62 STRLEN l; const char *p = SvPV(s, l);
365 62 rec->body_off = po_arena_put(&b->arena, p, (size_t)l);
366 62 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; } 0 62 62
367 62 rec->body_len = (uint32_t)l;
368 }
369 0 ▶ else if (bh) po_j_anyvalue(aTHX_ bh, &at, "body", 4, &b->arena, 0);
Branch: 1 branch
BranchTrueFalseTotal
else if (bh) po_j_anyvalue(aTHX_ bh, &at, "body", 4, &b->arena, 0); 0 0 0
370 }
371 62 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
372 62 ▶ if (rec->attr_off == PO_ARENA_ERR) b->err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) b->err = 1; 0 62 62
373 }
374  
375 /* The first exemplar carrying a trace id, out of a point's `exemplars` array.
376 * The protobuf decoder's rule, in the other spelling: one exemplar reaches
377 * the record because the record has one slot, and an exemplar with no trace
378 * id points nowhere. See po_otlp_in.h for why this field is the whole
379 * cross-signal jump. */
380 26 static void po_j_exemplar(pTHX_ SV *exs, po_rec *rec) {
381 26 AV *av = po_j_av(exs);
382 SSize_t i, n;
383 26 ▶ if (!av) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return; 25 1 26
384 1 n = av_len(av) + 1;
385 1 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 0 1
386 1 SV **e = av_fetch(av, i, 0);
387 1 ▶ HV *x = e ? po_j_hv(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *x = e ? po_j_hv(*e) : NULL; 1 0 1
388 uint8_t tid[16], sid[8];
389 size_t tn, sn;
390 1 po_u64 hi = 0, lo = 0;
391 1 ▶ if (!x) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) continue; 0 1 1
392 1 tn = po_j_id(aTHX_ po_j_get(aTHX_ x, "traceId"), tid, 16);
393 1 ▶ if (tn != 16) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (tn != 16) continue; 0 1 1
394 1 po_id16(tid, tn, &hi, &lo);
395 1 ▶ if (!po_trace_id_valid(hi, lo)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!po_trace_id_valid(hi, lo)) continue; 0 1 1
if (!po_trace_id_valid(hi, lo)) continue; 0 0 0
396 1 sn = po_j_id(aTHX_ po_j_get(aTHX_ x, "spanId"), sid, 8);
397 1 rec->trace_id_hi = hi;
398 1 rec->trace_id_lo = lo;
399 1 ▶ rec->span_id = sn == 8 ? po_id8(sid, sn) : 0;
Branch: 1 branch
BranchTrueFalseTotal
rec->span_id = sn == 8 ? po_id8(sid, sn) : 0; 1 0 1
400 1 return;
401 }
402 }
403  
404 26 static void po_j_points(pTHX_ SV *pts, po_batch *b, po_attrs *inherited,
405 const char *name, size_t namelen, uint16_t mflags) {
406 26 AV *av = po_j_av(pts);
407 SSize_t i, n;
408 26 ▶ if (!av) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return; 0 26 26
409 26 n = av_len(av) + 1;
410 52 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 26 26 52
411 26 SV **e = av_fetch(av, i, 0);
412 26 ▶ HV *p = e ? po_j_hv(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *p = e ? po_j_hv(*e) : NULL; 26 0 26
413 26 po_attrs at = *inherited;
414 po_rec *rec;
415 SV *dv, *iv2;
416 26 ▶ if (!p) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) continue; 0 26 26
417 26 po_j_attrs(aTHX_ po_j_get(aTHX_ p, "attributes"), &at, &b->arena);
418  
419 26 dv = po_j_get(aTHX_ p, "asDouble");
420 26 iv2 = po_j_get(aTHX_ p, "asInt");
421 26 ▶ if (!dv && !iv2) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!dv && !iv2) continue; 0 26 26
if (!dv && !iv2) continue; 0 0 0
422  
423 26 ▶ if (!(rec = po_batch_next(b))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return; 0 26 26
424 26 rec->kind = PO_METRIC;
425 26 rec->t_unix_nano = po_j_u64(aTHX_ po_j_get(aTHX_ p, "timeUnixNano"));
426 26 rec->flags |= mflags;
427 26 ▶ if (iv2) {
Branch: 1 branch
BranchTrueFalseTotal
if (iv2) { 0 26 26
428 0 po_u64 u = po_j_u64(aTHX_ iv2);
429 0 rec->flags |= PO_F_VALUE_IS_INT;
430 0 memcpy(&rec->value, &u, 8);
431 }
432 26 else rec->value = SvNV(dv);
433  
434 26 po_j_exemplar(aTHX_ po_j_get(aTHX_ p, "exemplars"), rec);
435  
436 26 ▶ if (name) {
Branch: 1 branch
BranchTrueFalseTotal
if (name) { 26 0 26
437 26 rec->body_off = po_arena_put(&b->arena, name, namelen);
438 26 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return; } 0 26 26
439 26 rec->body_len = (uint32_t)namelen;
440 }
441 26 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
442 26 ▶ if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return; } 0 26 26
443 }
444 }
445  
446 26 static void po_j_metric(pTHX_ HV *h, po_batch *b, po_attrs *inherited) {
447 26 SV *nv = po_j_get(aTHX_ h, "name");
448 26 STRLEN nl = 0;
449 52 ▶ const char *np = nv ? SvPV(nv, nl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *np = nv ? SvPV(nv, nl) : NULL; 26 0 26
450 HV *w;
451  
452 26 ▶ if ((w = po_j_hv(po_j_get(aTHX_ h, "sum")))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((w = po_j_hv(po_j_get(aTHX_ h, "sum")))) { 1 25 26
453 1 uint16_t f = 0;
454 1 SV *t = po_j_get(aTHX_ w, "aggregationTemporality");
455 1 SV *m = po_j_get(aTHX_ w, "isMonotonic");
456 /* Rule 4 plus the reversed-enum trap: OTLP names CUMULATIVE as
457 * AGGREGATION_TEMPORALITY_CUMULATIVE = 2. Same map back as the
458 * protobuf path, and the same one function decides. */
459 static const char *const T[] = {
460 "AGGREGATION_TEMPORALITY_UNSPECIFIED",
461 "AGGREGATION_TEMPORALITY_DELTA",
462 "AGGREGATION_TEMPORALITY_CUMULATIVE"
463 };
464 1 ▶ if (po_temporality_is_cumulative((po_u64)po_j_enum(aTHX_ t, T, 3)))
Branch: 1 branch
BranchTrueFalseTotal
if (po_temporality_is_cumulative((po_u64)po_j_enum(aTHX_ t, T, 3))) 1 0 1
465 1 f |= PO_F_CUMULATIVE;
466 1 ▶ if (m && SvTRUE(m)) f |= PO_F_MONOTONIC;
Branch: 2 branches
BranchTrueFalseTotal
if (m && SvTRUE(m)) f |= PO_F_MONOTONIC; 1 0 1
if (m && SvTRUE(m)) f |= PO_F_MONOTONIC; 1 0 1
467 1 po_j_points(aTHX_ po_j_get(aTHX_ w, "dataPoints"), b, inherited, np, nl, f);
468 26 return;
469 }
470 25 ▶ if ((w = po_j_hv(po_j_get(aTHX_ h, "gauge")))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((w = po_j_hv(po_j_get(aTHX_ h, "gauge")))) { 25 0 25
471 25 po_j_points(aTHX_ po_j_get(aTHX_ w, "dataPoints"), b, inherited, np, nl, 0);
472 25 return;
473 }
474 /* histogram, exponentialHistogram, summary: phase 5. Skipped, not
475 * half-decoded, exactly as the protobuf path skips them. */
476 }
477  
478 /* ---- the walk ------------------------------------------------------------- */
479  
480 32 static void po_j_walk(pTHX_ SV *top, po_batch *b, const char *f_resource_list,
481 const char *f_scope_list, const char *f_leaf_list,
482 po_j_leaf leaf) {
483 32 HV *root = po_j_hv(top);
484 AV *rl;
485 SSize_t i, n;
486  
487 32 ▶ if (!root) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!root) return; 0 32 32
488 32 rl = po_j_av(po_j_get(aTHX_ root, f_resource_list));
489 32 ▶ if (!rl) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rl) return; 4 28 32
490 28 n = av_len(rl) + 1;
491  
492 89 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61 28 89
493 61 SV **re = av_fetch(rl, i, 0);
494 61 ▶ HV *rh = re ? po_j_hv(*re) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *rh = re ? po_j_hv(*re) : NULL; 61 0 61
495 po_attrs res_attrs;
496 AV *sl;
497 SSize_t j, m;
498 61 ▶ if (!rh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!rh) continue; 0 61 61
499  
500 61 po_attrs_init(&res_attrs);
501 {
502 61 HV *res = po_j_hv(po_j_get(aTHX_ rh, "resource"));
503 61 ▶ if (res) po_j_attrs(aTHX_ po_j_get(aTHX_ res, "attributes"),
Branch: 1 branch
BranchTrueFalseTotal
if (res) po_j_attrs(aTHX_ po_j_get(aTHX_ res, "attributes"), 55 6 61
504 &res_attrs, &b->arena);
505 }
506  
507 61 sl = po_j_av(po_j_get(aTHX_ rh, f_scope_list));
508 61 ▶ if (!sl) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sl) continue; 0 61 61
509 61 m = av_len(sl) + 1;
510 122 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 61 61 122
511 61 SV **se = av_fetch(sl, j, 0);
512 61 ▶ HV *sh = se ? po_j_hv(*se) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = se ? po_j_hv(*se) : NULL; 61 0 61
513 AV *ll;
514 SSize_t k, o;
515 61 ▶ if (!sh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sh) continue; 0 61 61
516 61 ll = po_j_av(po_j_get(aTHX_ sh, f_leaf_list));
517 61 ▶ if (!ll) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ll) continue; 0 61 61
518 61 o = av_len(ll) + 1;
519 160 ▶ for (k = 0; k < o; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < o; k++) { 99 61 160
520 99 SV **le = av_fetch(ll, k, 0);
521 99 ▶ HV *lh = le ? po_j_hv(*le) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *lh = le ? po_j_hv(*le) : NULL; 99 0 99
522 99 ▶ if (lh) leaf(aTHX_ lh, b, &res_attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (lh) leaf(aTHX_ lh, b, &res_attrs); 99 0 99
523 }
524 }
525 }
526 }
527  
528 11 static void po_json_traces(pTHX_ SV *top, po_batch *b) {
529 11 po_j_walk(aTHX_ top, b, "resourceSpans", "scopeSpans", "spans", po_j_span);
530 11 }
531 9 static void po_json_logs(pTHX_ SV *top, po_batch *b) {
532 9 po_j_walk(aTHX_ top, b, "resourceLogs", "scopeLogs", "logRecords", po_j_log);
533 9 }
534 12 static void po_json_metrics(pTHX_ SV *top, po_batch *b) {
535 12 po_j_walk(aTHX_ top, b, "resourceMetrics", "scopeMetrics", "metrics",
536 po_j_metric);
537 12 }
538  
539 #endif /* PO_JSON_IN_H */
« xs/ingest.xs ↑ Summary include/punk_observe/po_attr.h »