include/otel_w3c.h

filter: Statementstmt 97.4% 77 / 79 Branchbran 64.3% 85 / 132 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.7% 162 / 211 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/resource.xs ↑ Summary xs/logger.xs »
1 /* otel_w3c.h - W3C Trace Context: traceparent and tracestate.
2 *
3 * traceparent is fixed-shape and 55 bytes for version 00:
4 *
5 * 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
6 * ^^ ^ ^ ^^
7 * version trace-id (32 hex) span-id (16 hex) flags
8 *
9 * VERSION TOLERANCE is the part worth reading twice. A version above 00 must
10 * be parsed LENIENTLY - take the first 55 bytes and ignore a trailing
11 * `-suffix` - rather than rejected. Rejecting an unknown version is how a
12 * service becomes the one that breaks every trace the day the ecosystem moves
13 * to version 01: it would be the only participant dropping context, and the
14 * traces would be broken in a way that points at everyone except the
15 * offender. Version ff is the one explicit invalid.
16 *
17 * tracestate is vendor state travelling beside it, and its mutation rule is
18 * the thing implementations get wrong: the member you changed moves to the
19 * FRONT, and everyone else keeps their relative order. That ordering is how a
20 * downstream vendor knows which system touched the trace most recently.
21 */
22  
23 #ifndef OTEL_W3C_H
24 #define OTEL_W3C_H
25  
26 #include "otel_ctx.h"
27  
28 #define OTEL_TRACESTATE_MAX_MEMBERS 32
29  
30 /* Parse a traceparent. Returns 1 and fills ctx, or 0 for "no context" -
31 * which is the answer for every malformed input, because a bad header is
32 * absent rather than fatal. */
33 21 static int otel_w3c_parse(const char *s, STRLEN len, otel_ctx *ctx) {
34 unsigned char ver[1], fl[1];
35 21 otel_ctx_clear(ctx);
36 21 ▶ if (!s || len < 55) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || len < 55) return 0; 21 0 21
if (!s || len < 55) return 0; 6 15 21
37 /* the separators sit at fixed offsets; anything else is not a
38 * traceparent, whatever version it claims */
39 15 ▶ if (s[2] != '-' || s[35] != '-' || s[52] != '-') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (s[2] != '-' || s[35] != '-' || s[52] != '-') return 0; 13 2 15
if (s[2] != '-' || s[35] != '-' || s[52] != '-') return 0; 13 0 13
if (s[2] != '-' || s[35] != '-' || s[52] != '-') return 0; 0 13 13
40 13 ▶ if (!otel_hexn(s, 2, 1, ver)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_hexn(s, 2, 1, ver)) return 0; 0 13 13
41 13 ▶ if (ver[0] == 0xff) return 0; /* the one invalid version */
Branch: 1 branch
BranchTrueFalseTotal
if (ver[0] == 0xff) return 0; /* the one invalid version */ 1 12 13
42  
43 12 ▶ if (ver[0] == 0x00) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver[0] == 0x00) { 10 2 12
44 10 ▶ if (len != 55) return 0; /* v00 is exactly 55 */
Branch: 1 branch
BranchTrueFalseTotal
if (len != 55) return 0; /* v00 is exactly 55 */ 0 10 10
45 }
46 else {
47 /* A FUTURE version may carry more fields. Take the part we
48 * understand and ignore the rest, rather than dropping the trace. */
49 2 ▶ if (len > 55 && s[55] != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (len > 55 && s[55] != '-') return 0; 2 0 2
if (len > 55 && s[55] != '-') return 0; 1 1 2
50 }
51  
52 11 ▶ if (!otel_hexn(s + 3, 32, 16, ctx->trace_id)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_hexn(s + 3, 32, 16, ctx->trace_id)) return 0; 1 10 11
53 10 ▶ if (!otel_hexn(s + 36, 16, 8, ctx->span_id)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_hexn(s + 36, 16, 8, ctx->span_id)) return 0; 0 10 10
54 10 ▶ if (otel_all_zero(ctx->trace_id, 16)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (otel_all_zero(ctx->trace_id, 16)) return 0; 1 9 10
55 9 ▶ if (otel_all_zero(ctx->span_id, 8)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (otel_all_zero(ctx->span_id, 8)) return 0; 1 8 9
56 8 ▶ if (!otel_hexn(s + 53, 2, 1, fl)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_hexn(s + 53, 2, 1, fl)) return 0; 0 8 8
57  
58 /* every flag bit is preserved, not just the one we understand: a bit we
59 * do not know the meaning of today is still somebody's information */
60 8 ctx->flags = fl[0];
61 8 ctx->valid = 1;
62 8 return 1;
63 }
64  
65 /* Render a traceparent into `out`, which must hold 56 bytes. */
66 3 static void otel_w3c_format(const otel_ctx *ctx, char *out) {
67 3 out[0] = '0'; out[1] = '0'; out[2] = '-';
68 3 otel_bytes_to_hex(ctx->trace_id, 16, out + 3);
69 3 out[35] = '-';
70 3 otel_bytes_to_hex(ctx->span_id, 8, out + 36);
71 3 out[52] = '-';
72 {
73 static const char H[] = "0123456789abcdef";
74 3 int f = ctx->flags & 0xff;
75 3 out[53] = H[(f >> 4) & 0xf];
76 3 out[54] = H[f & 0xf];
77 }
78 3 out[55] = '\0';
79 3 }
80  
81 /* ---- tracestate --------------------------------------------------------- *
82 * A comma-separated list of key=value members. The grammar is restrictive and
83 * a malformed member invalidates THAT MEMBER, not the whole header - dropping
84 * everyone else's state because one vendor emitted something odd is both
85 * rude and lossy. */
86  
87 45 static int otel_ts_key_ok(const char *k, STRLEN n) {
88 STRLEN i;
89 45 int at = 0;
90 45 ▶ if (n < 1 || n > 256) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 1 || n > 256) return 0; 45 0 45
if (n < 1 || n > 256) return 0; 0 45 45
91 /* lcalpha at the start, or a digit for the multi-tenant form */
92 45 ▶ if (!((k[0] >= 'a' && k[0] <= 'z') || (k[0] >= '0' && k[0] <= '9')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((k[0] >= 'a' && k[0] <= 'z') || (k[0] >= '0' && k[0] <= '9'))) 44 1 45
if (!((k[0] >= 'a' && k[0] <= 'z') || (k[0] >= '0' && k[0] <= '9'))) 0 44 44
if (!((k[0] >= 'a' && k[0] <= 'z') || (k[0] >= '0' && k[0] <= '9'))) 1 0 1
if (!((k[0] >= 'a' && k[0] <= 'z') || (k[0] >= '0' && k[0] <= '9'))) 1 0 1
93 1 return 0;
94 176 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 132 44 176
95 132 char c = k[i];
96 132 ▶ if (c == '@') { if (at++) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '@') { if (at++) return 0; continue; } 0 132 132
if (c == '@') { if (at++) return 0; continue; } 0 0 0
97 132 ▶ if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 79 53 132
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 79 79
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 53 0 53
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') 0 53 53
98 132 ▶ || c == '_' || c == '-' || c == '*' || c == '/') continue;
Branch: 4 branches
BranchTrueFalseTotal
|| c == '_' || c == '-' || c == '*' || c == '/') continue; 0 0 0
|| c == '_' || c == '-' || c == '*' || c == '/') continue; 0 0 0
|| c == '_' || c == '-' || c == '*' || c == '/') continue; 0 0 0
|| c == '_' || c == '-' || c == '*' || c == '/') continue; 0 0 0
99 0 return 0;
100 }
101 44 return 1;
102 }
103  
104 44 static int otel_ts_value_ok(const char *v, STRLEN n) {
105 STRLEN i;
106 44 ▶ if (n > 256) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 256) return 0; 0 44 44
107 149 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 105 44 149
108 105 unsigned char c = (unsigned char)v[i];
109 /* printable ASCII except comma and equals, which are the delimiters.
110 * This is also the check that stops a value reflected back out from
111 * carrying a byte that would forge a header. */
112 105 ▶ if (c < 0x20 || c > 0x7e || c == ',' || c == '=') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (c < 0x20 || c > 0x7e || c == ',' || c == '=') return 0; 105 0 105
if (c < 0x20 || c > 0x7e || c == ',' || c == '=') return 0; 105 0 105
if (c < 0x20 || c > 0x7e || c == ',' || c == '=') return 0; 105 0 105
if (c < 0x20 || c > 0x7e || c == ',' || c == '=') return 0; 0 105 105
113 }
114 44 return 1;
115 }
116  
117 /* Rebuild a tracestate with `key`=`val` moved to (or inserted at) the FRONT,
118 * every other valid member keeping its relative order, capped at 32 members.
119 * Returns a mortal SV. Members beyond the cap are dropped from the RIGHT,
120 * which is the oldest state - the same "keep what is most recent" reasoning
121 * the span queue uses. */
122 6 static SV *otel_ts_mutate(pTHX_ const char *in, STRLEN inlen,
123 const char *key, STRLEN klen,
124 const char *val, STRLEN vlen) {
125 6 SV *out = sv_2mortal(newSVpvs(""));
126 6 STRLEN i = 0;
127 6 int members = 0;
128  
129 6 ▶ if (klen && otel_ts_key_ok(key, klen) && otel_ts_value_ok(val, vlen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (klen && otel_ts_key_ok(key, klen) && otel_ts_value_ok(val, vlen)) { 6 0 6
if (klen && otel_ts_key_ok(key, klen) && otel_ts_value_ok(val, vlen)) { 6 0 6
if (klen && otel_ts_key_ok(key, klen) && otel_ts_value_ok(val, vlen)) { 6 0 6
130 6 sv_catpvn(out, key, klen);
131 6 sv_catpvs(out, "=");
132 6 sv_catpvn(out, val, vlen);
133 6 members = 1;
134 }
135 6 ▶ if (!in || !inlen) return out;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !inlen) return out; 6 0 6
if (!in || !inlen) return out; 1 5 6
136  
137 45 ▶ while (i < inlen && members < OTEL_TRACESTATE_MAX_MEMBERS) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < inlen && members < OTEL_TRACESTATE_MAX_MEMBERS) { 41 4 45
while (i < inlen && members < OTEL_TRACESTATE_MAX_MEMBERS) { 40 1 41
138 STRLEN start, end, eq;
139 const char *mk, *mv;
140 STRLEN mkl, mvl;
141 75 ▶ while (i < inlen && (in[i] == ' ' || in[i] == '\t' || in[i] == ','))
Branch: 4 branches
BranchTrueFalseTotal
while (i < inlen && (in[i] == ' ' || in[i] == '\t' || in[i] == ',')) 75 0 75
while (i < inlen && (in[i] == ' ' || in[i] == '\t' || in[i] == ',')) 0 75 75
while (i < inlen && (in[i] == ' ' || in[i] == '\t' || in[i] == ',')) 0 75 75
while (i < inlen && (in[i] == ' ' || in[i] == '\t' || in[i] == ',')) 35 40 75
142 35 i++;
143 40 ▶ if (i >= inlen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= inlen) break; 0 40 40
144 40 start = i;
145 297 ▶ while (i < inlen && in[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < inlen && in[i] != ',') i++; 293 4 297
while (i < inlen && in[i] != ',') i++; 257 36 293
146 40 end = i;
147 40 ▶ while (end > start && (in[end - 1] == ' ' || in[end - 1] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (in[end - 1] == ' ' || in[end - 1] == '\t')) 40 0 40
while (end > start && (in[end - 1] == ' ' || in[end - 1] == '\t')) 0 40 40
while (end > start && (in[end - 1] == ' ' || in[end - 1] == '\t')) 0 40 40
148 0 end--;
149 40 ▶ if (end <= start) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (end <= start) continue; 0 40 40
150  
151 162 ▶ for (eq = start; eq < end && in[eq] != '='; eq++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (eq = start; eq < end && in[eq] != '='; eq++) ; 161 1 162
for (eq = start; eq < end && in[eq] != '='; eq++) ; 122 39 161
152 40 ▶ if (eq >= end) continue; /* no '=': not a member */
Branch: 1 branch
BranchTrueFalseTotal
if (eq >= end) continue; /* no '=': not a member */ 1 39 40
153 39 mk = in + start; mkl = eq - start;
154 39 mv = in + eq + 1; mvl = end - eq - 1;
155 39 ▶ if (!otel_ts_key_ok(mk, mkl)) continue; /* drop THIS member only */
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_ts_key_ok(mk, mkl)) continue; /* drop THIS member only */ 1 38 39
156 38 ▶ if (!otel_ts_value_ok(mv, mvl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_ts_value_ok(mv, mvl)) continue; 0 38 38
157 /* the key we just promoted must not also appear later */
158 38 ▶ if (klen && mkl == klen && memEQ(mk, key, klen)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (klen && mkl == klen && memEQ(mk, key, klen)) continue; 38 0 38
if (klen && mkl == klen && memEQ(mk, key, klen)) continue; 4 34 38
if (klen && mkl == klen && memEQ(mk, key, klen)) continue; 1 3 4
159  
160 37 ▶ if (SvCUR(out)) sv_catpvs(out, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, ","); 37 0 37
161 37 sv_catpvn(out, mk, mkl);
162 37 sv_catpvs(out, "=");
163 37 sv_catpvn(out, mv, mvl);
164 37 members++;
165 }
166 5 return out;
167 }
168  
169 #endif /* OTEL_W3C_H */
« xs/resource.xs ↑ Summary xs/logger.xs »