xs/propagate.xs

filter: Statementstmt 99.0% 99 / 100 Branchbran 64.4% 89 / 138 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.9% 188 / 238 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_span.h ↑ Summary include/otel_schema.h »
1 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Propagate
2  
3 PROTOTYPES: DISABLE
4  
5 # Context propagation. Every parser here is handed CLIENT-CONTROLLED BYTES on
6 # the hot path of every request, so none of them allocates based on a length
7 # the client supplied, and none of them croaks: a malformed header yields no
8 # context rather than an error, because a 500 for a bad trace header would be
9 # a denial of service with extra steps.
10  
11 # extract(\%headers) -> { trace_id, span_id, sampled, flags, debug, format }
12 # or undef.
13 #
14 # %headers is the lowercased header name => value map. The propagators are
15 # tried in the order named by $order (comma-separated, default
16 # "tracecontext,baggage"), and a LATER one that finds a valid context
17 # overrides an earlier one - so "b3,tracecontext" and "tracecontext,b3"
18 # behave differently and both are reasonable things to configure.
19 SV *
20 extract(headers, order = &PL_sv_undef)
21 SV *headers
22 SV *order
23 CODE:
24 {
25 HV *h;
26 otel_ctx ctx, found;
27 const char *ord;
28 STRLEN ordlen, i;
29 37 const char *fmt = NULL;
30 37 int have = 0;
31  
32 37 ▶ if (!(SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVHV)) 37 0 37
if (!(SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVHV)) 0 37 37
33 0 croak("Punk::OpenTelemetry::Propagate::extract: expected a hashref");
34 37 h = (HV *)SvRV(headers);
35 37 otel_ctx_clear(&found);
36  
37 18 ord = SvOK(order) ? SvPV_const(order, ordlen)
38 37 ▶ : (ordlen = 22, "tracecontext,baggage");
Branch: 1 branch
BranchTrueFalseTotal
: (ordlen = 22, "tracecontext,baggage"); 18 19 37
39  
40 37 i = 0;
41 58 ▶ while (i <= ordlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i <= ordlen) { 58 0 58
42 58 STRLEN start = i, n;
43 541 ▶ while (i < ordlen && ord[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < ordlen && ord[i] != ',') i++; 504 37 541
while (i < ordlen && ord[i] != ',') i++; 483 21 504
44 58 n = i - start;
45 58 ▶ while (n && (ord[start] == ' ')) { start++; n--; }
Branch: 2 branches
BranchTrueFalseTotal
while (n && (ord[start] == ' ')) { start++; n--; } 58 0 58
while (n && (ord[start] == ' ')) { start++; n--; } 0 58 58
46 58 ▶ while (n && (ord[start + n - 1] == ' ')) n--;
Branch: 2 branches
BranchTrueFalseTotal
while (n && (ord[start + n - 1] == ' ')) n--; 58 0 58
while (n && (ord[start + n - 1] == ' ')) n--; 0 58 58
47  
48 79 ▶ if (n == 12 && memEQ(ord + start, "tracecontext", 12)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 12 && memEQ(ord + start, "tracecontext", 12)) { 21 37 58
if (n == 12 && memEQ(ord + start, "tracecontext", 12)) { 21 0 21
49 21 SV **v = hv_fetchs(h, "traceparent", 0);
50 21 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 20 1 21
if (v && *v && SvOK(*v)) { 20 0 20
if (v && *v && SvOK(*v)) { 20 0 20
51 20 STRLEN l; const char *s = SvPV_const(*v, l);
52 20 ▶ if (otel_w3c_parse(s, l, &ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (otel_w3c_parse(s, l, &ctx)) { 7 13 20
53 7 found = ctx; have = 1; fmt = "tracecontext";
54 }
55 }
56 }
57 49 ▶ else if (n == 2 && memEQ(ord + start, "b3", 2)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (n == 2 && memEQ(ord + start, "b3", 2)) { 12 25 37
else if (n == 2 && memEQ(ord + start, "b3", 2)) { 12 0 12
58 /* the SINGLE header wins when both are present, which is what
59 * the B3 spec says and what Zipkin itself does */
60 12 SV **v = hv_fetchs(h, "b3", 0);
61 12 int got = 0;
62 12 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 10 2 12
if (v && *v && SvOK(*v)) { 10 0 10
if (v && *v && SvOK(*v)) { 10 0 10
63 10 STRLEN l; const char *s = SvPV_const(*v, l);
64 10 got = otel_b3_parse_single(s, l, &ctx);
65 }
66 12 ▶ if (!got) {
Branch: 1 branch
BranchTrueFalseTotal
if (!got) { 2 10 12
67 2 SV **t = hv_fetchs(h, "x-b3-traceid", 0);
68 2 SV **sp = hv_fetchs(h, "x-b3-spanid", 0);
69 2 SV **sm = hv_fetchs(h, "x-b3-sampled", 0);
70 2 SV **fl = hv_fetchs(h, "x-b3-flags", 0);
71 2 STRLEN tl = 0, sl = 0, ml = 0, fll = 0;
72 4 ▶ const char *ts = (t && *t && SvOK(*t)) ? SvPV_const(*t, tl) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *ts = (t && *t && SvOK(*t)) ? SvPV_const(*t, tl) : NULL; 2 0 2
const char *ts = (t && *t && SvOK(*t)) ? SvPV_const(*t, tl) : NULL; 2 0 2
const char *ts = (t && *t && SvOK(*t)) ? SvPV_const(*t, tl) : NULL; 2 0 2
73 4 ▶ const char *ss = (sp && *sp && SvOK(*sp)) ? SvPV_const(*sp, sl) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *ss = (sp && *sp && SvOK(*sp)) ? SvPV_const(*sp, sl) : NULL; 2 0 2
const char *ss = (sp && *sp && SvOK(*sp)) ? SvPV_const(*sp, sl) : NULL; 2 0 2
const char *ss = (sp && *sp && SvOK(*sp)) ? SvPV_const(*sp, sl) : NULL; 2 0 2
74 4 ▶ const char *ms = (sm && *sm && SvOK(*sm)) ? SvPV_const(*sm, ml) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *ms = (sm && *sm && SvOK(*sm)) ? SvPV_const(*sm, ml) : NULL; 2 0 2
const char *ms = (sm && *sm && SvOK(*sm)) ? SvPV_const(*sm, ml) : NULL; 2 0 2
const char *ms = (sm && *sm && SvOK(*sm)) ? SvPV_const(*sm, ml) : NULL; 2 0 2
75 3 ▶ const char *fs = (fl && *fl && SvOK(*fl)) ? SvPV_const(*fl, fll) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *fs = (fl && *fl && SvOK(*fl)) ? SvPV_const(*fl, fll) : NULL; 1 1 2
const char *fs = (fl && *fl && SvOK(*fl)) ? SvPV_const(*fl, fll) : NULL; 1 0 1
const char *fs = (fl && *fl && SvOK(*fl)) ? SvPV_const(*fl, fll) : NULL; 1 0 1
76 2 got = otel_b3_parse_multi(ts, tl, ss, sl, ms, ml, fs, fll, &ctx);
77 }
78 12 ▶ if (got) { found = ctx; have = 1; fmt = "b3"; }
Branch: 1 branch
BranchTrueFalseTotal
if (got) { found = ctx; have = 1; fmt = "b3"; } 12 0 12
79 }
80 25 ▶ else if (n == 6 && memEQ(ord + start, "jaeger", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (n == 6 && memEQ(ord + start, "jaeger", 6)) { 6 19 25
else if (n == 6 && memEQ(ord + start, "jaeger", 6)) { 6 0 6
81 6 SV **v = hv_fetchs(h, "uber-trace-id", 0);
82 6 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 6 0 6
if (v && *v && SvOK(*v)) { 6 0 6
if (v && *v && SvOK(*v)) { 6 0 6
83 6 STRLEN l; const char *s = SvPV_const(*v, l);
84 6 ▶ if (otel_jaeger_parse(s, l, &ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (otel_jaeger_parse(s, l, &ctx)) { 4 2 6
85 4 found = ctx; have = 1; fmt = "jaeger";
86 }
87 }
88 }
89 /* "baggage" is a propagator in the list but carries no trace
90 * context; it is extracted separately, by baggage_extract */
91 58 ▶ if (i >= ordlen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= ordlen) break; 37 21 58
92 21 i++;
93 }
94  
95 37 ▶ if (!have) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!have) XSRETURN_UNDEF; 16 21 37
96 {
97 21 HV *o = newHV();
98 char hex[32];
99 21 otel_bytes_to_hex(found.trace_id, 16, hex);
100 21 (void)hv_stores(o, "trace_id", newSVpvn(hex, 32));
101 21 otel_bytes_to_hex(found.span_id, 8, hex);
102 21 (void)hv_stores(o, "span_id", newSVpvn(hex, 16));
103 21 (void)hv_stores(o, "sampled",
104 newSViv((found.flags & OTEL_FLAG_SAMPLED) ? 1 : 0));
105 21 (void)hv_stores(o, "flags", newSViv(found.flags));
106 21 (void)hv_stores(o, "debug", newSViv(found.debug));
107 21 ▶ (void)hv_stores(o, "format", newSVpv(fmt ? fmt : "", 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(o, "format", newSVpv(fmt ? fmt : "", 0)); 21 0 21
108 21 RETVAL = newRV_noinc((SV *)o);
109 }
110 }
111 OUTPUT:
112 RETVAL
113  
114 # inject($trace_id, $span_id, $sampled, $order) -> \%headers
115 #
116 # Injects EVERY configured format, so one request can carry traceparent and b3
117 # together for a mixed fleet - which is what makes a migration possible
118 # without a flag day.
119 SV *
120 inject(trace_id, span_id, sampled, order = &PL_sv_undef)
121 SV *trace_id
122 SV *span_id
123 IV sampled
124 SV *order
125 CODE:
126 {
127 otel_ctx ctx;
128 3 HV *out = newHV();
129 const char *ord;
130 STRLEN ordlen, tl, sl, i;
131 3 ▶ const char *ts = SvPV_const(trace_id, tl);
Branch: 1 branch
BranchTrueFalseTotal
const char *ts = SvPV_const(trace_id, tl); 3 0 3
132 3 const char *ss = SvPV_const(span_id, sl);
133 char buf[64];
134  
135 3 otel_ctx_clear(&ctx);
136 3 ▶ if (!otel_hexn(ts, tl, 16, ctx.trace_id)
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_hexn(ts, tl, 16, ctx.trace_id) 3 0 3
137 3 ▶ || !otel_hexn(ss, sl, 8, ctx.span_id)
Branch: 1 branch
BranchTrueFalseTotal
|| !otel_hexn(ss, sl, 8, ctx.span_id) 3 0 3
138 3 ▶ || otel_all_zero(ctx.trace_id, 16)
Branch: 1 branch
BranchTrueFalseTotal
|| otel_all_zero(ctx.trace_id, 16) 2 1 3
139 2 ▶ || otel_all_zero(ctx.span_id, 8)) {
Branch: 1 branch
BranchTrueFalseTotal
|| otel_all_zero(ctx.span_id, 8)) { 0 2 2
140 /* nothing valid to propagate is not an error - it is a root span
141 * that has not started, or a caller passing through */
142 1 RETVAL = newRV_noinc((SV *)out);
143 }
144 else {
145 2 ctx.flags = sampled ? OTEL_FLAG_SAMPLED : 0;
146 2 ord = SvOK(order) ? SvPV_const(order, ordlen)
147 2 ▶ : (ordlen = 22, "tracecontext,baggage");
Branch: 1 branch
BranchTrueFalseTotal
: (ordlen = 22, "tracecontext,baggage"); 2 0 2
148 2 i = 0;
149 4 ▶ while (i <= ordlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i <= ordlen) { 4 0 4
150 4 STRLEN start = i, n;
151 36 ▶ while (i < ordlen && ord[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < ordlen && ord[i] != ',') i++; 34 2 36
while (i < ordlen && ord[i] != ',') i++; 32 2 34
152 4 n = i - start;
153 4 ▶ while (n && ord[start] == ' ') { start++; n--; }
Branch: 2 branches
BranchTrueFalseTotal
while (n && ord[start] == ' ') { start++; n--; } 4 0 4
while (n && ord[start] == ' ') { start++; n--; } 0 4 4
154 4 ▶ while (n && ord[start + n - 1] == ' ') n--;
Branch: 2 branches
BranchTrueFalseTotal
while (n && ord[start + n - 1] == ' ') n--; 4 0 4
while (n && ord[start + n - 1] == ' ') n--; 0 4 4
155  
156 4 ▶ if (n == 12 && memEQ(ord + start, "tracecontext", 12)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 12 && memEQ(ord + start, "tracecontext", 12)) { 2 2 4
if (n == 12 && memEQ(ord + start, "tracecontext", 12)) { 2 0 2
157 2 otel_w3c_format(&ctx, buf);
158 2 (void)hv_stores(out, "traceparent", newSVpv(buf, 0));
159 }
160 2 ▶ else if (n == 2 && memEQ(ord + start, "b3", 2)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (n == 2 && memEQ(ord + start, "b3", 2)) { 1 1 2
else if (n == 2 && memEQ(ord + start, "b3", 2)) { 1 0 1
161 1 otel_b3_format_single(&ctx, buf);
162 1 (void)hv_stores(out, "b3", newSVpv(buf, 0));
163 }
164 1 ▶ else if (n == 6 && memEQ(ord + start, "jaeger", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (n == 6 && memEQ(ord + start, "jaeger", 6)) { 1 0 1
else if (n == 6 && memEQ(ord + start, "jaeger", 6)) { 1 0 1
165 1 otel_jaeger_format(&ctx, buf);
166 1 (void)hv_stores(out, "uber-trace-id", newSVpv(buf, 0));
167 }
168 4 ▶ if (i >= ordlen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= ordlen) break; 2 2 4
169 2 i++;
170 }
171 2 RETVAL = newRV_noinc((SV *)out);
172 }
173 }
174 OUTPUT:
175 RETVAL
176  
177 # tracestate($existing, $key, $value) -> the mutated header.
178 #
179 # The changed member moves to the FRONT and everyone else keeps their relative
180 # order; that ordering is how a downstream vendor knows which system touched
181 # the trace most recently, and it is the rule implementations get wrong.
182 SV *
183 tracestate(existing, key, value)
184 SV *existing
185 SV *key
186 SV *value
187 CODE:
188 {
189 6 STRLEN el = 0, kl, vl;
190 12 ▶ const char *e = SvOK(existing) ? SvPV_const(existing, el) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *e = SvOK(existing) ? SvPV_const(existing, el) : NULL; 6 0 6
191 6 ▶ const char *k = SvPV_const(key, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_const(key, kl); 6 0 6
192 6 const char *v = SvPV_const(value, vl);
193 6 RETVAL = newSVsv(otel_ts_mutate(aTHX_ e, el, k, kl, v, vl));
194 }
195 OUTPUT:
196 RETVAL
197  
198 # baggage_extract($header) -> \%baggage
199 SV *
200 baggage_extract(header)
201 SV *header
202 CODE:
203 {
204 6 STRLEN l = 0;
205 12 ▶ const char *s = SvOK(header) ? SvPV_const(header, l) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvOK(header) ? SvPV_const(header, l) : NULL; 6 0 6
206 6 RETVAL = newRV_noinc((SV *)otel_baggage_parse(aTHX_ s, l));
207 }
208 OUTPUT:
209 RETVAL
210  
211 # baggage_inject(\%baggage) -> the header value
212 SV *
213 baggage_inject(baggage)
214 SV *baggage
215 CODE:
216 {
217 5 ▶ HV *h = (SvROK(baggage) && SvTYPE(SvRV(baggage)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *h = (SvROK(baggage) && SvTYPE(SvRV(baggage)) == SVt_PVHV) 5 0 5
218 10 ▶ ? (HV *)SvRV(baggage) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(baggage) : NULL; 5 0 5
219 5 RETVAL = newSVsv(otel_baggage_format(aTHX_ h));
220 }
221 OUTPUT:
222 RETVAL
« include/otel_span.h ↑ Summary include/otel_schema.h »