include/otel_consume.h

filter: Statementstmt 85.7% 102 / 119 Branchbran 49.2% 64 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.6% 166 / 249 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_ctx.h ↑ Summary xs/encode.xs »
1 /* otel_consume.h - resolving the ABIs this dist instruments through, and the
2 * Fetch client observer that needs them.
3 *
4 * Punk's pk_abi and Fetch's fetch_abi are both resolved LAZILY and both are
5 * OPTIONAL. A process that has loaded neither still gets a working tracer -
6 * manual spans, encoders, exporter - and simply has nothing automatic to
7 * observe. That is the difference between a telemetry layer and a
8 * dependency: this one attaches to what is there.
9 *
10 * Included after otel_instr.h, which defines the callbacks, and before
11 * xs/instrument.xs, which registers them.
12 */
13  
14 #ifndef OTEL_CONSUME_H
15 #define OTEL_CONSUME_H
16  
17 #include "fetch_abi.h" /* pk_abi.h comes in with otel_instr.h */
18 #include "dbil_abi.h" /* DBIx::Loop's statement observer */
19  
20 /* A header name compared without regard to case, because HTTP field names
21 * are case-insensitive and a caller writing `TraceParent` has set the header
22 * whatever this file thinks of the spelling. Written out rather than reached
23 * for through strncasecmp, which is locale-dependent: in a Turkish locale
24 * tolower('I') is not 'i'. */
25 2 static int otel_ieq(const char *a, const char *b, STRLEN n) {
26 STRLEN i;
27 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22 2 24
28 22 char x = a[i], y = b[i];
29 22 ▶ if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 22 0 22
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 0 22 22
30 22 ▶ if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 22 0 22
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 0 22 22
31 22 ▶ if (x != y) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x != y) return 0; 0 22 22
32 }
33 2 return 1;
34 }
35  
36 static const pk_abi *OTEL_PK = NULL;
37 static const fetch_abi *OTEL_FT = NULL;
38 static const dbil_abi *OTEL_DL = NULL;
39 static int OTEL_PK_TRIED = 0, OTEL_FT_TRIED = 0, OTEL_DL_TRIED = 0;
40 static int OTEL_PK_INSTALLED = 0, OTEL_FT_INSTALLED = 0, OTEL_DL_INSTALLED = 0;
41 /* What the one-and-only registration actually managed, kept so that a LATER
42 * install() reports the same answer. Registration is process-global and
43 * happens once; a second application asking what is installed was being told
44 * only about the server hook, so a test - or an operator - reading the
45 * report from any app but the first saw the logs signal as absent when it was
46 * running. */
47 static int OTEL_PK_DB = 0, OTEL_PK_LOGS = 0, OTEL_DL_DB = 0;
48  
49 /* The shared shape of an optional ABI lookup: require the module, call its
50 * _abi_ptr, check the version. A miss is NOT an error - it means that dist is
51 * not in this process, and there is simply nothing of its kind to instrument. */
52 12 static UV otel_abi_ptr(pTHX_ const char *module, const char *fn) {
53 12 dSP;
54 int count;
55 12 UV p = 0;
56 12 SV *req = sv_2mortal(newSVpvf("require %s;", module));
57 12 eval_pv(SvPV_nolen(req), FALSE);
58 12 SPAGAIN; /* the require ran Perl; the stack may move */
59 12 ▶ if (SvTRUE(ERRSV)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) return 0; 12 0 12
if (SvTRUE(ERRSV)) return 0; 0 12 12
60 12 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 12 12
61 12 count = call_pv(fn, G_SCALAR | G_EVAL);
62 12 SPAGAIN;
63 12 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPu;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPu; 12 0 12
if (!SvTRUE(ERRSV) && count > 0) p = POPu; 12 0 12
if (!SvTRUE(ERRSV) && count > 0) p = POPu; 12 0 12
64 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
65 12 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 12 0 12
66 12 return p;
67 }
68  
69 23 static const pk_abi *otel_pk(pTHX) {
70 23 ▶ if (!OTEL_PK && !OTEL_PK_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_PK && !OTEL_PK_TRIED) { 4 19 23
if (!OTEL_PK && !OTEL_PK_TRIED) { 4 0 4
71 UV p;
72 4 OTEL_PK_TRIED = 1;
73 4 p = otel_abi_ptr(aTHX_ "Punk", "Punk::_abi_ptr");
74 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
75 4 const pk_abi *a = INT2PTR(const pk_abi *, p);
76 /* >=, not ==: the table only ever grows, so a NEWER Punk is a
77 * superset we use a prefix of. Requiring equality would make
78 * every Punk release break this dist for no reason. */
79 4 ▶ if (a && a->abi_version >= 1) OTEL_PK = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= 1) OTEL_PK = a; 4 0 4
if (a && a->abi_version >= 1) OTEL_PK = a; 4 0 4
80 }
81 }
82 23 return OTEL_PK;
83 }
84  
85 23 static const fetch_abi *otel_ft(pTHX) {
86 23 ▶ if (!OTEL_FT && !OTEL_FT_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_FT && !OTEL_FT_TRIED) { 4 19 23
if (!OTEL_FT && !OTEL_FT_TRIED) { 4 0 4
87 UV p;
88 4 OTEL_FT_TRIED = 1;
89 4 p = otel_abi_ptr(aTHX_ "Fetch", "Fetch::_abi_ptr");
90 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
91 4 const fetch_abi *a = INT2PTR(const fetch_abi *, p);
92 /* the outbound observer is v2; an older Fetch has no hook to
93 * attach to, so there is nothing to do rather than something to
94 * complain about */
95 4 ▶ if (a && a->abi_version >= 2) OTEL_FT = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= 2) OTEL_FT = a; 4 0 4
if (a && a->abi_version >= 2) OTEL_FT = a; 4 0 4
96 }
97 }
98 23 return OTEL_FT;
99 }
100  
101 23 static const dbil_abi *otel_dl(pTHX) {
102 23 ▶ if (!OTEL_DL && !OTEL_DL_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_DL && !OTEL_DL_TRIED) { 4 19 23
if (!OTEL_DL && !OTEL_DL_TRIED) { 4 0 4
103 UV p;
104 4 OTEL_DL_TRIED = 1;
105 4 p = otel_abi_ptr(aTHX_ "DBIx::Loop", "DBIx::Loop::_abi_ptr");
106 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
107 4 const dbil_abi *a = INT2PTR(const dbil_abi *, p);
108 /* the statement observer is v2 */
109 4 ▶ if (a && a->abi_version >= 2) OTEL_DL = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= 2) OTEL_DL = a; 4 0 4
if (a && a->abi_version >= 2) OTEL_DL = a; 4 0 4
110 }
111 }
112 23 return OTEL_DL;
113 }
114  
115 /* ---- DBIx::Loop's statements -------------------------------------------- *
116 * The OTHER database path. An application on the async backend generates no
117 * pk_abi query traffic at all, so registering only there left a whole class of
118 * application with no database spans and nothing to say why - which three
119 * comments in this distribution claimed was already handled, and was not.
120 *
121 * dbil_exec is the one place all three of its backends have in common, and it
122 * fires this BEFORE the statement runs, synchronously with the caller. So the
123 * dispatch frame is still on the stack and current_of is the request that
124 * issued it - an async backend is no obstacle to parenting, because a span's
125 * parent is fixed at start and start is never the deferred half. */
126 0 static void *otel_dl_start(pTHX_ int is_query, const char *sql, STRLEN len,
127 int nbind, void *ud) {
128 PERL_UNUSED_ARG(is_query);
129 0 return (void *)otel_db_start(aTHX_ otel_pk(aTHX), sql, len, nbind,
130 (const char *)ud);
131 }
132  
133 /* Exactly one of res and err is non-NULL; both borrowed. */
134 0 static void otel_dl_done(pTHX_ void *token, SV *res, SV *err, void *ud) {
135 PERL_UNUSED_ARG(res);
136 PERL_UNUSED_ARG(ud);
137 0 otel_db_end(aTHX_ (otel_span *)token, err ? 0 : 1);
138 0 }
139  
140 /* ---- the outbound client observer --------------------------------------- *
141 * Two jobs, and the second is the one that makes distributed tracing work at
142 * all: measure the call, and INJECT the traceparent so the far side continues
143 * this trace instead of starting its own. */
144  
145 1 static void *otel_ft_start(pTHX_ const char *method, STRLEN mlen,
146 const char *url, STRLEN ulen, AV *headers,
147 void *ud) {
148 otel_span *s;
149 SV *name;
150 const char *canon;
151 PERL_UNUSED_ARG(ud);
152  
153 1 ▶ if (OTEL_SUPPRESSED || !OTEL_INSTR.client) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (OTEL_SUPPRESSED || !OTEL_INSTR.client) return NULL; 1 0 1
if (OTEL_SUPPRESSED || !OTEL_INSTR.client) return NULL; 1 0 1
if (OTEL_SUPPRESSED || !OTEL_INSTR.client) return NULL; 1 0 1
if (OTEL_SUPPRESSED || !OTEL_INSTR.client) return NULL; 0 1 1
154  
155 1 canon = otel_sc_method(method, mlen);
156 1 ▶ name = sv_2mortal(newSVpv(canon ? canon : "HTTP", 0));
Branch: 1 branch
BranchTrueFalseTotal
name = sv_2mortal(newSVpv(canon ? canon : "HTTP", 0)); 1 0 1
157  
158 /* A `traceparent` THE CALLER ALREADY SET IS THE PARENT.
159 *
160 * Started with a NULL parent, every outbound call opened a BRAND NEW
161 * TRACE: the client span was a root, the header derived from it named
162 * that new trace, and the callee's server span joined a trace containing
163 * nothing but itself. The visible effect is a store in which every trace
164 * is one span long and no service map edge ever crosses a process.
165 *
166 * A caller that propagated deliberately keeps that precedence: its own
167 * header wins, and its call is joined to its own trace rather than
168 * silently overridden.
169 *
170 * WITH NO HEADER, THE REQUEST BEING SERVED IS THE PARENT. This used to say
171 * there was no ambient current span to take instead - that two requests
172 * are in flight on one worker at any moment and a process-global would
173 * attribute one request's calls to the other. That was true of a global
174 * that outlived its frame. pk_abi v5's current_of does not: it is saved
175 * and restored around the dispatch frame, so a call made from a
176 * continuation or a queue job reads NULL and is still a root. The
177 * degraded answer is no parent, never the wrong one.
178 */
179 {
180 1 ▶ SSize_t i, n = headers ? (av_len(headers) + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = headers ? (av_len(headers) + 1) : 0; 1 0 1
181 otel_ctx in;
182 1 int have = 0;
183 1 otel_ctx_clear(&in);
184  
185 1 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 1 0 1
186 1 SV **k = av_fetch(headers, i, 0);
187 1 SV **v = av_fetch(headers, i + 1, 0);
188 STRLEN kl, vl;
189 const char *kp, *vp;
190 1 ▶ if (!k || !*k || !v || !*v) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 0 1 1
191 1 kp = SvPV_const(*k, kl);
192 1 ▶ if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue; 1 0 1
if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue; 0 1 1
193 1 vp = SvPV_const(*v, vl);
194 1 have = otel_w3c_parse(vp, vl, &in);
195 1 break;
196 }
197  
198 1 ▶ if (have)
Branch: 1 branch
BranchTrueFalseTotal
if (have) 1 0 1
199 1 s = otel_tracer_start(aTHX_ OTEL_TRACER, in.trace_id, in.span_id,
200 1 (in.flags & OTEL_FLAG_SAMPLED) ? 1 : 0,
201 name, OTEL_KIND_CLIENT);
202 else {
203 0 otel_span *p = otel_current_span(aTHX_ otel_pk(aTHX));
204 0 ▶ s = otel_tracer_start(aTHX_ OTEL_TRACER,
Branch: 3 branches
BranchTrueFalseTotal
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 0 0
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 0 0
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 0 0
205 p ? p->trace_id : NULL,
206 p ? p->span_id : NULL,
207 p ? p->sampled : 0,
208 name, OTEL_KIND_CLIENT);
209 }
210 }
211 1 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 1 1
212  
213 {
214 1 SV *k = sv_2mortal(newSVpvs(SC_HTTP_METHOD));
215 1 ▶ otel_span_attr(aTHX_ s, k,
Branch: 1 branch
BranchTrueFalseTotal
otel_span_attr(aTHX_ s, k, 1 0 1
216 sv_2mortal(newSVpv(canon ? canon : "_OTHER", 0)));
217 1 ▶ if (!canon) {
Branch: 1 branch
BranchTrueFalseTotal
if (!canon) { 0 1 1
218 0 SV *ko = sv_2mortal(newSVpvs(SC_HTTP_METHOD_ORIGINAL));
219 0 otel_span_attr(aTHX_ s, ko, sv_2mortal(newSVpvn(method, mlen)));
220 }
221 /* url.full on a CLIENT span is bounded by what this process calls,
222 * not by what a client sends it, so it is safe here in a way the
223 * server-side path never is */
224 {
225 1 SV *ku = sv_2mortal(newSVpvs(SC_URL_FULL));
226 1 otel_span_attr(aTHX_ s, ku, sv_2mortal(newSVpvn(url, ulen)));
227 }
228 }
229  
230 /* The traceparent. Without this the far side starts a new trace and the
231 * two halves of the call are never joined - which is the entire point of
232 * the hook Fetch grew.
233 *
234 * SET IN PLACE WHERE ONE IS ALREADY THERE, never appended beside it. Two
235 * `traceparent` headers on one request is not a request with a fallback:
236 * it is malformed, the callee picks whichever it picks, and the trace
237 * joins up or does not depending on which. The value is REPLACED rather
238 * than left alone because the callee should be a child of this client
239 * span - which is itself now a child of whatever the caller propagated,
240 * so the chain is caller -> client -> callee either way. */
241 {
242 otel_ctx ctx;
243 char buf[56];
244 1 ▶ SSize_t i, n = headers ? (av_len(headers) + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = headers ? (av_len(headers) + 1) : 0; 1 0 1
245 1 int replaced = 0;
246  
247 1 otel_ctx_clear(&ctx);
248 1 Copy(s->trace_id, ctx.trace_id, 16, unsigned char);
249 1 Copy(s->span_id, ctx.span_id, 8, unsigned char);
250 1 ctx.flags = OTEL_FLAG_SAMPLED; /* the span exists, so it is sampled */
251 1 otel_w3c_format(&ctx, buf);
252  
253 1 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 1 0 1
254 1 SV **k = av_fetch(headers, i, 0);
255 1 SV **v = av_fetch(headers, i + 1, 0);
256 STRLEN kl;
257 const char *kp;
258 1 ▶ if (!k || !*k || !v || !*v) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 1 0 1
if (!k || !*k || !v || !*v) continue; 0 1 1
259 1 kp = SvPV_const(*k, kl);
260 1 ▶ if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue; 1 0 1
if (kl != 11 || !otel_ieq(kp, "traceparent", 11)) continue; 0 1 1
261 1 sv_setpv(*v, buf);
262 1 replaced = 1;
263 1 break;
264 }
265 1 ▶ if (!replaced) {
Branch: 1 branch
BranchTrueFalseTotal
if (!replaced) { 0 1 1
266 0 av_push(headers, newSVpvs("traceparent"));
267 0 av_push(headers, newSVpv(buf, 0));
268 }
269 }
270 1 return (void *)s;
271 }
272  
273 1 static void otel_ft_done(pTHX_ void *token, SV *res, SV *err, void *ud) {
274 1 otel_span *s = (otel_span *)token;
275 PERL_UNUSED_ARG(ud);
276 1 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 1 1
277  
278 1 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 1 1
279 /* a timeout, a refused connection, a DNS failure: the endings an
280 * instrumented client most wants and the easiest to lose */
281 0 s->status_code = OTEL_STATUS_ERROR;
282 0 ▶ if (SvOK(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(err)) { 0 0 0
283 0 SV *k = sv_2mortal(newSVpvs(SC_ERROR_TYPE));
284 0 otel_span_attr(aTHX_ s, k, err);
285 }
286 }
287 1 ▶ else if (res && OTEL_FT) {
Branch: 2 branches
BranchTrueFalseTotal
else if (res && OTEL_FT) { 1 0 1
else if (res && OTEL_FT) { 1 0 1
288 1 int status = 0;
289 1 OTEL_FT->res_parts(aTHX_ res, &status, NULL, NULL);
290 1 ▶ if (status > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status > 0) { 1 0 1
291 1 SV *k = sv_2mortal(newSVpvs(SC_HTTP_STATUS));
292 1 otel_span_attr(aTHX_ s, k, sv_2mortal(newSViv(status)));
293 /* on a CLIENT span a 4xx IS a failure of the call this process
294 * made, which is the opposite of the server-side rule */
295 1 s->status_code = otel_sc_client_status(status);
296 }
297 }
298 1 otel_span_end(aTHX_ s);
299 1 ▶ if (OTEL_TRACER) {
Branch: 1 branch
BranchTrueFalseTotal
if (OTEL_TRACER) { 1 0 1
300 1 otel_tracer_enqueue(aTHX_ OTEL_TRACER, s);
301 1 OTEL_TRACER->ended++;
302 }
303 0 else otel_span_free(aTHX_ s);
304 }
305  
306 #endif /* OTEL_CONSUME_H */
« include/otel_ctx.h ↑ Summary xs/encode.xs »