include/otel_pb.h

filter: Statementstmt 100% 102 / 102 Branchbran 91.6% 22 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.4% 124 / 126 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_sample.h ↑ Summary blib/lib/Punk/OpenTelemetry.pm »
1 /* otel_pb.h - a protobuf writer for one fixed schema.
2 *
3 * OTLP's schema is small, versioned slowly, and entirely known at compile
4 * time, so encoding it needs no descriptors, no reflection and no dependency:
5 * a varint writer, a length-delimited writer, and the field numbers. That is
6 * the whole of protobuf that OTLP uses.
7 *
8 * THE NESTING PROBLEM. Every embedded message is length-delimited, so a
9 * parent cannot be written until its children's byte count is known. There
10 * are two ways out: build backwards into a buffer, or walk twice - once to
11 * measure, once to write. This takes the second. It costs a second pass over
12 * data already in cache, and in exchange every size_* function sits beside
13 * the write_* function it must agree with, where a reader can check them
14 * against each other. Pointer arithmetic into a buffer being filled from the
15 * end is faster to run and much harder to prove, and a wrong length here
16 * produces bytes a collector rejects with no useful diagnostic.
17 *
18 * The two halves are kept honest by otel_pb_check (a debug build asserts that
19 * what was written matches what was measured) and by the golden vectors in
20 * t/01-protobuf.t.
21 *
22 * Needs Perl headers for SV/STRLEN/UV.
23 */
24  
25 #ifndef OTEL_PB_H
26 #define OTEL_PB_H
27  
28 /* wire types */
29 #define PB_VARINT 0
30 #define PB_FIXED64 1
31 #define PB_BYTES 2
32 #define PB_FIXED32 5
33  
34 /* A growable output buffer. An SV would do, but this is appended to once per
35 * field on the hot path and SvGROW/SvCUR bookkeeping per call is exactly the
36 * overhead worth not paying; the bytes become an SV once, at the end. */
37 typedef struct {
38 char *buf;
39 size_t len;
40 size_t cap;
41 } otel_buf;
42  
43 78 static void otel_buf_init(otel_buf *b, size_t hint) {
44 78 b->cap = hint > 64 ? hint : 64;
45 78 Newx(b->buf, b->cap, char);
46 78 b->len = 0;
47 78 }
48  
49 78 static void otel_buf_free(otel_buf *b) {
50 78 ▶ if (b->buf) Safefree(b->buf);
Branch: 1 branch
BranchTrueFalseTotal
if (b->buf) Safefree(b->buf); 78 0 78
51 78 b->buf = NULL; b->len = b->cap = 0;
52 78 }
53  
54 2659 static void otel_buf_need(otel_buf *b, size_t n) {
55 2659 ▶ if (b->len + n <= b->cap) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->len + n <= b->cap) return; 2642 17 2659
56 34 ▶ while (b->cap < b->len + n) b->cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (b->cap < b->len + n) b->cap *= 2; 17 17 34
57 17 Renew(b->buf, b->cap, char);
58 }
59  
60 345 static void otel_buf_raw(otel_buf *b, const char *p, size_t n) {
61 345 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 345 345
62 345 otel_buf_need(b, n);
63 345 memcpy(b->buf + b->len, p, n);
64 345 b->len += n;
65 }
66  
67 /* ---- varints ------------------------------------------------------------ */
68  
69 /* How many bytes a varint of v takes. Base 128, seven bits a byte. */
70 9825 static size_t otel_pb_varint_size(UV v) {
71 9825 size_t n = 1;
72 10267 ▶ while (v >= 0x80) { v >>= 7; n++; }
Branch: 1 branch
BranchTrueFalseTotal
while (v >= 0x80) { v >>= 7; n++; } 442 9825 10267
73 9825 return n;
74 }
75  
76 2193 static void otel_pb_varint(otel_buf *b, UV v) {
77 2193 otel_buf_need(b, 10); /* a 64-bit varint is at most 10 */
78 2319 ▶ while (v >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (v >= 0x80) { 126 2193 2319
79 126 b->buf[b->len++] = (char)((v & 0x7f) | 0x80);
80 126 v >>= 7;
81 }
82 2193 b->buf[b->len++] = (char)v;
83 2193 }
84  
85 /* A tag is the field number and wire type in one varint. */
86 #define OTEL_PB_TAG(field, wire) ((UV)(((UV)(field) << 3) | (UV)(wire)))
87  
88 4145 static size_t otel_pb_tag_size(int field, int wire) {
89 4145 return otel_pb_varint_size(OTEL_PB_TAG(field, wire));
90 }
91  
92 1006 static void otel_pb_tag(otel_buf *b, int field, int wire) {
93 1006 otel_pb_varint(b, OTEL_PB_TAG(field, wire));
94 1006 }
95  
96 /* ---- scalar fields ------------------------------------------------------ *
97 * Proto3 omits a field at its default value, and OTLP relies on that: a span
98 * with no parent has no parent_span_id field at all rather than eight zero
99 * bytes. Each writer below therefore takes the decision to emit at its call
100 * site, not inside itself, because "zero" is a legitimate value for some of
101 * these (a status code of 0 is UNSET and is genuinely omitted; a bucket count
102 * of 0 inside a packed array is not). */
103  
104 142 static size_t otel_pb_uint64_size(int field, UV v) {
105 142 return otel_pb_tag_size(field, PB_VARINT) + otel_pb_varint_size(v);
106 }
107 31 static void otel_pb_uint64(otel_buf *b, int field, UV v) {
108 31 otel_pb_tag(b, field, PB_VARINT);
109 31 otel_pb_varint(b, v);
110 31 }
111  
112 201 static size_t otel_pb_int32_size(int field, int v) {
113 201 return otel_pb_tag_size(field, PB_VARINT) + otel_pb_varint_size((UV)v);
114 }
115 41 static void otel_pb_int32(otel_buf *b, int field, int v) {
116 41 otel_pb_tag(b, field, PB_VARINT);
117 41 otel_pb_varint(b, (UV)v);
118 41 }
119  
120 35 static size_t otel_pb_bool_size(int field) {
121 35 return otel_pb_tag_size(field, PB_VARINT) + 1;
122 }
123 11 static void otel_pb_bool(otel_buf *b, int field, int v) {
124 11 otel_pb_tag(b, field, PB_VARINT);
125 11 otel_pb_varint(b, v ? 1 : 0);
126 11 }
127  
128 /* fixed64, little endian. OTLP uses it for the nanosecond timestamps, which
129 * are always large, so a varint would cost more than the eight bytes. */
130 428 static size_t otel_pb_fixed64_size(int field) {
131 428 return otel_pb_tag_size(field, PB_FIXED64) + 8;
132 }
133 114 static void otel_pb_fixed64(otel_buf *b, int field, U64TYPE v) {
134 int i;
135 114 otel_pb_tag(b, field, PB_FIXED64);
136 114 otel_buf_need(b, 8);
137 1026 ▶ for (i = 0; i < 8; i++) b->buf[b->len++] = (char)((v >> (i * 8)) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) b->buf[b->len++] = (char)((v >> (i * 8)) & 0xff); 912 114 1026
138 114 }
139  
140 /* a double, as its IEEE-754 bits in fixed64 */
141 198 static size_t otel_pb_double_size(int field) {
142 198 return otel_pb_tag_size(field, PB_FIXED64) + 8;
143 }
144 34 static void otel_pb_double(otel_buf *b, int field, double d) {
145 U64TYPE bits;
146 34 Copy(&d, &bits, 1, U64TYPE);
147 34 otel_pb_fixed64(b, field, bits);
148 34 }
149  
150 /* ---- length-delimited --------------------------------------------------- */
151  
152 1550 static size_t otel_pb_bytes_size(int field, size_t n) {
153 1550 return otel_pb_tag_size(field, PB_BYTES) + otel_pb_varint_size((UV)n) + n;
154 }
155 345 static void otel_pb_bytes(otel_buf *b, int field, const char *p, size_t n) {
156 345 otel_pb_tag(b, field, PB_BYTES);
157 345 otel_pb_varint(b, (UV)n);
158 345 otel_buf_raw(b, p, n);
159 345 }
160  
161 /* An embedded message: the same shape, with the child's measured size as the
162 * length. The caller writes the child straight after. */
163 1493 static size_t otel_pb_msg_size(int field, size_t inner) {
164 1493 return otel_pb_tag_size(field, PB_BYTES) + otel_pb_varint_size((UV)inner)
165 1493 + inner;
166 }
167 448 static void otel_pb_msg_head(otel_buf *b, int field, size_t inner) {
168 448 otel_pb_tag(b, field, PB_BYTES);
169 448 otel_pb_varint(b, (UV)inner);
170 448 }
171  
172 /* ---- packed repeated fields --------------------------------------------- *
173 * A repeated numeric field is encoded as ONE length-delimited field holding
174 * the values back to back, not as a tag per value. Emitting the unpacked form
175 * is legal proto2 and is accepted by most parsers, but the OTLP schema
176 * declares these packed and some consumers read them strictly - so a
177 * histogram's buckets would arrive empty from something that only accepts
178 * what the schema promised. */
179  
180 /* packed varints, for repeated uint64 (the exponential histogram's buckets) */
181 56 static size_t otel_pb_packed_varint_size(int field, const IV *v, int n) {
182 56 size_t inner = 0;
183 int i;
184 1958 ▶ for (i = 0; i < n; i++) inner += otel_pb_varint_size((UV)v[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) inner += otel_pb_varint_size((UV)v[i]); 1902 56 1958
185 56 return otel_pb_tag_size(field, PB_BYTES)
186 56 + otel_pb_varint_size((UV)inner) + inner;
187 }
188 9 static void otel_pb_packed_varint(otel_buf *b, int field, const IV *v, int n) {
189 9 size_t inner = 0;
190 int i;
191 294 ▶ for (i = 0; i < n; i++) inner += otel_pb_varint_size((UV)v[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) inner += otel_pb_varint_size((UV)v[i]); 285 9 294
192 9 otel_pb_tag(b, field, PB_BYTES);
193 9 otel_pb_varint(b, (UV)inner);
194 294 ▶ for (i = 0; i < n; i++) otel_pb_varint(b, (UV)v[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) otel_pb_varint(b, (UV)v[i]); 285 9 294
195 9 }
196  
197 42 static size_t otel_pb_packed_double_size(int field, int n) {
198 42 return otel_pb_tag_size(field, PB_BYTES)
199 42 + otel_pb_varint_size((UV)(n * 8)) + (size_t)n * 8;
200 }
201 7 static void otel_pb_packed_double(otel_buf *b, int field, const double *v,
202 int n) {
203 int i, j;
204 7 otel_pb_tag(b, field, PB_BYTES);
205 7 otel_pb_varint(b, (UV)(n * 8));
206 7 otel_buf_need(b, (size_t)n * 8);
207 93 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 86 7 93
208 U64TYPE bits;
209 86 Copy(&v[i], &bits, 1, U64TYPE);
210 774 ▶ for (j = 0; j < 8; j++) b->buf[b->len++] = (char)((bits >> (j*8)) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 8; j++) b->buf[b->len++] = (char)((bits >> (j*8)) & 0xff); 688 86 774
211 }
212 7 }
213  
214 /* The assertion that keeps the two passes honest: after writing a child, the
215 * bytes actually appended must equal what size_* promised. A mismatch means a
216 * size function and its writer have drifted, which produces a length prefix
217 * that lies - and a collector rejecting the payload with nothing useful to
218 * say about why. Compiled out of a normal build. */
219 #ifdef OTEL_PB_ASSERT
220 # define OTEL_PB_CHECK(b, mark, expect, what) \
221 do { size_t _got = (b)->len - (mark); \
222 if (_got != (size_t)(expect)) \
223 croak("Punk::OpenTelemetry: %s wrote %lu bytes, measured " \
224 "%lu - a size_ function and its writer disagree", \
225 (what), (unsigned long)_got, \
226 (unsigned long)(expect)); } while (0)
227 #else
228 # define OTEL_PB_CHECK(b, mark, expect, what) ((void)0)
229 #endif
230  
231 #endif /* OTEL_PB_H */
« include/otel_sample.h ↑ Summary blib/lib/Punk/OpenTelemetry.pm »