include/punk_observe/po_otlp_in.h

filter: Statementstmt 93.0% 485 / 521 Branchbran 53.7% 330 / 614 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.8% 815 / 1135 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/view.xs ↑ Summary include/punk_observe/po_traceset.h »
1 /* po_otlp_in.h - OTLP request messages to po_rec.
2 *
3 * The schema layer. po_pb_read.h knows the wire; this file knows what the
4 * fields MEAN, and it learns the numbers from Punk::OpenTelemetry's
5 * otel_proto.h by dependency rather than by copying. Two sources of truth for
6 * a field number is the one thing that must not happen here: the client
7 * writing field 9 and the server reading field 9 as something else produces
8 * plausible wrong data, silently, with no error anywhere.
9 *
10 * All three signals reduce to the same po_rec, because that is what makes one
11 * `where` work across all three and the cross-signal stages expressible.
12 */
13 #ifndef PO_OTLP_IN_H
14 #define PO_OTLP_IN_H
15  
16 #include "punk_observe/po_compat.h"
17 #include "punk_observe/po_pb_read.h"
18 #include "punk_observe/po_rec.h"
19 #include "punk_observe/po_attr.h"
20 #include "otel_proto.h" /* the field numbers, from the SDK next door */
21  
22 typedef struct {
23 po_rec *rec;
24 size_t n;
25 size_t cap;
26 po_arena arena;
27 int dropped_bad_trace; /* all-zero trace ids, refused and counted */
28 int clamped_durations; /* end < start, clamped and counted */
29 int err;
30 } po_batch;
31  
32 103 static int po_batch_init(po_batch *b, size_t hint) {
33 103 memset(b, 0, sizeof(*b));
34 103 ▶ b->cap = hint ? hint : 64;
Branch: 1 branch
BranchTrueFalseTotal
b->cap = hint ? hint : 64; 103 0 103
35 103 b->rec = (po_rec *)malloc(b->cap * sizeof(po_rec));
36 103 ▶ if (!b->rec) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->rec) return 0; 0 103 103
37 103 ▶ if (!po_arena_init(&b->arena, 4096)) { free(b->rec); b->rec = NULL; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_arena_init(&b->arena, 4096)) { free(b->rec); b->rec = NULL; return 0; } 0 103 103
38 103 return 1;
39 }
40  
41 103 static void po_batch_free(po_batch *b) {
42 103 free(b->rec); b->rec = NULL;
43 103 po_arena_free(&b->arena);
44 103 b->n = b->cap = 0;
45 103 }
46  
47 248 static po_rec *po_batch_next(po_batch *b) {
48 248 ▶ if (b->n == b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->n == b->cap) { 1 247 248
49 1 size_t want = b->cap * 2;
50 1 po_rec *nr = (po_rec *)realloc(b->rec, want * sizeof(po_rec));
51 1 ▶ if (!nr) { b->err = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nr) { b->err = 1; return NULL; } 0 1 1
52 1 b->rec = nr; b->cap = want;
53 }
54 248 po_rec_zero(&b->rec[b->n]);
55 248 return &b->rec[b->n++];
56 }
57  
58 /* A trace or span id arrives as raw bytes: 16 for a trace, 8 for a span.
59 * A wrong length is OMITTED rather than padded - padding invents an id that
60 * collides with a real one. */
61 55 static void po_id16(const uint8_t *p, size_t n, po_u64 *hi, po_u64 *lo) {
62 int i;
63 55 *hi = *lo = 0;
64 55 ▶ if (n != 16) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n != 16) return; 1 54 55
65 486 ▶ for (i = 0; i < 8; i++) *hi = (*hi << 8) | p[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) *hi = (*hi << 8) | p[i]; 432 54 486
66 486 ▶ for (i = 8; i < 16; i++) *lo = (*lo << 8) | p[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 8; i < 16; i++) *lo = (*lo << 8) | p[i]; 432 54 486
67 }
68  
69 53 static po_u64 po_id8(const uint8_t *p, size_t n) {
70 53 po_u64 v = 0; int i;
71 53 ▶ if (n != 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != 8) return 0; 0 53 53
72 477 ▶ for (i = 0; i < 8; i++) v = (v << 8) | p[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) v = (v << 8) | p[i]; 424 53 477
73 53 return v;
74 }
75  
76 /* ---- temporality, and the trap otel_proto.h wrote down for this reader ----
77 *
78 * otel_proto.h:217-226 says it plainly: the OTLP enum is DELTA=1,
79 * CUMULATIVE=2, the REVERSE of the SDK's internal constants. Its own
80 * description of the consequence is why this lives in one function and is
81 * tested directly - a backend that gets it wrong "accepts without complaint
82 * and then draws completely wrongly".
83 *
84 * Temporality is not cosmetic. rate() over a delta sum is a division; over a
85 * cumulative sum it is a difference that must handle counter resets. The flag
86 * picks the algorithm. */
87 31 static int po_temporality_is_cumulative(po_u64 otlp_value) {
88 31 return otlp_value == (po_u64)PB_TEMPORALITY_CUMULATIVE;
89 }
90  
91 /* ---- traces -------------------------------------------------------------- */
92  
93 37 static int po_span_read(po_pbr *r, po_batch *b, po_attrs *inherited) {
94 po_rec *rec;
95 po_attrs at;
96 uint32_t f, w;
97 37 const uint8_t *namep = NULL; size_t namen = 0;
98 37 po_u64 t_start = 0, t_end = 0;
99 37 po_u64 tr_hi = 0, tr_lo = 0, span = 0, parent = 0;
100 37 int32_t kind = 0, status = 0;
101 37 int have_trace = 0;
102  
103 37 at = *inherited; /* resource + scope attributes first */
104  
105 289 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 252 37 289
106 252 switch (f) {
107 37 case PB_SPAN_TRACE_ID: {
108 const uint8_t *p; size_t n;
109 74 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 37 37
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
110 37 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 37 37
111 37 po_id16(p, n, &tr_hi, &tr_lo); have_trace = 1;
112 37 break;
113 }
114 37 case PB_SPAN_SPAN_ID: {
115 const uint8_t *p; size_t n;
116 74 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 37 37
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
117 37 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 37 37
118 37 span = po_id8(p, n);
119 37 break;
120 }
121 1 case PB_SPAN_PARENT_SPAN_ID: {
122 const uint8_t *p; size_t n;
123 2 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
124 1 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 1 1
125 1 parent = po_id8(p, n);
126 1 break;
127 }
128 37 case PB_SPAN_NAME:
129 37 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 37 37
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
130 37 ▶ if (!po_pbr_bytes(r, &namep, &namen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &namep, &namen)) return 0; 0 37 37
131 37 break;
132 17 case PB_SPAN_KIND:
133 17 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 17 17
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
134 17 ▶ if (!po_pbr_int32(r, &kind)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_int32(r, &kind)) return 0; 0 17 17
135 17 break;
136 /* START and END are FIXED64, not varint. Reading one as a varint
137 * produces garbage rather than an error, which is the worst kind
138 * of wrong, so the wire type is checked rather than assumed. */
139 37 case PB_SPAN_START_TIME:
140 37 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 37 37
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
141 37 ▶ if (!po_pbr_fixed64(r, &t_start)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t_start)) return 0; 0 37 37
142 37 break;
143 37 case PB_SPAN_END_TIME:
144 37 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 37 37
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
145 37 ▶ if (!po_pbr_fixed64(r, &t_end)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t_end)) return 0; 0 37 37
146 37 break;
147 32 case PB_SPAN_ATTRIBUTES:
148 32 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 32 32
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
149 32 ▶ if (!po_attrs_read(r, &at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attrs_read(r, &at)) return 0; 0 32 32
150 32 break;
151 17 case PB_SPAN_STATUS: {
152 po_pbr st; uint32_t sf, sw;
153 34 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 17 17
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
154 17 ▶ if (!po_pbr_sub(r, &st)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &st)) return 0; 0 17 17
155 34 ▶ while (po_pbr_next(&st, &sf, &sw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&st, &sf, &sw)) { 17 17 34
156 17 ▶ if (sf == PB_STATUS_CODE && sw == PO_PB_VARINT) {
Branch: 2 branches
BranchTrueFalseTotal
if (sf == PB_STATUS_CODE && sw == PO_PB_VARINT) { 17 0 17
if (sf == PB_STATUS_CODE && sw == PO_PB_VARINT) { 17 0 17
157 17 ▶ if (!po_pbr_int32(&st, &status)) { po_pbr_join(r, &st); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_int32(&st, &status)) { po_pbr_join(r, &st); return 0; } 0 17 17
158 }
159 0 ▶ else if (!po_pbr_skip(&st, sw)) { po_pbr_join(r, &st); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&st, sw)) { po_pbr_join(r, &st); return 0; } 0 0 0
160 }
161 17 po_pbr_join(r, &st);
162 17 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 17 17
163 17 break;
164 }
165 0 default:
166 0 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 0 0
167 }
168 }
169 37 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 37 37
170  
171 /* An all-zero trace id is invalid and OTLP says so. It arrives anyway,
172 * from a misconfigured propagator, and a "trace" collecting every
173 * unparented span from a broken deployment is a landmine in the UI. */
174 37 ▶ if (have_trace && !po_trace_id_valid(tr_hi, tr_lo)) {
Branch: 3 branches
BranchTrueFalseTotal
if (have_trace && !po_trace_id_valid(tr_hi, tr_lo)) { 37 0 37
if (have_trace && !po_trace_id_valid(tr_hi, tr_lo)) { 2 35 37
if (have_trace && !po_trace_id_valid(tr_hi, tr_lo)) { 2 0 2
175 2 b->dropped_bad_trace++;
176 2 return 1;
177 }
178  
179 35 ▶ if (!(rec = po_batch_next(b))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return 0; 0 35 35
180 35 rec->kind = PO_SPAN;
181 35 rec->t_unix_nano = t_start;
182 35 rec->trace_id_hi = tr_hi;
183 35 rec->trace_id_lo = tr_lo;
184 35 rec->span_id = span;
185 35 rec->parent_span_id = parent;
186 35 ▶ if (have_trace) rec->flags |= PO_F_HAS_TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if (have_trace) rec->flags |= PO_F_HAS_TRACE; 35 0 35
187 35 ▶ if (parent) rec->flags |= PO_F_HAS_PARENT;
Branch: 1 branch
BranchTrueFalseTotal
if (parent) rec->flags |= PO_F_HAS_PARENT; 1 34 35
188  
189 /* end < start means the wall clock stepped. In a uint64_t that
190 * subtraction is about 1.8e19, not a small negative number. */
191 35 ▶ 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; 34 1 35
192 1 else { rec->dur_nano = 0; rec->flags |= PO_F_CLAMPED_DUR; b->clamped_durations++; }
193  
194 35 po_rec_set_aux(rec, (uint8_t)kind, (uint8_t)status);
195  
196 35 ▶ if (namep) {
Branch: 1 branch
BranchTrueFalseTotal
if (namep) { 35 0 35
197 35 rec->body_off = po_arena_put(&b->arena, (const char *)namep, namen);
198 35 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 35 35
199 35 rec->body_len = (uint32_t)namen;
200 }
201 35 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
202 35 ▶ if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 35 35
203 35 ▶ if (at.dropped) rec->flags |= PO_F_TRUNCATED;
Branch: 1 branch
BranchTrueFalseTotal
if (at.dropped) rec->flags |= PO_F_TRUNCATED; 0 35 35
204 35 return 1;
205 }
206  
207 /* ---- logs ---------------------------------------------------------------- */
208  
209 5 static int po_logrec_read(po_pbr *r, po_batch *b, po_attrs *inherited) {
210 po_rec *rec;
211 5 po_attrs at = *inherited;
212 uint32_t f, w;
213 5 const uint8_t *bodyp = NULL; size_t bodyn = 0;
214 5 po_u64 t = 0, t_obs = 0, tr_hi = 0, tr_lo = 0, span = 0;
215 5 int32_t sev = 0;
216 5 int have_trace = 0;
217  
218 19 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 14 5 19
219 14 switch (f) {
220 4 case PB_LOGRECORD_TIME:
221 4 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 4 4
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
222 4 ▶ if (!po_pbr_fixed64(r, &t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t)) return 0; 0 4 4
223 4 break;
224 1 case PB_LOGRECORD_OBSERVED_TIME:
225 1 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
226 1 ▶ if (!po_pbr_fixed64(r, &t_obs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t_obs)) return 0; 0 1 1
227 1 break;
228 2 case PB_LOGRECORD_SEVERITY_NUMBER:
229 2 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
230 2 ▶ if (!po_pbr_int32(r, &sev)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_int32(r, &sev)) return 0; 0 2 2
231 2 break;
232 5 case PB_LOGRECORD_BODY: {
233 /* The body is an AnyValue. A string body is the common case
234 * and is stored as the record's body; anything else is
235 * flattened into the attributes under "body", because a log
236 * record whose body is a map is real and dropping it is not
237 * an option. */
238 po_pbr bv; uint32_t bf, bw;
239 const uint8_t *bstart; size_t blen;
240 10 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 5 5
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
241 5 ▶ if (!po_pbr_sub(r, &bv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &bv)) return 0; 0 5 5
242 5 bstart = bv.p;
243 5 blen = (size_t)(bv.end - bv.p);
244  
245 5 ▶ if (po_pbr_next(&bv, &bf, &bw)
Branch: 1 branch
BranchTrueFalseTotal
if (po_pbr_next(&bv, &bf, &bw) 5 0 5
246 5 ▶ && bf == PB_ANYVALUE_STRING && bw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
&& bf == PB_ANYVALUE_STRING && bw == PO_PB_BYTES) { 4 1 5
&& bf == PB_ANYVALUE_STRING && bw == PO_PB_BYTES) { 4 0 4
247 4 ▶ if (!po_pbr_bytes(&bv, &bodyp, &bodyn))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(&bv, &bodyp, &bodyn)) 0 4 4
248 0 { po_pbr_join(r, &bv); return 0; }
249 }
250 else {
251 /* Not a plain string. Re-read from the start as a generic
252 * AnyValue under the key "body", so a structured body is
253 * preserved and searchable instead of dropped. */
254 po_pbr again;
255 1 po_pbr_init(&again, bstart, blen);
256 1 ▶ if (!po_attr_value(&again, &at, "body", 4, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attr_value(&again, &at, "body", 4, 0)) 0 1 1
257 0 { po_pbr_join(r, &again); return 0; }
258 }
259 5 po_pbr_join(r, &bv);
260 5 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 5 5
261 5 break;
262 }
263 0 case PB_LOGRECORD_ATTRIBUTES:
264 0 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
265 0 ▶ if (!po_attrs_read(r, &at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attrs_read(r, &at)) return 0; 0 0 0
266 0 break;
267 1 case PB_LOGRECORD_TRACE_ID: {
268 const uint8_t *p; size_t n;
269 2 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
270 1 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 1 1
271 1 po_id16(p, n, &tr_hi, &tr_lo);
272 1 ▶ have_trace = po_trace_id_valid(tr_hi, tr_lo);
Branch: 2 branches
BranchTrueFalseTotal
have_trace = po_trace_id_valid(tr_hi, tr_lo); 0 1 1
have_trace = po_trace_id_valid(tr_hi, tr_lo); 0 0 0
273 1 break;
274 }
275 1 case PB_LOGRECORD_SPAN_ID: {
276 const uint8_t *p; size_t n;
277 2 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
278 1 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 1 1
279 1 span = po_id8(p, n);
280 1 break;
281 }
282 0 default:
283 0 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 0 0
284 }
285 }
286 5 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 5 5
287  
288 5 ▶ if (!(rec = po_batch_next(b))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return 0; 0 5 5
289 5 rec->kind = PO_LOG;
290 /* An absent time_unix_nano falls back to observed_time, which is what the
291 * spec intends and what every collector does. A record with neither is
292 * stamped at ingest by the caller, not here. */
293 5 ▶ rec->t_unix_nano = t ? t : t_obs;
Branch: 1 branch
BranchTrueFalseTotal
rec->t_unix_nano = t ? t : t_obs; 4 1 5
294 5 ▶ 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)); 5 0 5
rec->severity = (uint16_t)(sev < 0 ? 0 : (sev > 24 ? 24 : sev)); 5 0 5
295 5 rec->trace_id_hi = tr_hi;
296 5 rec->trace_id_lo = tr_lo;
297 5 rec->span_id = span;
298 5 ▶ if (have_trace) rec->flags |= PO_F_HAS_TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if (have_trace) rec->flags |= PO_F_HAS_TRACE; 1 4 5
299  
300 5 ▶ if (bodyp) {
Branch: 1 branch
BranchTrueFalseTotal
if (bodyp) { 4 1 5
301 4 rec->body_off = po_arena_put(&b->arena, (const char *)bodyp, bodyn);
302 4 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 4 4
303 4 rec->body_len = (uint32_t)bodyn;
304 }
305 5 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
306 5 ▶ if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 5 5
307 5 ▶ if (at.dropped) rec->flags |= PO_F_TRUNCATED;
Branch: 1 branch
BranchTrueFalseTotal
if (at.dropped) rec->flags |= PO_F_TRUNCATED; 0 5 5
308 5 return 1;
309 }
310  
311 /* ---- metrics ------------------------------------------------------------- */
312  
313 /* ---- exemplars ------------------------------------------------------------
314 *
315 * THE WHOLE CROSS-SIGNAL JUMP IS THIS FIELD. An exemplar is the (trace id,
316 * span id) recorded alongside a metric point, and it is the only thing that
317 * ties "the p99 went up at 14:05" to a request anybody can read. Dropped, the
318 * `| exemplars | traces | logs` pipeline has nothing to re-key on and cannot
319 * be executed at all - which is exactly why it could not be, until this.
320 *
321 * ONE EXEMPLAR PER POINT REACHES THE RECORD, and the record has one slot for
322 * it: `po_rec` carries a single trace id on every kind. A wire point may
323 * carry several (the SDK next door keeps a reservoir of four), so the FIRST
324 * one bearing a valid trace id wins and the rest are counted, never silently
325 * discarded. One trace per point is the jump; four would be four slots on
326 * eighty-eight bytes for a rounding of the same answer.
327 */
328 7 static int po_exemplar_read(po_pbr *r, po_u64 *hi, po_u64 *lo, po_u64 *span,
329 double *value) {
330 uint32_t f, w;
331 20 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 13 7 20
332 13 switch (f) {
333 /* The VALUE is read only where it decides something: a histogram
334 * exemplar belongs to the bucket its value falls in, and there is
335 * no bucket index on the wire to say which. */
336 5 case PB_EXEMPLAR_AS_DOUBLE: {
337 5 double dv = 0;
338 10 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 5 5
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
339 5 ▶ if (!po_pbr_double(r, &dv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &dv)) return 0; 0 5 5
340 5 ▶ if (value) *value = dv;
Branch: 1 branch
BranchTrueFalseTotal
if (value) *value = dv; 2 3 5
341 5 break;
342 }
343 0 case PB_EXEMPLAR_AS_INT: {
344 0 po_u64 iv = 0;
345 0 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
346 0 ▶ if (!po_pbr_varint(r, &iv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &iv)) return 0; 0 0 0
347 0 ▶ if (value) *value = (double)iv;
Branch: 1 branch
BranchTrueFalseTotal
if (value) *value = (double)iv; 0 0 0
348 0 break;
349 }
350 5 case PB_EXEMPLAR_TRACE_ID: {
351 const uint8_t *p; size_t n;
352 10 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 5 5
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
353 5 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 5 5
354 5 po_id16(p, n, hi, lo);
355 5 break;
356 }
357 2 case PB_EXEMPLAR_SPAN_ID: {
358 const uint8_t *p; size_t n;
359 4 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
360 2 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 2 2
361 2 *span = po_id8(p, n);
362 2 break;
363 }
364 /* The value and the timestamp are the point's own, and the
365 * filtered attributes are the ones the aggregation dropped -
366 * none of the three tells us which trace to open, so all three
367 * are skipped rather than stored twice. */
368 1 default:
369 1 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 1 1
370 }
371 }
372 7 return !r->err;
373 }
374  
375 /* Reads one exemplar submessage into the caller's locals, if it has no trace
376 * id yet. The point's record does not exist until its field loop has finished
377 * - the name can follow the value on the wire - so the ids are collected here
378 * and attached when the record is made. Returns 1 when the field was
379 * consumed; the caller's switch has already decided this is the exemplars
380 * field. */
381 6 static int po_exemplar_take(po_pbr *r, uint32_t w,
382 po_u64 *hi, po_u64 *lo, po_u64 *span) {
383 const uint8_t *p; size_t n;
384 po_pbr sub;
385 6 po_u64 h = 0, l = 0, s = 0;
386  
387 6 ▶ if (w != PO_PB_BYTES) return po_pbr_skip(r, w);
Branch: 1 branch
BranchTrueFalseTotal
if (w != PO_PB_BYTES) return po_pbr_skip(r, w); 0 6 6
388 6 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 6 6
389 /* Already carrying one: the first valid exemplar is the point's, and
390 * this one is read past rather than parsed. */
391 6 ▶ if (po_trace_id_valid(*hi, *lo)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (po_trace_id_valid(*hi, *lo)) return 1; 5 1 6
if (po_trace_id_valid(*hi, *lo)) return 1; 0 5 5
392  
393 5 po_pbr_init(&sub, p, n);
394 5 ▶ if (!po_exemplar_read(&sub, &h, &l, &s, NULL)) return 1; /* a bad one is not fatal */
Branch: 1 branch
BranchTrueFalseTotal
if (!po_exemplar_read(&sub, &h, &l, &s, NULL)) return 1; /* a bad one is not fatal */ 0 5 5
395 /* AN EXEMPLAR WITH NO TRACE ID POINTS NOWHERE. OTLP allows one - the
396 * value and timestamp alone are legal - and storing it would give a
397 * metric point a trace id of zero, which `po_trace_id_valid` already
398 * treats as the broken-propagator case everywhere else. */
399 5 ▶ if (!po_trace_id_valid(h, l)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!po_trace_id_valid(h, l)) return 1; 2 3 5
if (!po_trace_id_valid(h, l)) return 1; 2 0 2
400 3 *hi = h; *lo = l; *span = s;
401 3 return 1;
402 }
403  
404 /* One NumberDataPoint. `flags` carries the temporality and monotonic bits
405 * already resolved by the caller, because they live on the wrapper message
406 * rather than on the point. */
407 /* ---- histograms, summaries and exponential histograms ---------------------
408 *
409 * A HISTOGRAM POINT IS NOT ONE VALUE, so it cannot be one record. It carries a
410 * count, a sum, and a bucket array - and the whole reason anybody records one
411 * is that those buckets let a percentile be computed EXACTLY over any range,
412 * which is the promise the rollup tier makes and could not keep while this
413 * decoder skipped them.
414 *
415 * So a point is EXPLODED into records that the existing series model already
416 * knows how to store:
417 *
418 * <name>_bucket {le="0.005"} cumulative count at or below that bound
419 * <name>_bucket {le="+Inf"} the total, which is also the count
420 * <name>_sum the sum of observations
421 * <name>_count how many there were
422 *
423 * That is the convention Prometheus made ordinary, and it is chosen here for a
424 * structural reason rather than familiarity: every one of those is a plain
425 * (timestamp, double) series, so histograms need no new chunk format, no new
426 * index and no new merge rule. They get content-derived series ids, Gorilla
427 * compression, the postings index and the rollup for free.
428 *
429 * The cost is honest and worth naming: N buckets is N series. A histogram with
430 * fifty buckets and ten routes is five hundred series, which is exactly what
431 * the cardinality cap exists to bound - and the cap counts them, so the
432 * operator sees it rather than discovering it.
433 */
434  
435 /* One derived series from a histogram point: the name gains a suffix and,
436 * for a bucket, one more attribute. */
437 /* The exemplars one histogram point carried, kept until the buckets are
438 * emitted: an exemplar belongs to the bucket its VALUE falls in, and the
439 * buckets are not built until the whole point has been read. */
440 #define PO_HIST_EX_MAX 8
441 typedef struct {
442 double value;
443 po_u64 hi, lo, span;
444 } po_hist_ex;
445  
446 96 static int po_hist_emit(po_batch *b, const po_attrs *base,
447 const uint8_t *namep, size_t namen,
448 const char *suffix, size_t suflen,
449 const char *lekey, const char *leval, size_t levlen,
450 double value, po_u64 t, uint16_t mflags,
451 const po_hist_ex *ex) {
452 po_rec *rec;
453 96 po_attrs at = *base;
454 char nm[PO_ATTR_KEYMAX];
455 size_t nl;
456  
457 96 ▶ if (lekey) {
Branch: 1 branch
BranchTrueFalseTotal
if (lekey) { 60 36 96
458 60 po_attr *a = po_attrs_push(&at, lekey, strlen(lekey));
459 /* BORROWED, like every other attribute value. The caller's buffer
460 * outlives this call, and po_attrs_encode copies into the arena
461 * before it goes anywhere. */
462 60 ▶ if (a) {
Branch: 1 branch
BranchTrueFalseTotal
if (a) { 60 0 60
463 60 a->tag = PO_AV_STRING;
464 60 a->sp = (const uint8_t *)leval;
465 60 a->slen = levlen;
466 }
467 }
468  
469 96 ▶ if (!(rec = po_batch_next(b))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return 0; 0 96 96
470 96 rec->kind = PO_METRIC;
471 96 rec->t_unix_nano = t;
472 96 rec->flags = mflags;
473 96 rec->value = value;
474 96 ▶ if (ex) {
Branch: 1 branch
BranchTrueFalseTotal
if (ex) { 2 94 96
475 2 rec->trace_id_hi = ex->hi;
476 2 rec->trace_id_lo = ex->lo;
477 2 rec->span_id = ex->span;
478 }
479  
480 96 nl = namen < sizeof(nm) - suflen - 1 ? namen : sizeof(nm) - suflen - 1;
481 96 ▶ if (namep && nl) memcpy(nm, namep, nl); else nl = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (namep && nl) memcpy(nm, namep, nl); else nl = 0; 96 0 96
if (namep && nl) memcpy(nm, namep, nl); else nl = 0; 96 0 96
482 96 memcpy(nm + nl, suffix, suflen);
483 96 rec->body_off = po_arena_put(&b->arena, nm, nl + suflen);
484 96 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 96 96
485 96 rec->body_len = (uint32_t)(nl + suflen);
486  
487 96 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
488 96 ▶ if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 96 96
489 96 ▶ if (at.dropped) rec->flags |= PO_F_TRUNCATED;
Branch: 1 branch
BranchTrueFalseTotal
if (at.dropped) rec->flags |= PO_F_TRUNCATED; 0 96 96
490 96 return 1;
491 }
492  
493 /* A bound as text, for the `le` attribute.
494 *
495 * Hand-formatted for the reason po_svg.h is: %f in a Perl-flavoured formatter
496 * reads an NV, and this string becomes part of a SERIES IDENTITY - two
497 * spellings of the same bound are two series that never merge, so the
498 * rendering has to be stable rather than merely readable. */
499 44 static size_t po_le_str(double v, char *out) {
500 44 size_t n = 0;
501 po_u64 whole, frac;
502 44 int neg = 0;
503  
504 44 ▶ if (!(v == v)) { memcpy(out, "NaN", 3); return 3; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(v == v)) { memcpy(out, "NaN", 3); return 3; } 0 44 44
505 44 ▶ if (v > 1e18) { memcpy(out, "+Inf", 4); return 4; }
Branch: 1 branch
BranchTrueFalseTotal
if (v > 1e18) { memcpy(out, "+Inf", 4); return 4; } 0 44 44
506 44 ▶ if (v < -1e18) { memcpy(out, "-Inf", 4); return 4; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < -1e18) { memcpy(out, "-Inf", 4); return 4; } 0 44 44
507 44 ▶ if (v < 0) { neg = 1; v = -v; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { neg = 1; v = -v; } 0 44 44
508  
509 44 whole = (po_u64)v;
510 44 frac = (po_u64)((v - (double)whole) * 1000000.0 + 0.5);
511 44 ▶ if (frac >= 1000000) { whole++; frac -= 1000000; }
Branch: 1 branch
BranchTrueFalseTotal
if (frac >= 1000000) { whole++; frac -= 1000000; } 0 44 44
512  
513 44 ▶ if (neg) out[n++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) out[n++] = '-'; 0 44 44
514 44 ▶ if (!whole) out[n++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!whole) out[n++] = '0'; 18 26 44
515 else {
516 26 char t[24]; size_t k = 0;
517 59 ▶ while (whole) { t[k++] = (char)('0' + (int)(whole % 10)); whole /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (whole) { t[k++] = (char)('0' + (int)(whole % 10)); whole /= 10; } 33 26 59
518 59 ▶ while (k) out[n++] = t[--k];
Branch: 1 branch
BranchTrueFalseTotal
while (k) out[n++] = t[--k]; 33 26 59
519 }
520 44 ▶ if (frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (frac) { 22 22 44
521 char t[8]; int k;
522 22 out[n++] = '.';
523 154 ▶ for (k = 5; k >= 0; k--) { t[k] = (char)('0' + (int)(frac % 10)); frac /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
for (k = 5; k >= 0; k--) { t[k] = (char)('0' + (int)(frac % 10)); frac /= 10; } 132 22 154
524 { /* trailing zeroes are noise, and a differently-trimmed bound is a
525 * different series id */
526 22 int last = 5;
527 119 ▶ while (last > 0 && t[last] == '0') last--;
Branch: 2 branches
BranchTrueFalseTotal
while (last > 0 && t[last] == '0') last--; 106 13 119
while (last > 0 && t[last] == '0') last--; 97 9 106
528 57 ▶ for (k = 0; k <= last; k++) out[n++] = t[k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k <= last; k++) out[n++] = t[k]; 35 22 57
529 }
530 }
531 44 return n;
532 }
533  
534 #define PO_HIST_MAX_BUCKETS 256
535  
536 19 static int po_hdp_read(po_pbr *r, po_batch *b, po_attrs *inherited,
537 const uint8_t *namep, size_t namen, uint16_t mflags) {
538 19 po_attrs at = *inherited;
539 uint32_t f, w;
540 19 po_u64 t = 0, count = 0;
541 19 double sum = 0, mn = 0, mx = 0;
542 19 int have_sum = 0, have_min = 0, have_max = 0;
543 po_u64 counts[PO_HIST_MAX_BUCKETS];
544 double bounds[PO_HIST_MAX_BUCKETS];
545 19 uint32_t ncounts = 0, nbounds = 0;
546 uint32_t i;
547 19 po_u64 cumulative = 0;
548 po_hist_ex exs[PO_HIST_EX_MAX];
549 19 uint32_t nex = 0;
550  
551 119 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 100 19 119
552 100 switch (f) {
553 19 case PB_HDP_TIME:
554 19 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 19 19
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
555 19 ▶ if (!po_pbr_fixed64(r, &t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t)) return 0; 0 19 19
556 19 break;
557 19 case PB_HDP_COUNT:
558 /* FIXED64, not a varint. `count` is one of the few integer
559 * fields OTLP declares fixed-width, and reading it as a
560 * varint skips it silently - the point still decodes, with a
561 * count of zero, so every percentile computed from it is
562 * divided by nothing.
563 *
564 * A varint is accepted too, because an encoder that wrote one
565 * meant a count and there is nothing to gain by losing it. */
566 19 ▶ if (w == PO_PB_FIXED64) { if (!po_pbr_fixed64(r, &count)) return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (w == PO_PB_FIXED64) { if (!po_pbr_fixed64(r, &count)) return 0; } 1 18 19
if (w == PO_PB_FIXED64) { if (!po_pbr_fixed64(r, &count)) return 0; } 0 1 1
567 18 ▶ else if (w == PO_PB_VARINT) { if (!po_pbr_varint(r, &count)) return 0; }
Branch: 2 branches
BranchTrueFalseTotal
else if (w == PO_PB_VARINT) { if (!po_pbr_varint(r, &count)) return 0; } 18 0 18
else if (w == PO_PB_VARINT) { if (!po_pbr_varint(r, &count)) return 0; } 0 18 18
568 0 ▶ else if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(r, w)) return 0; 0 0 0
569 19 break;
570 17 case PB_HDP_SUM:
571 17 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 17 17
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
572 17 ▶ if (!po_pbr_double(r, &sum)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &sum)) return 0; 0 17 17
573 17 have_sum = 1;
574 17 break;
575 2 case PB_HDP_MIN:
576 2 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
577 2 ▶ if (!po_pbr_double(r, &mn)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &mn)) return 0; 0 2 2
578 2 have_min = 1;
579 2 break;
580 2 case PB_HDP_MAX:
581 2 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
582 2 ▶ if (!po_pbr_double(r, &mx)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &mx)) return 0; 0 2 2
583 2 have_max = 1;
584 2 break;
585 18 case PB_HDP_BUCKET_COUNTS: {
586 /* Packed OR unpacked: proto3 says a repeated scalar may
587 * arrive either way, and a reader that assumes packed loses
588 * every bucket from an encoder that did not pack. */
589 po_pb_packed it;
590 36 ▶ if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; } 0 18 18
if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
591 {
592 po_u64 v;
593 101 ▶ while (po_pb_packed_varint(r, &it, &v))
Branch: 1 branch
BranchTrueFalseTotal
while (po_pb_packed_varint(r, &it, &v)) 65 18 83
594 65 ▶ if (ncounts < PO_HIST_MAX_BUCKETS) counts[ncounts++] = v;
Branch: 1 branch
BranchTrueFalseTotal
if (ncounts < PO_HIST_MAX_BUCKETS) counts[ncounts++] = v; 0 65 65
595 }
596 18 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 18 18
597 18 break;
598 }
599 18 case PB_HDP_EXPLICIT_BOUNDS: {
600 po_pb_packed it;
601 36 ▶ if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; } 0 18 18
if (!po_pb_packed_begin(r, w, &it)) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
602 {
603 double v;
604 83 ▶ while (po_pb_packed_double(r, &it, &v))
Branch: 1 branch
BranchTrueFalseTotal
while (po_pb_packed_double(r, &it, &v)) 47 18 65
605 47 ▶ if (nbounds < PO_HIST_MAX_BUCKETS) bounds[nbounds++] = v;
Branch: 1 branch
BranchTrueFalseTotal
if (nbounds < PO_HIST_MAX_BUCKETS) bounds[nbounds++] = v; 0 47 47
606 }
607 18 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 18 18
608 18 break;
609 }
610 2 case PB_HDP_ATTRIBUTES:
611 2 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
612 2 ▶ if (!po_attrs_read(r, &at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attrs_read(r, &at)) return 0; 0 2 2
613 2 break;
614 2 case PB_HDP_EXEMPLARS: {
615 const uint8_t *p; size_t n;
616 po_pbr sub;
617 po_hist_ex e;
618 4 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
619 2 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 2 2
620 2 ▶ if (nex >= PO_HIST_EX_MAX) break; /* kept bounded, not grown */
Branch: 1 branch
BranchTrueFalseTotal
if (nex >= PO_HIST_EX_MAX) break; /* kept bounded, not grown */ 0 2 2
621 2 memset(&e, 0, sizeof(e));
622 2 po_pbr_init(&sub, p, n);
623 2 ▶ if (!po_exemplar_read(&sub, &e.hi, &e.lo, &e.span, &e.value))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_exemplar_read(&sub, &e.hi, &e.lo, &e.span, &e.value)) 0 2 2
624 0 break; /* a bad one is not fatal */
625 2 ▶ if (!po_trace_id_valid(e.hi, e.lo)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!po_trace_id_valid(e.hi, e.lo)) break; 0 2 2
if (!po_trace_id_valid(e.hi, e.lo)) break; 0 0 0
626 2 exs[nex++] = e;
627 2 break;
628 }
629 1 default:
630 1 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 1 1
631 }
632 }
633 19 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 19 19
634  
635 /* THE BUCKET ARRAY IS ONE LONGER THAN THE BOUNDS ARRAY - the last bucket
636 * is everything above the highest bound. A point where that does not hold
637 * is malformed, and half-decoding it would produce a histogram whose
638 * percentiles are quietly wrong. */
639 19 ▶ if (ncounts && ncounts != nbounds + 1) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ncounts && ncounts != nbounds + 1) return 1; 18 1 19
if (ncounts && ncounts != nbounds + 1) return 1; 2 16 18
640  
641 77 ▶ for (i = 0; i < ncounts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ncounts; i++) { 60 17 77
642 char le[32];
643 size_t len;
644 60 const po_hist_ex *mine = NULL;
645 uint32_t k;
646  
647 /* CUMULATIVE counts, not per-bucket. That is what makes a percentile
648 * a search rather than a sum, and what lets two points merge. */
649 60 cumulative += counts[i];
650 44 len = i < nbounds ? po_le_str(bounds[i], le)
651 60 ▶ : (memcpy(le, "+Inf", 4), 4);
Branch: 1 branch
BranchTrueFalseTotal
: (memcpy(le, "+Inf", 4), 4); 44 16 60
652  
653 /* THE EXEMPLAR BELONGS TO ONE BUCKET, NOT TO ALL OF THEM. One wire
654 * point becomes a record per bucket here, and copying the trace id
655 * onto every one would claim the request was observed at fifteen
656 * different latencies. The wire carries no bucket index, so the
657 * bucket is the one the exemplar's own VALUE falls in - the first
658 * bound at or above it, and the +Inf bucket for anything over the
659 * top. Cumulative counts do not change that: the value fell where it
660 * fell. */
661 66 ▶ for (k = 0; k < nex; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nex; k++) { 8 58 66
662 8 int in_bucket = (i < nbounds)
663 6 ? (exs[k].value <= bounds[i]
664 6 ▶ && (i == 0 || exs[k].value > bounds[i - 1]))
Branch: 3 branches
BranchTrueFalseTotal
&& (i == 0 || exs[k].value > bounds[i - 1])) 1 5 6
&& (i == 0 || exs[k].value > bounds[i - 1])) 1 0 1
&& (i == 0 || exs[k].value > bounds[i - 1])) 1 0 1
665 14 ▶ : (nbounds == 0 || exs[k].value > bounds[nbounds - 1]);
Branch: 3 branches
BranchTrueFalseTotal
: (nbounds == 0 || exs[k].value > bounds[nbounds - 1]); 6 2 8
: (nbounds == 0 || exs[k].value > bounds[nbounds - 1]); 2 0 2
: (nbounds == 0 || exs[k].value > bounds[nbounds - 1]); 1 1 2
666 8 ▶ if (in_bucket) { mine = &exs[k]; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (in_bucket) { mine = &exs[k]; break; } 2 6 8
667 }
668  
669 60 ▶ if (!po_hist_emit(b, &at, namep, namen, "_bucket", 7,
Branch: 1 branch
BranchTrueFalseTotal
if (!po_hist_emit(b, &at, namep, namen, "_bucket", 7, 0 60 60
670 "le", le, len, (double)cumulative, t, mflags, mine))
671 0 return 0;
672 }
673  
674 /* The derived aggregates carry no exemplar: `_sum` and `_count` are not
675 * observations, they are arithmetic over all of them, and a trace id on
676 * one would name a single request as the source of the total. */
677 17 ▶ if (have_sum && !po_hist_emit(b, &at, namep, namen, "_sum", 4,
Branch: 2 branches
BranchTrueFalseTotal
if (have_sum && !po_hist_emit(b, &at, namep, namen, "_sum", 4, 15 2 17
if (have_sum && !po_hist_emit(b, &at, namep, namen, "_sum", 4, 0 15 15
678 0 NULL, NULL, 0, sum, t, mflags, NULL)) return 0;
679 17 ▶ if (!po_hist_emit(b, &at, namep, namen, "_count", 6,
Branch: 1 branch
BranchTrueFalseTotal
if (!po_hist_emit(b, &at, namep, namen, "_count", 6, 0 17 17
680 0 NULL, NULL, 0, (double)count, t, mflags, NULL)) return 0;
681 17 ▶ if (have_min && !po_hist_emit(b, &at, namep, namen, "_min", 4,
Branch: 2 branches
BranchTrueFalseTotal
if (have_min && !po_hist_emit(b, &at, namep, namen, "_min", 4, 2 15 17
if (have_min && !po_hist_emit(b, &at, namep, namen, "_min", 4, 0 2 2
682 0 NULL, NULL, 0, mn, t, mflags, NULL)) return 0;
683 17 ▶ if (have_max && !po_hist_emit(b, &at, namep, namen, "_max", 4,
Branch: 2 branches
BranchTrueFalseTotal
if (have_max && !po_hist_emit(b, &at, namep, namen, "_max", 4, 2 15 17
if (have_max && !po_hist_emit(b, &at, namep, namen, "_max", 4, 0 2 2
684 0 NULL, NULL, 0, mx, t, mflags, NULL)) return 0;
685 17 return 1;
686 }
687  
688 13 static int po_ndp_read(po_pbr *r, po_batch *b, po_attrs *inherited,
689 const uint8_t *namep, size_t namen, uint16_t mflags) {
690 po_rec *rec;
691 13 po_attrs at = *inherited;
692 uint32_t f, w;
693 13 po_u64 t = 0;
694 13 double dv = 0; po_u64 iv = 0;
695 13 po_u64 ex_hi = 0, ex_lo = 0, ex_span = 0;
696 13 int is_int = 0, have_val = 0;
697  
698 45 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 32 13 45
699 32 switch (f) {
700 13 case PB_NDP_TIME:
701 13 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 13 13
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
702 13 ▶ if (!po_pbr_fixed64(r, &t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &t)) return 0; 0 13 13
703 13 break;
704 /* as_double is fixed64, as_int is a VARINT (sfixed64 would be
705 * fixed64; the schema says int64, so varint it is). A point
706 * carries one or the other, never both, and real series switch
707 * representation mid-stream. */
708 11 case PB_NDP_AS_DOUBLE:
709 11 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 11 11
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
710 11 ▶ if (!po_pbr_double(r, &dv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &dv)) return 0; 0 11 11
711 11 is_int = 0; have_val = 1;
712 11 break;
713 2 case PB_NDP_AS_INT:
714 2 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 2 2
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
715 2 ▶ if (!po_pbr_varint(r, &iv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &iv)) return 0; 0 2 2
716 2 is_int = 1; have_val = 1;
717 2 break;
718 0 case PB_NDP_ATTRIBUTES:
719 0 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
720 0 ▶ if (!po_attrs_read(r, &at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attrs_read(r, &at)) return 0; 0 0 0
721 0 break;
722 6 case PB_NDP_EXEMPLARS:
723 6 ▶ if (!po_exemplar_take(r, w, &ex_hi, &ex_lo, &ex_span)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_exemplar_take(r, w, &ex_hi, &ex_lo, &ex_span)) return 0; 0 6 6
724 6 break;
725 0 default:
726 0 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 0 0
727 }
728 }
729 13 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 13 13
730 13 ▶ if (!have_val) return 1; /* a point with no value is nothing */
Branch: 1 branch
BranchTrueFalseTotal
if (!have_val) return 1; /* a point with no value is nothing */ 0 13 13
731  
732 13 ▶ if (!(rec = po_batch_next(b))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(rec = po_batch_next(b))) return 0; 0 13 13
733 13 rec->kind = PO_METRIC;
734 13 rec->t_unix_nano = t;
735 13 rec->flags |= mflags;
736 13 rec->trace_id_hi = ex_hi;
737 13 rec->trace_id_lo = ex_lo;
738 13 rec->span_id = ex_span;
739 13 ▶ if (is_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int) { 2 11 13
740 2 rec->flags |= PO_F_VALUE_IS_INT;
741 /* The int is kept exactly, as a bit pattern in the double slot. A
742 * cast to double loses integers above 2^53, and a counter that big
743 * is exactly the one somebody is watching. */
744 2 memcpy(&rec->value, &iv, 8);
745 }
746 11 else rec->value = dv;
747  
748 13 ▶ if (namep) {
Branch: 1 branch
BranchTrueFalseTotal
if (namep) { 13 0 13
749 13 rec->body_off = po_arena_put(&b->arena, (const char *)namep, namen);
750 13 ▶ if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 13 13
751 13 rec->body_len = (uint32_t)namen;
752 }
753 13 rec->attr_off = po_attrs_encode(&at, &b->arena, &rec->attr_len);
754 13 ▶ if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_off == PO_ARENA_ERR) { b->err = 1; return 0; } 0 13 13
755 13 ▶ if (at.dropped) rec->flags |= PO_F_TRUNCATED;
Branch: 1 branch
BranchTrueFalseTotal
if (at.dropped) rec->flags |= PO_F_TRUNCATED; 0 13 13
756 13 return 1;
757 }
758  
759 34 static int po_metric_read(po_pbr *r, po_batch *b, po_attrs *inherited) {
760 uint32_t f, w;
761 34 const uint8_t *namep = NULL; size_t namen = 0;
762 /* The name precedes the data on the wire in practice, but nothing
763 * guarantees it, so the points are collected after a full pass finds the
764 * name. Two passes over one small message is cheaper than being wrong. */
765 34 const uint8_t *save_p = r->p;
766 34 const uint8_t *save_end = r->end;
767  
768 102 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 68 34 102
769 68 ▶ if (f == PB_METRIC_NAME && w == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (f == PB_METRIC_NAME && w == PO_PB_BYTES) { 34 34 68
if (f == PB_METRIC_NAME && w == PO_PB_BYTES) { 34 0 34
770 34 ▶ if (!po_pbr_bytes(r, &namep, &namen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &namep, &namen)) return 0; 0 34 34
771 }
772 34 ▶ else if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(r, w)) return 0; 0 34 34
773 }
774 34 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 34 34
775  
776 34 po_pbr_init(r, save_p, (size_t)(save_end - save_p));
777  
778 102 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 68 34 102
779 68 uint16_t mflags = 0;
780 68 int is_sum = 0;
781 68 switch (f) {
782 9 case PB_METRIC_SUM: is_sum = 1; /* fall through */
783 13 case PB_METRIC_GAUGE: {
784 po_pbr d; uint32_t df, dw;
785 13 po_u64 temporality = 0; po_u64 monotonic = 0;
786 const uint8_t *dp_start;
787 26 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 13 13
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
788 13 ▶ if (!po_pbr_sub(r, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &d)) return 0; 0 13 13
789 13 dp_start = d.p;
790  
791 13 ▶ if (is_sum) { /* the wrapper's own fields first */
Branch: 1 branch
BranchTrueFalseTotal
if (is_sum) { /* the wrapper's own fields first */ 9 4 13
792 36 ▶ while (po_pbr_next(&d, &df, &dw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&d, &df, &dw)) { 27 9 36
793 27 ▶ if (df == PB_SUM_TEMPORALITY && dw == PO_PB_VARINT) {
Branch: 2 branches
BranchTrueFalseTotal
if (df == PB_SUM_TEMPORALITY && dw == PO_PB_VARINT) { 9 18 27
if (df == PB_SUM_TEMPORALITY && dw == PO_PB_VARINT) { 9 0 9
794 9 ▶ if (!po_pbr_varint(&d, &temporality)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(&d, &temporality)) { po_pbr_join(r,&d); return 0; } 0 9 9
795 }
796 18 ▶ else if (df == PB_SUM_IS_MONOTONIC && dw == PO_PB_VARINT) {
Branch: 2 branches
BranchTrueFalseTotal
else if (df == PB_SUM_IS_MONOTONIC && dw == PO_PB_VARINT) { 9 9 18
else if (df == PB_SUM_IS_MONOTONIC && dw == PO_PB_VARINT) { 9 0 9
797 9 ▶ if (!po_pbr_varint(&d, &monotonic)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(&d, &monotonic)) { po_pbr_join(r,&d); return 0; } 0 9 9
798 }
799 9 ▶ else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; } 0 9 9
800 }
801 9 ▶ if (d.err) { po_pbr_join(r, &d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (d.err) { po_pbr_join(r, &d); return 0; } 0 9 9
802 9 ▶ if (po_temporality_is_cumulative(temporality))
Branch: 1 branch
BranchTrueFalseTotal
if (po_temporality_is_cumulative(temporality)) 8 1 9
803 8 mflags |= PO_F_CUMULATIVE;
804 9 ▶ if (monotonic) mflags |= PO_F_MONOTONIC;
Branch: 1 branch
BranchTrueFalseTotal
if (monotonic) mflags |= PO_F_MONOTONIC; 9 0 9
805 9 po_pbr_init(&d, dp_start, (size_t)(d.end - dp_start));
806 }
807  
808 44 ▶ while (po_pbr_next(&d, &df, &dw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&d, &df, &dw)) { 31 13 44
809 31 ▶ if ((df == PB_SUM_DATA_POINTS || df == PB_GAUGE_DATA_POINTS)
Branch: 2 branches
BranchTrueFalseTotal
if ((df == PB_SUM_DATA_POINTS || df == PB_GAUGE_DATA_POINTS) 18 13 31
if ((df == PB_SUM_DATA_POINTS || df == PB_GAUGE_DATA_POINTS) 0 18 18
810 26 ▶ && dw == PO_PB_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
&& dw == PO_PB_BYTES) { 13 0 13
811 po_pbr pt;
812 13 ▶ if (!po_pbr_sub(&d, &pt)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&d, &pt)) { po_pbr_join(r,&d); return 0; } 0 13 13
813 13 ▶ if (!po_ndp_read(&pt, b, inherited, namep, namen, mflags))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_ndp_read(&pt, b, inherited, namep, namen, mflags)) 0 13 13
814 0 { po_pbr_join(r,&d); return 0; }
815 }
816 18 ▶ else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; } 0 18 18
817 }
818 13 po_pbr_join(r, &d);
819 13 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 13 13
820 13 break;
821 }
822 21 case PB_METRIC_HISTOGRAM: {
823 po_pbr d; uint32_t df, dw;
824 21 po_u64 temporality = 0;
825 const uint8_t *dp_start;
826 42 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 21 21
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
827 21 ▶ if (!po_pbr_sub(r, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &d)) return 0; 0 21 21
828 21 dp_start = d.p;
829  
830 61 ▶ while (po_pbr_next(&d, &df, &dw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&d, &df, &dw)) { 40 21 61
831 40 ▶ if (df == PB_HIST_TEMPORALITY && dw == PO_PB_VARINT) {
Branch: 2 branches
BranchTrueFalseTotal
if (df == PB_HIST_TEMPORALITY && dw == PO_PB_VARINT) { 21 19 40
if (df == PB_HIST_TEMPORALITY && dw == PO_PB_VARINT) { 21 0 21
832 21 ▶ if (!po_pbr_varint(&d, &temporality)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(&d, &temporality)) { po_pbr_join(r,&d); return 0; } 0 21 21
833 }
834 19 ▶ else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; } 0 19 19
835 }
836 21 ▶ if (d.err) { po_pbr_join(r, &d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (d.err) { po_pbr_join(r, &d); return 0; } 0 21 21
837 21 ▶ if (po_temporality_is_cumulative(temporality))
Branch: 1 branch
BranchTrueFalseTotal
if (po_temporality_is_cumulative(temporality)) 20 1 21
838 20 mflags |= PO_F_CUMULATIVE;
839 /* A histogram's buckets are counts, and counts only go up
840 * within a point. Marking them monotonic is what lets rate()
841 * treat a cumulative histogram the way it treats a counter. */
842 21 mflags |= PO_F_MONOTONIC;
843 21 po_pbr_init(&d, dp_start, (size_t)(d.end - dp_start));
844  
845 61 ▶ while (po_pbr_next(&d, &df, &dw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&d, &df, &dw)) { 40 21 61
846 59 ▶ if (df == PB_HIST_DATA_POINTS && dw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (df == PB_HIST_DATA_POINTS && dw == PO_PB_BYTES) { 19 21 40
if (df == PB_HIST_DATA_POINTS && dw == PO_PB_BYTES) { 19 0 19
847 po_pbr pt;
848 19 ▶ if (!po_pbr_sub(&d, &pt)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&d, &pt)) { po_pbr_join(r,&d); return 0; } 0 19 19
849 19 ▶ if (!po_hdp_read(&pt, b, inherited, namep, namen, mflags))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_hdp_read(&pt, b, inherited, namep, namen, mflags)) 0 19 19
850 0 { po_pbr_join(r,&d); return 0; }
851 }
852 21 ▶ else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&d, dw)) { po_pbr_join(r,&d); return 0; } 0 21 21
853 }
854 21 po_pbr_join(r, &d);
855 21 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 21 21
856 21 break;
857 }
858 34 default:
859 /* Exponential histograms and summaries remain. An exponential
860 * histogram's buckets are base-2 rather than explicit, so the
861 * bounds have to be COMPUTED from a scale before they can
862 * become `le` labels; a summary carries pre-computed quantiles
863 * that cannot be merged across points at all, which makes it a
864 * different storage question rather than the same one.
865 *
866 * Skipped rather than half-decoded, and the skip still costs
867 * nothing. */
868 34 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 34 34
869 }
870 }
871 34 return !r->err;
872 }
873  
874 /* ---- the three request messages ------------------------------------------ */
875  
876 /* Each Export*ServiceRequest has one repeated field at number 1, and each
877 * Resource* has resource at 1, scope* at 2. The three walks are the same
878 * shape with different leaf readers, which is the whole reason one record
879 * type was worth insisting on. */
880 typedef int (*po_leaf_fn)(po_pbr *, po_batch *, po_attrs *);
881  
882 70 static int po_walk(po_pbr *r, po_batch *b, po_leaf_fn leaf,
883 uint32_t f_scope, uint32_t f_leaf) {
884 uint32_t f, w;
885 136 ▶ while (po_pbr_next(r, &f, &w)) { /* Resource* */
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { /* Resource* */ 67 69 136
886 67 ▶ if (f != 1 || w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (f != 1 || w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; continue; } 66 1 67
if (f != 1 || w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; continue; } 0 66 66
if (f != 1 || w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; continue; } 1 0 1
887 {
888 po_pbr rs; uint32_t rf, rw;
889 po_attrs res_attrs;
890 66 po_attrs_init(&res_attrs);
891 66 ▶ if (!po_pbr_sub(r, &rs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &rs)) return 0; 0 66 66
892  
893 /* resource attributes first: they are inherited by every record
894 * below, which is what makes service.name a label on a span. */
895 {
896 po_pbr scan; uint32_t sf, sw;
897 66 po_pbr_init(&scan, rs.p, (size_t)(rs.end - rs.p));
898 168 ▶ while (po_pbr_next(&scan, &sf, &sw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&scan, &sf, &sw)) { 102 66 168
899 140 ▶ if (sf == 1 && sw == PO_PB_BYTES) { /* Resource */
Branch: 2 branches
BranchTrueFalseTotal
if (sf == 1 && sw == PO_PB_BYTES) { /* Resource */ 38 64 102
if (sf == 1 && sw == PO_PB_BYTES) { /* Resource */ 38 0 38
900 po_pbr res; uint32_t af, aw;
901 38 ▶ if (!po_pbr_sub(&scan, &res)) { po_pbr_join(r,&scan); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&scan, &res)) { po_pbr_join(r,&scan); return 0; } 0 38 38
902 75 ▶ while (po_pbr_next(&res, &af, &aw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&res, &af, &aw)) { 37 38 75
903 37 ▶ if (af == PB_RESOURCE_ATTRIBUTES && aw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (af == PB_RESOURCE_ATTRIBUTES && aw == PO_PB_BYTES) { 37 0 37
if (af == PB_RESOURCE_ATTRIBUTES && aw == PO_PB_BYTES) { 37 0 37
904 37 ▶ if (!po_attrs_read(&res, &res_attrs))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attrs_read(&res, &res_attrs)) 0 37 37
905 0 { po_pbr_join(r,&res); return 0; }
906 }
907 0 ▶ else if (!po_pbr_skip(&res, aw)) { po_pbr_join(r,&res); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&res, aw)) { po_pbr_join(r,&res); return 0; } 0 0 0
908 }
909 38 po_pbr_join(&scan, &res);
910 }
911 64 ▶ else if (!po_pbr_skip(&scan, sw)) { po_pbr_join(r,&scan); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&scan, sw)) { po_pbr_join(r,&scan); return 0; } 0 64 64
912 }
913 66 ▶ if (scan.err) { po_pbr_join(r, &scan); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (scan.err) { po_pbr_join(r, &scan); return 0; } 0 66 66
914 }
915  
916 168 ▶ while (po_pbr_next(&rs, &rf, &rw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&rs, &rf, &rw)) { 102 66 168
917 166 ▶ if (rf == f_scope && rw == PO_PB_BYTES) { /* Scope* */
Branch: 2 branches
BranchTrueFalseTotal
if (rf == f_scope && rw == PO_PB_BYTES) { /* Scope* */ 64 38 102
if (rf == f_scope && rw == PO_PB_BYTES) { /* Scope* */ 64 0 64
918 po_pbr ss; uint32_t sf, sw;
919 64 ▶ if (!po_pbr_sub(&rs, &ss)) { po_pbr_join(r,&rs); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&rs, &ss)) { po_pbr_join(r,&rs); return 0; } 0 64 64
920 204 ▶ while (po_pbr_next(&ss, &sf, &sw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&ss, &sf, &sw)) { 140 64 204
921 216 ▶ if (sf == f_leaf && sw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (sf == f_leaf && sw == PO_PB_BYTES) { 76 64 140
if (sf == f_leaf && sw == PO_PB_BYTES) { 76 0 76
922 po_pbr leafr;
923 76 ▶ if (!po_pbr_sub(&ss, &leafr)) { po_pbr_join(r,&ss); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&ss, &leafr)) { po_pbr_join(r,&ss); return 0; } 0 76 76
924 76 ▶ if (!leaf(&leafr, b, &res_attrs)) { po_pbr_join(r,&ss); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!leaf(&leafr, b, &res_attrs)) { po_pbr_join(r,&ss); return 0; } 0 76 76
925 }
926 64 ▶ else if (!po_pbr_skip(&ss, sw)) { po_pbr_join(r,&ss); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&ss, sw)) { po_pbr_join(r,&ss); return 0; } 0 64 64
927 }
928 64 po_pbr_join(&rs, &ss);
929 64 ▶ if (rs.err) { po_pbr_join(r, &rs); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rs.err) { po_pbr_join(r, &rs); return 0; } 0 64 64
930 }
931 38 ▶ else if (!po_pbr_skip(&rs, rw)) { po_pbr_join(r,&rs); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&rs, rw)) { po_pbr_join(r,&rs); return 0; } 0 38 38
932 }
933 66 po_pbr_join(r, &rs);
934 66 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 66 66
935 }
936 }
937 69 return !r->err;
938 }
939  
940 34 static int po_otlp_traces(const void *buf, size_t len, po_batch *b) {
941 34 po_pbr r; po_pbr_init(&r, buf, len);
942 34 ▶ if (!po_walk(&r, b, po_span_read,
Branch: 1 branch
BranchTrueFalseTotal
if (!po_walk(&r, b, po_span_read, 2 32 34
943 2 PB_RESOURCESPANS_SCOPE_SPANS, PB_SCOPESPANS_SPANS)) return 0;
944 32 return 1;
945 }
946  
947 5 static int po_otlp_logs(const void *buf, size_t len, po_batch *b) {
948 5 po_pbr r; po_pbr_init(&r, buf, len);
949 5 ▶ if (!po_walk(&r, b, po_logrec_read,
Branch: 1 branch
BranchTrueFalseTotal
if (!po_walk(&r, b, po_logrec_read, 0 5 5
950 0 PB_RESOURCELOGS_SCOPE_LOGS, PB_SCOPELOGS_RECORDS)) return 0;
951 5 return 1;
952 }
953  
954 31 static int po_otlp_metrics(const void *buf, size_t len, po_batch *b) {
955 31 po_pbr r; po_pbr_init(&r, buf, len);
956 31 ▶ if (!po_walk(&r, b, po_metric_read,
Branch: 1 branch
BranchTrueFalseTotal
if (!po_walk(&r, b, po_metric_read, 0 31 31
957 0 PB_RESOURCEMETRICS_SCOPE_METRICS, PB_SCOPEMETRICS_METRICS)) return 0;
958 31 return 1;
959 }
960  
961 #endif /* PO_OTLP_IN_H */
« xs/view.xs ↑ Summary include/punk_observe/po_traceset.h »