xs/decode.xs

filter: Statementstmt 96.4% 54 / 56 Branchbran 72.5% 29 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.4% 83 / 96 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_scan.h ↑ Summary include/punk_observe/po_chunk.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Decode PREFIX = pod_
2  
3 # The decode surface. Records come back as an array of hashrefs so the tests
4 # can assert field by field; the ingest path in phase 3 never builds an SV per
5 # record and calls the C directly.
6  
7 # --- the generic reader, exposed for the wire-level tests -------------------
8  
9 # Parse a buffer into [ { field, wire, ... } ], one entry per top-level field.
10 # Enough to assert the wire contract without any OTLP schema in the way.
11 void
12 pod_pb_fields(SV *buf)
13 PPCODE:
14 {
15 po_pbr r;
16 STRLEN len;
17 const char *p;
18 uint32_t f, w;
19 129 AV *out = newAV();
20  
21 129 p = SvPV(buf, len); /* own line: sibling-arg order is UB */
22 129 po_pbr_init(&r, p, (size_t)len);
23  
24 170 ▶ while (po_pbr_next(&r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&r, &f, &w)) { 139 31 170
25 139 HV *h = newHV();
26 139 hv_stores(h, "field", newSVuv((UV)f));
27 139 hv_stores(h, "wire", newSVuv((UV)w));
28 139 switch (w) {
29 31 case PO_PB_VARINT: {
30 po_u64 v;
31 31 ▶ if (!po_pbr_varint(&r, &v)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(&r, &v)) break; 3 28 31
32 28 hv_stores(h, "varint", po_u64_to_sv(v));
33 28 hv_stores(h, "int32", newSViv((IV)(int32_t)(uint32_t)(v & 0xFFFFFFFFu)));
34 28 break;
35 }
36 3 case PO_PB_FIXED64: {
37 po_u64 v; double d;
38 3 ▶ if (!po_pbr_fixed64(&r, &v)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(&r, &v)) break; 0 3 3
39 3 memcpy(&d, &v, 8);
40 3 hv_stores(h, "fixed64", po_u64_to_sv(v));
41 3 hv_stores(h, "double", newSVnv((NV)d));
42 3 break;
43 }
44 2 case PO_PB_FIXED32: {
45 uint32_t v;
46 2 ▶ if (!po_pbr_fixed32(&r, &v)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed32(&r, &v)) break; 0 2 2
47 2 hv_stores(h, "fixed32", newSVuv((UV)v));
48 2 break;
49 }
50 103 case PO_PB_BYTES: {
51 const uint8_t *bp; size_t bn;
52 103 ▶ if (!po_pbr_bytes(&r, &bp, &bn)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(&r, &bp, &bn)) break; 95 8 103
53 8 hv_stores(h, "bytes", newSVpvn((const char *)bp, bn));
54 8 break;
55 }
56 0 default: break;
57 }
58 139 av_push(out, newRV_noinc((SV *)h));
59 139 ▶ if (r.err) break;
Branch: 1 branch
BranchTrueFalseTotal
if (r.err) break; 98 41 139
60 }
61  
62 {
63 129 HV *res = newHV();
64 129 hv_stores(res, "fields", newRV_noinc((SV *)out));
65 129 hv_stores(res, "err", newSViv((IV)r.err));
66 129 hv_stores(res, "errstr", newSVpv(po_pbr_errstr(r.err), 0));
67 129 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 129 129
68 }
69 }
70  
71 # --- OTLP ------------------------------------------------------------------
72  
73 SV *
74 pod_decode(SV *buf, SV *signal)
75 CODE:
76 {
77 po_batch b;
78 STRLEN len, slen;
79 const char *p, *sig;
80 int ok;
81 AV *recs;
82 HV *res;
83 size_t i;
84  
85 48 ▶ p = SvPV(buf, len);
Branch: 1 branch
BranchTrueFalseTotal
p = SvPV(buf, len); 48 0 48
86 48 sig = SvPV(signal, slen);
87  
88 48 ▶ if (!po_batch_init(&b, 16)) croak("out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_batch_init(&b, 16)) croak("out of memory"); 0 48 48
89  
90 48 ▶ if (slen == 6 && memcmp(sig, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 14 34 48
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 14 0 14
91 14 ok = po_otlp_traces(p, (size_t)len, &b);
92 34 ▶ else if (slen == 4 && memcmp(sig, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 4 30 34
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 4 0 4
93 4 ok = po_otlp_logs(p, (size_t)len, &b);
94 30 ▶ else if (slen == 7 && memcmp(sig, "metrics", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 30 0 30
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 30 0 30
95 30 ok = po_otlp_metrics(p, (size_t)len, &b);
96 0 else { po_batch_free(&b); croak("unknown signal"); }
97  
98 48 recs = newAV();
99 48 ▶ if (ok && !b.err) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && !b.err) { 47 1 48
if (ok && !b.err) { 47 0 47
100 169 ▶ for (i = 0; i < b.n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b.n; i++) 122 47 169
101 122 av_push(recs, po_rec_hv(aTHX_ &b.rec[i], &b.arena));
102 }
103  
104 48 res = newHV();
105 48 ▶ hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0)); 47 1 48
hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0)); 47 0 47
106 48 hv_stores(res, "records", newRV_noinc((SV *)recs));
107 48 hv_stores(res, "dropped_bad_trace", newSViv((IV)b.dropped_bad_trace));
108 48 hv_stores(res, "clamped_durations", newSViv((IV)b.clamped_durations));
109 48 po_batch_free(&b);
110 48 RETVAL = newRV_noinc((SV *)res);
111 }
112 OUTPUT:
113 RETVAL
« include/punk_observe/po_scan.h ↑ Summary include/punk_observe/po_chunk.h »