xs/ingest.xs

filter: Statementstmt 73.4% 133 / 181 Branchbran 47.7% 104 / 218 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 59.4% 237 / 399 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_nsarith.h ↑ Summary include/punk_observe/po_json_in.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Ingest PREFIX = poi_
2  
3 # --- the engine -------------------------------------------------------------
4  
5 # The object is a pointer, not a hash: `call` reads five fields per request and
6 # a hash lookup for each is the sort of cost that only looks small.
7 SV *
8 poi_new(SV *class, ...)
9 CODE:
10 {
11 po_ingest *ing;
12 SV *sv;
13 int i;
14  
15 37 ▶ if ((items - 1) % 2) croak("Punk::Observe::Ingest->new: odd option");
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2) croak("Punk::Observe::Ingest->new: odd option"); 0 37 37
16 37 ing = po_ingest_new();
17 37 ▶ if (!ing) croak("out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!ing) croak("out of memory"); 0 37 37
18  
19 { /* $MAX_RATIO stays the documented override point. */
20 37 SV *mr = get_sv("Punk::Observe::Ingest::MAX_RATIO", 0);
21 37 po_u64 v = 0;
22 37 ▶ if (mr && SvOK(mr) && po_sv_to_u64(aTHX_ mr, &v) && v)
Branch: 4 branches
BranchTrueFalseTotal
if (mr && SvOK(mr) && po_sv_to_u64(aTHX_ mr, &v) && v) 37 0 37
if (mr && SvOK(mr) && po_sv_to_u64(aTHX_ mr, &v) && v) 37 0 37
if (mr && SvOK(mr) && po_sv_to_u64(aTHX_ mr, &v) && v) 37 0 37
if (mr && SvOK(mr) && po_sv_to_u64(aTHX_ mr, &v) && v) 37 0 37
23 37 ing->max_ratio = v;
24 }
25  
26 85 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 49 36 85
27 STRLEN kl;
28 const char *k;
29 49 SV *v = ST(i + 1);
30 49 po_u64 n = 0;
31  
32 49 k = SvPV(ST(i), kl);
33  
34 49 ▶ if (kl == 8 && memcmp(k, "max_body", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 8 && memcmp(k, "max_body", 8) == 0) { 37 12 49
if (kl == 8 && memcmp(k, "max_body", 8) == 0) { 4 33 37
35 6 ▶ if (po_sv_to_u64(aTHX_ v, &n) && n)
Branch: 2 branches
BranchTrueFalseTotal
if (po_sv_to_u64(aTHX_ v, &n) && n) 2 2 4
if (po_sv_to_u64(aTHX_ v, &n) && n) 2 0 2
36 2 ▶ ing->max_body = n > PO_BODY_MAX ? PO_BODY_MAX : n;
Branch: 1 branch
BranchTrueFalseTotal
ing->max_body = n > PO_BODY_MAX ? PO_BODY_MAX : n; 2 0 2
37 }
38 45 ▶ else if (kl == 9 && memcmp(k, "max_ratio", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 9 && memcmp(k, "max_ratio", 9) == 0) { 2 43 45
else if (kl == 9 && memcmp(k, "max_ratio", 9) == 0) { 2 0 2
39 2 ▶ if (po_sv_to_u64(aTHX_ v, &n) && n) ing->max_ratio = n;
Branch: 2 branches
BranchTrueFalseTotal
if (po_sv_to_u64(aTHX_ v, &n) && n) ing->max_ratio = n; 2 0 2
if (po_sv_to_u64(aTHX_ v, &n) && n) ing->max_ratio = n; 2 0 2
40 }
41 43 ▶ else if (kl == 11 && memcmp(k, "max_records", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 11 && memcmp(k, "max_records", 11) == 0) { 3 40 43
else if (kl == 11 && memcmp(k, "max_records", 11) == 0) { 3 0 3
42 3 ▶ if (po_sv_to_u64(aTHX_ v, &n)) ing->max_records = n;
Branch: 1 branch
BranchTrueFalseTotal
if (po_sv_to_u64(aTHX_ v, &n)) ing->max_records = n; 3 0 3
43 }
44 40 ▶ else if (kl == 4 && memcmp(k, "auth", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memcmp(k, "auth", 4) == 0) { 7 33 40
else if (kl == 4 && memcmp(k, "auth", 4) == 0) { 6 1 7
45 6 ▶ if (SvOK(v)) ing->auth = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) ing->auth = newSVsv(v); 6 0 6
46 }
47 34 ▶ else if (kl == 8 && memcmp(k, "on_batch", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memcmp(k, "on_batch", 8) == 0) { 33 1 34
else if (kl == 8 && memcmp(k, "on_batch", 8) == 0) { 33 0 33
48 33 ▶ if (SvOK(v)) ing->on_batch = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) ing->on_batch = newSVsv(v); 33 0 33
49 }
50 1 ▶ else if (kl == 4 && memcmp(k, "grpc", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memcmp(k, "grpc", 4) == 0) { 1 0 1
else if (kl == 4 && memcmp(k, "grpc", 4) == 0) { 1 0 1
51 1 ▶ if (SvTRUE(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(v)) { 1 0 1
52 1 po_ingest_free(aTHX_ ing);
53 /* gRPC refuses at BOOT rather than at the first
54 * request. A gRPC call returns HTTP 200 even when it
55 * fails; the outcome lives entirely in the grpc-status
56 * and grpc-message TRAILERS. PSGI has no trailer
57 * channel and Hyperman's HTTP/2 path has no escape for
58 * one. An endpoint that answered 200 with no trailers
59 * would be an exporter that looks completely
60 * successful and has lost everything, which is worse
61 * than not offering gRPC at all - so this is a loud
62 * refusal at configuration time, where somebody can
63 * act on it. */
64 1 croak("Punk::Observe::Ingest: gRPC ingest is not available.\n"
65 " gRPC reports failure in HTTP/2 trailers, and there is no\n"
66 " trailer channel in PSGI or in Hyperman's HTTP/2 path. Serving\n"
67 " it anyway would answer 200 with no grpc-status, which an\n"
68 " exporter reads as complete success while losing every batch.\n"
69 " Use the OTLP/HTTP endpoints on 4318 instead.\n");
70 }
71 }
72 }
73  
74 36 sv = newSViv(PTR2IV(ing));
75 36 RETVAL = newRV_noinc(sv);
76 36 ▶ sv_bless(RETVAL, gv_stashsv(SvROK(class) ? SvRV(class) : class, GV_ADD));
Branch: 1 branch
BranchTrueFalseTotal
sv_bless(RETVAL, gv_stashsv(SvROK(class) ? SvRV(class) : class, GV_ADD)); 0 36 36
77 36 SvREADONLY_on(sv);
78 }
79 OUTPUT:
80 RETVAL
81  
82 void
83 poi_DESTROY(SV *self)
84 CODE:
85 {
86 36 SV *sv = SvRV(self);
87 36 po_ingest *ing = INT2PTR(po_ingest *, SvIVX(sv));
88 36 SvREADONLY_off(sv);
89 36 sv_setiv(sv, 0);
90 36 po_ingest_free(aTHX_ ing);
91 }
92  
93 # The PSGI application. A real CV with the engine hung off it as ext magic,
94 # rather than a Perl closure: the coderef IS the entry point on every request,
95 # and a closure would put a Perl frame and a method dispatch in front of it.
96 SV *
97 poi_to_app(SV *self)
98 CODE:
99 {
100 CV *cv;
101 34 (void)po_ingest_of(aTHX_ self, "to_app");
102 34 cv = newXS(NULL, po_ingest_app_xs, __FILE__);
103 34 ▶ if (!cv) croak("cannot create the app coderef");
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) croak("cannot create the app coderef"); 0 34 34
104 /* Who owns the reference newXS returned depends on the perl: newer
105 * ones hand back an unowned anonymous CV, older ones may have
106 * parked it in a glob first. Take our own only in the second case,
107 * so this neither leaks a CV nor frees one twice. */
108 34 ▶ if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv);
Branch: 3 branches
BranchTrueFalseTotal
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 34 0 34
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 0 34 34
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 0 0 0
109 34 sv_magicext((SV *)cv, self, PERL_MAGIC_ext, &po_app_vtbl, NULL, 0);
110 34 RETVAL = newRV_noinc((SV *)cv);
111 }
112 OUTPUT:
113 RETVAL
114  
115 SV *
116 poi_call(SV *self, SV *env)
117 CODE:
118 3 RETVAL = po_ingest_call(aTHX_ po_ingest_of(aTHX_ self, "call"), env);
119 OUTPUT:
120 RETVAL
121  
122 # --- the pieces, for the tests ----------------------------------------------
123  
124 # Decode an already-parsed OTLP/JSON structure into records.
125 #
126 # The parse itself is File::Raw::JSON's, in Perl, and that is deliberate:
127 # otel_json.h:29-32 makes the same call about the same transport - JSON is the
128 # debuggable path, protobuf is the production one, and duplicating a JSON
129 # parser to save allocations on a path nobody benchmarks is the wrong trade.
130 # The WALK is C, so nothing is allocated per record.
131 SV *
132 poi_decode_json(SV *doc, SV *signal)
133 CODE:
134 {
135 po_batch b;
136 STRLEN slen;
137 const char *sig;
138 AV *recs;
139 HV *res;
140 size_t i;
141  
142 8 sig = SvPV(signal, slen);
143 8 ▶ 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 8 8
144  
145 8 ▶ if (slen == 6 && memcmp(sig, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 7 1 8
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 7 0 7
146 7 po_json_traces(aTHX_ doc, &b);
147 1 ▶ else if (slen == 4 && memcmp(sig, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 1 1
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 0 0
148 0 po_json_logs(aTHX_ doc, &b);
149 1 ▶ else if (slen == 7 && memcmp(sig, "metrics", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 1 0 1
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 1 0 1
150 1 po_json_metrics(aTHX_ doc, &b);
151 0 else { po_batch_free(&b); croak("unknown signal"); }
152  
153 8 recs = newAV();
154 8 ▶ if (!b.err)
Branch: 1 branch
BranchTrueFalseTotal
if (!b.err) 8 0 8
155 15 ▶ for (i = 0; i < b.n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b.n; i++) 7 8 15
156 7 av_push(recs, po_rec_hv(aTHX_ &b.rec[i], &b.arena));
157  
158 8 res = newHV();
159 8 hv_stores(res, "ok", newSViv(b.err ? 0 : 1));
160 8 hv_stores(res, "records", newRV_noinc((SV *)recs));
161 8 hv_stores(res, "dropped_bad_trace", newSViv((IV)b.dropped_bad_trace));
162 8 hv_stores(res, "clamped_durations", newSViv((IV)b.clamped_durations));
163 8 po_batch_free(&b);
164 8 RETVAL = newRV_noinc((SV *)res);
165 }
166 OUTPUT:
167 RETVAL
168  
169 # Count records without materialising them. This is what the real ingest path
170 # does: decode, count, append to the WAL, reply - no SV per record anywhere.
171 SV *
172 poi_count(SV *buf, SV *signal, SV *encoding)
173 CODE:
174 {
175 po_batch b;
176 STRLEN len, slen, elen;
177 const char *p, *sig, *enc;
178 0 int ok = 0;
179 HV *res;
180  
181 0 ▶ p = SvPV(buf, len);
Branch: 1 branch
BranchTrueFalseTotal
p = SvPV(buf, len); 0 0 0
182 0 ▶ sig = SvPV(signal, slen);
Branch: 1 branch
BranchTrueFalseTotal
sig = SvPV(signal, slen); 0 0 0
183 0 enc = SvPV(encoding, elen);
184  
185 0 ▶ if (!po_batch_init(&b, 64)) croak("out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_batch_init(&b, 64)) croak("out of memory"); 0 0 0
186  
187 0 ▶ if (elen == 8 && memcmp(enc, "protobuf", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (elen == 8 && memcmp(enc, "protobuf", 8) == 0) { 0 0 0
if (elen == 8 && memcmp(enc, "protobuf", 8) == 0) { 0 0 0
188 0 ▶ if (slen == 6 && memcmp(sig, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 0 0
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 0 0
189 0 ok = po_otlp_traces(p, (size_t)len, &b);
190 0 ▶ else if (slen == 4 && memcmp(sig, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 0 0
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 0 0
191 0 ok = po_otlp_logs(p, (size_t)len, &b);
192 0 ▶ else if (slen == 7 && memcmp(sig, "metrics", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 0 0 0
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 0 0 0
193 0 ok = po_otlp_metrics(p, (size_t)len, &b);
194 0 else { po_batch_free(&b); croak("unknown signal"); }
195 }
196 0 else { po_batch_free(&b); croak("unknown encoding"); }
197  
198 0 res = newHV();
199 0 ▶ hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0)); 0 0 0
hv_stores(res, "ok", newSViv(ok && !b.err ? 1 : 0)); 0 0 0
200 0 hv_stores(res, "records", po_u64_to_sv((po_u64)b.n));
201 0 hv_stores(res, "dropped_bad_trace", newSViv((IV)b.dropped_bad_trace));
202 0 hv_stores(res, "clamped_durations", newSViv((IV)b.clamped_durations));
203 0 po_batch_free(&b);
204 0 RETVAL = newRV_noinc((SV *)res);
205 }
206 OUTPUT:
207 RETVAL
208  
209 # Decode a batch and append it to the write-ahead log in ONE pass.
210 #
211 # THIS IS THE INGEST PATH. What it replaces is a round trip the store had no
212 # business paying for: decode to records in C, materialise an SV per record,
213 # hand the array to Perl, and have Perl hand it back to a second XSUB that
214 # rebuilt the record array and re-encoded every attribute block. Two
215 # allocations per attribute per record, per exported batch - and it was
216 # LOSSY as well as slow, because the hash in the middle only carried the
217 # three fields the WAL surface happened to read.
218 #
219 # Here the decoder's own record array and its arena go straight into the
220 # writev. Nothing is copied and no SV exists unless `want_records` asks for
221 # one, which is what an on_records observer does.
222 #
223 # `buf` is the wire bytes for protobuf and the already-parsed document for
224 # json, because the JSON parse is File::Raw::JSON's and stays in Perl.
225 SV *
226 poi_decode_append(SV *path, SV *buf, SV *signal, SV *encoding, SV *policy, SV *interval_ns, SV *want_records, ...)
227 CODE:
228 {
229 po_batch b;
230 po_wal w;
231 STRLEN slen, elen, plen;
232 const char *sig, *enc, *pth;
233 20 po_u64 iv = 0;
234 20 int ok = 0, appended = 0, opened = 0;
235 HV *res;
236  
237 20 ▶ sig = SvPV(signal, slen);
Branch: 1 branch
BranchTrueFalseTotal
sig = SvPV(signal, slen); 20 0 20
238 20 ▶ enc = SvPV(encoding, elen);
Branch: 1 branch
BranchTrueFalseTotal
enc = SvPV(encoding, elen); 20 0 20
239 20 pth = SvPV(path, plen);
240  
241 20 ▶ if (!po_batch_init(&b, 64)) croak("out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_batch_init(&b, 64)) croak("out of memory"); 0 20 20
242  
243 20 ▶ if (elen == 4 && memcmp(enc, "json", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (elen == 4 && memcmp(enc, "json", 4) == 0) { 20 0 20
if (elen == 4 && memcmp(enc, "json", 4) == 0) { 20 0 20
244 20 ▶ if (slen == 6 && memcmp(sig, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 20 20
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 0 0
245 0 po_json_traces(aTHX_ buf, &b);
246 20 ▶ else if (slen == 4 && memcmp(sig, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 9 11 20
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 9 0 9
247 9 po_json_logs(aTHX_ buf, &b);
248 11 ▶ else if (slen == 7 && memcmp(sig, "metrics", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 11 0 11
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 11 0 11
249 11 po_json_metrics(aTHX_ buf, &b);
250 0 else { po_batch_free(&b); croak("unknown signal"); }
251 20 ok = b.err ? 0 : 1;
252 }
253 else {
254 STRLEN blen;
255 0 const char *bp = SvPV(buf, blen);
256 0 ▶ if (slen == 6 && memcmp(sig, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 0 0
if (slen == 6 && memcmp(sig, "traces", 6) == 0) 0 0 0
257 0 ok = po_otlp_traces(bp, (size_t)blen, &b);
258 0 ▶ else if (slen == 4 && memcmp(sig, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 0 0
else if (slen == 4 && memcmp(sig, "logs", 4) == 0) 0 0 0
259 0 ok = po_otlp_logs(bp, (size_t)blen, &b);
260 0 ▶ else if (slen == 7 && memcmp(sig, "metrics", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 0 0 0
else if (slen == 7 && memcmp(sig, "metrics", 7) == 0) 0 0 0
261 0 ok = po_otlp_metrics(bp, (size_t)blen, &b);
262 0 else { po_batch_free(&b); croak("unknown signal"); }
263 0 ▶ if (b.err) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b.err) ok = 0; 0 0 0
264 }
265  
266 20 res = newHV();
267  
268 /* The records are materialised BEFORE the append, because the
269 * observer is documented as seeing what was decoded whether or
270 * not the disk took it - and because a failed append is exactly
271 * when somebody wants to know what was in the batch. */
272 20 ▶ if (ok && SvTRUE(want_records)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && SvTRUE(want_records)) { 20 0 20
if (ok && SvTRUE(want_records)) { 0 20 20
273 0 AV *recs = newAV();
274 size_t i;
275 0 ▶ for (i = 0; i < b.n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b.n; i++) 0 0 0
276 0 av_push(recs, po_rec_hv(aTHX_ &b.rec[i], &b.arena));
277 0 hv_stores(res, "records", newRV_noinc((SV *)recs));
278 }
279  
280 /* THE RATE LIMIT DECIDES BEFORE THE APPEND, NOT AFTER.
281 *
282 * A partial success means "I did not keep these". Deciding after
283 * the write and reporting the overflow anyway would say that
284 * about records already on the disk, and an exporter doing the
285 * correct thing - resending what it was told was rejected -
286 * would duplicate every one of them. So the batch is truncated
287 * here and only the admitted prefix reaches the log.
288 *
289 * Truncating a PREFIX rather than refusing the batch is the whole
290 * reason po_rate_admit returns a count: refusing whole batches is
291 * what turns a limiter into an amplifier. */
292 20 ▶ if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) {
Branch: 5 branches
BranchTrueFalseTotal
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 20 0 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 20 0 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 1 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 0 19
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 0 19
293 19 po_shared *sh = INT2PTR(po_shared *, SvIV(ST(7)));
294 po_rate_cfg cfg;
295 po_rate_win win;
296 19 po_u64 maxr = 0, maxb = 0, admit, bytes = 0;
297  
298 19 ▶ if (items > 8) (void)po_sv_to_u64(aTHX_ ST(8), &maxr);
Branch: 1 branch
BranchTrueFalseTotal
if (items > 8) (void)po_sv_to_u64(aTHX_ ST(8), &maxr); 19 0 19
299 19 ▶ if (items > 9) (void)po_sv_to_u64(aTHX_ ST(9), &maxb);
Branch: 1 branch
BranchTrueFalseTotal
if (items > 9) (void)po_sv_to_u64(aTHX_ ST(9), &maxb); 19 0 19
300 19 po_rate_cfg_init(&cfg, maxr, maxb);
301 19 po_shared_rate(sh, &win);
302  
303 /* The decoded size, which is what the store will hold - not
304 * the request body, which may be gzipped and is a different
305 * number depending on how well it compressed. */
306 19 bytes = (po_u64)b.arena.len;
307 19 admit = po_rate_admit(&cfg, &win, (po_u64)b.n, bytes);
308 19 ▶ if (admit < (po_u64)b.n) {
Branch: 1 branch
BranchTrueFalseTotal
if (admit < (po_u64)b.n) { 1 18 19
309 1 hv_stores(res, "rejected", po_u64_to_sv((po_u64)b.n - admit));
310 1 b.n = (size_t)admit;
311 }
312 }
313  
314 /* THE CARDINALITY GATE, BEFORE THE APPEND.
315 *
316 * Each metric record's series id is the hash of its canonical
317 * attribute block - the same bytes the segment writer interns at
318 * seal. An
319 * existing series passes untouched; a NEW one past the cap is
320 * not dropped and not stored as itself: its record is rewritten
321 * onto the named overflow series, so the volume survives, the
322 * cardinality stops, and the data says "you exceeded the cap"
323 * rather than going quietly missing. The exporter is NOT told
324 * these were rejected - they were kept, and a partial success
325 * naming them would invite a resend that duplicates them. */
326 20 ▶ if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) {
Branch: 5 branches
BranchTrueFalseTotal
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 20 0 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 20 0 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 1 20
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 0 19
if (ok && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) { 19 0 19
327 19 po_shared *shc = INT2PTR(po_shared *, SvIV(ST(7)));
328 19 uint32_t ov_off = 0, ov_len = 0;
329 19 po_u64 overflowed = 0;
330 size_t ri;
331 96 ▶ for (ri = 0; ri < b.n; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < b.n; ri++) { 77 19 96
332 po_h128 h;
333 /* METRICS ONLY. The cap bounds per-series ONGOING state -
334 * rollups, exemplar sidecars, compression streams - and
335 * only metrics have any. A log line or span with a unique
336 * attribute block is bytes in a sealed segment, paid once
337 * and aged out by retention; its bound is the rate
338 * limiter. Gating those too meant a payment id logged as
339 * a field cost a series per checkout, and "log any data"
340 * is a requirement. */
341 97 ▶ if (b.rec[ri].kind != PO_METRIC) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b.rec[ri].kind != PO_METRIC) continue; 52 25 77
342 25 h = po_murmur3_128(
343 25 b.arena.base + b.rec[ri].attr_off,
344 25 (size_t)b.rec[ri].attr_len, 0);
345 25 ▶ if (po_shared_series_admit(shc, h.hi, h.lo)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (po_shared_series_admit(shc, h.hi, h.lo)) continue; 20 5 25
346 5 ▶ if (!ov_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ov_len) { 3 2 5
347 po_attrs ov;
348 po_attr *oa;
349 po_h128 ovid;
350 3 po_attrs_init(&ov);
351 3 oa = po_attrs_push(&ov, "otel.overflow", 13);
352 3 ▶ if (oa) { oa->tag = PO_AV_STRING;
Branch: 1 branch
BranchTrueFalseTotal
if (oa) { oa->tag = PO_AV_STRING; 3 0 3
353 3 oa->sp = (const uint8_t *)"cap"; oa->slen = 3; }
354 3 ov_off = po_attrs_encode(&ov, &b.arena, &ov_len);
355 3 ▶ if (ov_off == PO_ARENA_ERR) { ov_len = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (ov_off == PO_ARENA_ERR) { ov_len = 0; continue; } 0 3 3
356 /* The overflow series is itself a series, admitted
357 * outside the cap or attributing to it would loop. */
358 3 ovid = po_murmur3_128(b.arena.base + ov_off,
359 (size_t)ov_len, 0);
360 3 po_shared_series_force(shc, ovid.hi, ovid.lo);
361 }
362 5 b.rec[ri].attr_off = ov_off;
363 5 b.rec[ri].attr_len = ov_len;
364 5 overflowed++;
365 }
366 19 ▶ if (overflowed)
Branch: 1 branch
BranchTrueFalseTotal
if (overflowed) 3 16 19
367 3 hv_stores(res, "overflowed", po_u64_to_sv(overflowed));
368 }
369  
370 /* An empty batch is a success that writes nothing. Opening the log
371 * for it would create an empty file per exporter that had nothing
372 * to say. */
373 20 ▶ if (ok && b.n && plen) {
Branch: 3 branches
BranchTrueFalseTotal
if (ok && b.n && plen) { 20 0 20
if (ok && b.n && plen) { 20 0 20
if (ok && b.n && plen) { 20 0 20
374 20 ▶ if (!po_sv_to_u64(aTHX_ interval_ns, &iv)) iv = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_sv_to_u64(aTHX_ interval_ns, &iv)) iv = 0; 0 20 20
375 40 ▶ if (po_wal_open(&w, pth, (int)SvIV(policy), iv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_wal_open(&w, pth, (int)SvIV(policy), iv)) { 20 0 20
376 20 opened = 1;
377 20 appended = po_wal_append(&w, b.rec, b.n,
378 20 b.arena.base, b.arena.len);
379 20 ▶ if (!appended)
Branch: 1 branch
BranchTrueFalseTotal
if (!appended) 0 20 20
380 0 hv_stores(res, "errno", newSVpv(strerror(errno), 0));
381 20 hv_stores(res, "frames", po_u64_to_sv(w.frames_written));
382 20 hv_stores(res, "bytes", po_u64_to_sv(w.bytes_written));
383 20 hv_stores(res, "fsyncs", po_u64_to_sv(w.fsyncs));
384 20 po_wal_close(&w);
385 }
386 0 else hv_stores(res, "errno", newSVpv(strerror(errno), 0));
387 }
388 0 ▶ else if (ok) appended = 1; /* nothing to write is written */
Branch: 1 branch
BranchTrueFalseTotal
else if (ok) appended = 1; /* nothing to write is written */ 0 0 0
389  
390 20 hv_stores(res, "ok", newSViv(ok ? 1 : 0));
391 20 hv_stores(res, "appended", newSViv(appended ? 1 : 0));
392 20 hv_stores(res, "opened", newSViv(opened ? 1 : 0));
393 20 hv_stores(res, "n", po_u64_to_sv((po_u64)b.n));
394  
395 /* WHAT ARRIVED, counted where every worker can see it. The store's
396 * own record count is what survived retention; this is what was
397 * accepted, and the two answer different questions. */
398 20 ▶ if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7)))
Branch: 5 branches
BranchTrueFalseTotal
if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) 20 0 20
if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) 20 0 20
if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) 19 1 20
if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) 19 0 19
if (appended && b.n && items > 7 && SvOK(ST(7)) && SvIV(ST(7))) 19 0 19
399 19 po_shared_record(INT2PTR(po_shared *, SvIV(ST(7))),
400 19 (po_u64)b.n, (po_u64)b.arena.len);
401  
402 20 hv_stores(res, "dropped_bad_trace", newSViv((IV)b.dropped_bad_trace));
403 20 hv_stores(res, "clamped_durations", newSViv((IV)b.clamped_durations));
404 20 po_batch_free(&b);
405 20 RETVAL = newRV_noinc((SV *)res);
406 }
407 OUTPUT:
408 RETVAL
409  
410 # The OTLP partial-success body, in protobuf.
411 #
412 # ExportTraceServiceResponse { partial_success = 1 }
413 # ExportTracePartialSuccess { rejected_* = 1, error_message = 2 }
414 #
415 # Tiny, and on the response path rather than the hot one, but it has to be
416 # exactly right: this is the channel that tells a client "I kept 9,600 of
417 # these and rejected 400", and Punk::OpenTelemetry's exporter reads it.
418 SV *
419 poi_partial_success_pb(SV *rejected, SV *message)
420 CODE:
421 {
422 char outer[80];
423 0 po_u64 rej = 0;
424 0 STRLEN mlen = 0;
425 0 const char *msg = NULL;
426 size_t o;
427  
428 0 (void)po_sv_to_u64(aTHX_ rejected, &rej);
429 0 ▶ if (SvOK(message)) msg = SvPV(message, mlen);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(message)) msg = SvPV(message, mlen); 0 0 0
430 0 o = po_partial_success_pb(outer, rej, msg, (size_t)mlen);
431 0 RETVAL = newSVpvn(outer, (STRLEN)o);
432 }
433 OUTPUT:
434 RETVAL
« include/punk_observe/po_nsarith.h ↑ Summary include/punk_observe/po_json_in.h »