include/otel_instr.h

filter: Statementstmt 93.0% 175 / 188 Branchbran 57.1% 128 / 224 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.5% 303 / 412 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« OpenTelemetry.xs ↑ Summary xs/exporter.xs »
1 /* otel_instr.h - where the spans actually come from.
2 *
3 * The observers registered with the hooks phase 1 added: Punk's pk_abi
4 * request/response and query observers, Fetch's outbound observer, and
5 * DBIx::Loop's statement observer. Each turns an event into a span.
6 *
7 * TWO RULES GOVERN EVERYTHING HERE.
8 *
9 * 1. THE SAMPLING DECISION COMES FIRST. Attributes are built only after a
10 * span exists. An unsampled request must not pay to assemble strings
11 * nobody will read - which is the whole reason otel_tracer_start returns
12 * NULL rather than a null object, and why every function below begins by
13 * checking for one.
14 *
15 * 2. http.route IS THE PATTERN OR IT IS ABSENT. Never the path. For a 404,
16 * a 405 or a mounted app, pk_abi's route_pattern_of returns NULL and the
17 * attribute is simply not set. Falling back to url.path there is the exact
18 * substitution that turns a bounded dimension into an unbounded one, and
19 * 404 traffic is precisely where a scanner will hand you a million
20 * distinct values.
21 */
22  
23 #ifndef OTEL_INSTR_H
24 #define OTEL_INSTR_H
25  
26 #include "pk_abi.h" /* the observers below are typed by it */
27 #include "otel_semconv.h"
28 #include "otel_tracer.h"
29 #include "otel_w3c.h"
30  
31 /* Which instrumentation points are live. Each is switchable on its own, so an
32 * application drowning in database spans can turn those off without losing
33 * its server spans. */
34 typedef struct {
35 int server;
36 int client;
37 int db;
38 int metrics;
39 int enabled; /* the master switch: OTEL_SDK_DISABLED */
40 int suppress; /* re-entrancy guard; see below */
41 } otel_instr_cfg;
42  
43 static otel_instr_cfg OTEL_INSTR = { 1, 1, 1, 1, 1, 0 };
44 static otel_tracer *OTEL_TRACER = NULL;
45  
46 /* THE METER IS NOT THE TRACER, and the difference is the whole of the metrics
47 * path below.
48 *
49 * A trace is a SAMPLE of requests. A duration histogram over a sample is not a
50 * smaller histogram, it is a wrong one - so everything recorded here happens
51 * on EVERY request, including the ones otel_tracer_start declined to build a
52 * span for. What an unsampled request still must not pay is attribute
53 * assembly, and it does not: the attributes are read on the response side out
54 * of values that already exist there. */
55 static otel_meter *OTEL_METER = NULL;
56  
57 /* THE RECURSION GUARD.
58 *
59 * The exporter sends spans over HTTP with Fetch. Fetch is instrumented. So an
60 * export produces a client span, which is queued, and exported... The first
61 * collector outage would otherwise become an infinite loop of telemetry about
62 * failing to send telemetry.
63 *
64 * Everything the SDK does on its own behalf runs with this set, and every
65 * instrumentation point below checks it. */
66 #define OTEL_SUPPRESSED (OTEL_INSTR.suppress || !OTEL_INSTR.enabled \
67 || !OTEL_TRACER)
68  
69 27 static void otel_suppress_begin(void) { OTEL_INSTR.suppress++; }
70 28 ▶static void otel_suppress_end(void) { if (OTEL_INSTR.suppress)
Branch: 1 branch
BranchTrueFalseTotal
static void otel_suppress_end(void) { if (OTEL_INSTR.suppress) 27 1 28
71 27 OTEL_INSTR.suppress--; }
72  
73 /* ---- the per-request span slot ------------------------------------------ *
74 * The server span starts in one callback and ends in another, so it has to
75 * live somewhere in between. pk_abi hands both callbacks the SAME context and
76 * will create its stash on demand, which is exactly the hook it was given for.
77 */
78 #define OTEL_STASH_KEY "punk.otel.span"
79  
80 /* The request's clock, stashed whatever the sampling decision was. Two
81 * integers, no strings, no allocation beyond the SVs the stash needs. */
82 #define OTEL_CLOCK_KEY "punk.otel.t0"
83  
84 8 static void otel_stash_span(pTHX_ const pk_abi *A, SV *c, otel_span *s) {
85 8 SV *st = A->stash_of(aTHX_ c);
86 8 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 8 0 8
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 8 0 8
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 0 8 8
87 8 (void)hv_store((HV *)SvRV(st), OTEL_STASH_KEY,
88 (I32)(sizeof(OTEL_STASH_KEY) - 1),
89 newSViv(PTR2IV(s)), 0);
90 }
91  
92 /* Look, and leave it there. The response side ENDS the span it takes, so
93 * taking is right exactly once; a parent lookup happens many times per request
94 * and must leave the span where the response side will still find it. */
95 7 static otel_span *otel_peek_span(pTHX_ const pk_abi *A, SV *c) {
96 SV *st;
97 SV **e;
98 7 ▶ if (!(A && c)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(A && c)) return NULL; 7 0 7
if (!(A && c)) return NULL; 0 7 7
99 7 st = A->stash_of(aTHX_ c);
100 7 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 0 7 7
101 7 e = hv_fetch((HV *)SvRV(st), OTEL_STASH_KEY,
102 (I32)(sizeof(OTEL_STASH_KEY) - 1), 0);
103 7 ▶ if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 7 0 7
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 7 0 7
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 7 0 7
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 0 7 7
104 7 return INT2PTR(otel_span *, SvIV(*e));
105 }
106  
107 7 static void otel_stash_clock(pTHX_ const pk_abi *A, SV *c,
108 U64TYPE wall, U64TYPE mono) {
109 7 SV *st = A->stash_of(aTHX_ c);
110 AV *av;
111 7 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return; 0 7 7
112 7 av = newAV();
113 7 av_push(av, newSVuv((UV)wall));
114 7 av_push(av, newSVuv((UV)mono));
115 7 (void)hv_store((HV *)SvRV(st), OTEL_CLOCK_KEY,
116 (I32)(sizeof(OTEL_CLOCK_KEY) - 1),
117 newRV_noinc((SV *)av), 0);
118 }
119  
120 /* The duration in SECONDS, which is the unit the conventions name, or -1 when
121 * this request was never clocked. */
122 7 static double otel_unstash_duration(pTHX_ const pk_abi *A, SV *c) {
123 7 SV *st = A->stash_of(aTHX_ c);
124 SV **e;
125 AV *av;
126 SV **w, **m;
127 7 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return -1; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return -1; 7 0 7
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return -1; 0 7 7
128 7 e = hv_fetch((HV *)SvRV(st), OTEL_CLOCK_KEY,
129 (I32)(sizeof(OTEL_CLOCK_KEY) - 1), 0);
130 7 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return -1; 7 0 7
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return -1; 7 0 7
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return -1; 7 0 7
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return -1; 0 7 7
131 7 av = (AV *)SvRV(*e);
132 7 w = av_fetch(av, 0, 0);
133 7 m = av_fetch(av, 1, 0);
134 7 ▶ if (!(w && *w && m && *m)) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!(w && *w && m && *m)) return -1; 7 0 7
if (!(w && *w && m && *m)) return -1; 7 0 7
if (!(w && *w && m && *m)) return -1; 7 0 7
if (!(w && *w && m && *m)) return -1; 0 7 7
135 {
136 7 U64TYPE start_wall = (U64TYPE)SvUV(*w);
137 7 U64TYPE end = otel_end_nanos(start_wall, (U64TYPE)SvUV(*m));
138 7 ▶ return (end > start_wall) ? (double)(end - start_wall) / 1e9 : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
return (end > start_wall) ? (double)(end - start_wall) / 1e9 : 0.0; 7 0 7
139 }
140 }
141  
142 10 static otel_span *otel_unstash_span(pTHX_ const pk_abi *A, SV *c) {
143 10 SV *st = A->stash_of(aTHX_ c);
144 SV **e;
145 otel_span *s;
146 10 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 10 0 10
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 10 0 10
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 0 10 10
147 10 e = hv_fetch((HV *)SvRV(st), OTEL_STASH_KEY,
148 (I32)(sizeof(OTEL_STASH_KEY) - 1), 0);
149 10 ▶ if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 8 2 10
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 8 0 8
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 8 0 8
if (!(e && *e && SvIOK(*e) && SvIV(*e))) return NULL; 0 8 8
150 8 s = INT2PTR(otel_span *, SvIV(*e));
151 8 sv_setiv(*e, 0); /* taken: a second response event finds nothing */
152 8 return s;
153 }
154  
155 /* The span a child should hang off: the server span of the request whose
156 * dispatch frame is running right now, or NULL.
157 *
158 * pk_abi v5. Below it there is no way to ask, and every child is a root - the
159 * behaviour this whole release exists to end, kept as the degraded path so an
160 * older Punk still works rather than failing to load. */
161 3 static otel_span *otel_current_span(pTHX_ const pk_abi *A) {
162 SV *c;
163 3 ▶ if (!(A && A->abi_version >= 5)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(A && A->abi_version >= 5)) return NULL; 3 0 3
if (!(A && A->abi_version >= 5)) return NULL; 0 3 3
164 3 c = A->current_of(aTHX);
165 3 ▶ return c ? otel_peek_span(aTHX_ A, c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return c ? otel_peek_span(aTHX_ A, c) : NULL; 1 2 3
166 }
167  
168 /* ---- a borrowed env string ---------------------------------------------- */
169 75 static const char *otel_env_str(pTHX_ HV *env, const char *k, STRLEN *len) {
170 75 ▶ SV **e = env ? hv_fetch(env, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = env ? hv_fetch(env, k, (I32)strlen(k), 0) : NULL; 75 0 75
171 118 ▶ if (e && *e && SvOK(*e)) return SvPV_const(*e, *len);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 43 32 75
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 43 0 43
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 43 0 43
172 32 *len = 0;
173 32 return NULL;
174 }
175  
176 /* ---- the server span ---------------------------------------------------- */
177  
178 10 static void otel_on_request(pTHX_ SV *c, void *ud) {
179 10 const pk_abi *A = (const pk_abi *)ud;
180 SV *envsv;
181 HV *env;
182 otel_ctx ctx;
183 otel_span *s;
184 const char *m, *tp;
185 STRLEN ml, tpl;
186 SV *name;
187 10 int has_parent = 0;
188  
189 12 ▶ if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return;
Branch: 2 branches
BranchTrueFalseTotal
if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 10 0 10
if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 0 10 10
190  
191 /* THE CLOCK STARTS BEFORE THE SAMPLING DECISION, AND BEFORE THE SERVER
192 * SWITCH.
193 *
194 * A duration histogram over the sampled requests is not a smaller
195 * histogram, it is a wrong one - so every request is clocked, including
196 * the ones that get no span at all, and including a process that has
197 * turned server SPANS off and kept its metrics. Two integers into the
198 * stash the dispatcher has already built.
199 *
200 * The file's first rule is about not assembling STRINGS for a request
201 * nobody will read. None are assembled here, and none on the way out
202 * either: the response side reads values that already exist. */
203 10 ▶ if (OTEL_METER && OTEL_INSTR.metrics)
Branch: 2 branches
BranchTrueFalseTotal
if (OTEL_METER && OTEL_INSTR.metrics) 7 3 10
if (OTEL_METER && OTEL_INSTR.metrics) 7 0 7
204 7 otel_stash_clock(aTHX_ A, c, otel_wall_nanos(), otel_mono_nanos());
205  
206 10 ▶ if (!OTEL_TRACER || !OTEL_INSTR.server) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_TRACER || !OTEL_INSTR.server) return; 10 0 10
if (!OTEL_TRACER || !OTEL_INSTR.server) return; 0 10 10
207 10 envsv = A->env_of(aTHX_ c);
208 10 ▶ env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 10 0 10
env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 10 0 10
209 20 ▶ ? (HV *)SvRV(envsv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(envsv) : NULL; 10 0 10
210 10 ▶ if (!env) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!env) return; 0 10 10
211  
212 /* the inbound context, if any. An unparseable header is absent, not an
213 * error, so this can only ever leave us starting a root span. */
214 10 otel_ctx_clear(&ctx);
215 10 tp = otel_env_str(aTHX_ env, "HTTP_TRACEPARENT", &tpl);
216 10 ▶ if (tp && otel_w3c_parse(tp, tpl, &ctx)) has_parent = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (tp && otel_w3c_parse(tp, tpl, &ctx)) has_parent = 1; 0 10 10
if (tp && otel_w3c_parse(tp, tpl, &ctx)) has_parent = 1; 0 0 0
217  
218 /* The span name. At request start the route is NOT known - routing has
219 * not happened - so the method alone is the honest name, and the response
220 * side upgrades it to "GET /users/:id" once the pattern exists. Naming it
221 * after the PATH here would be the cardinality mistake, permanently. */
222 10 m = otel_env_str(aTHX_ env, "REQUEST_METHOD", &ml);
223 10 ▶ name = sv_2mortal(newSVpvn(m ? m : "HTTP", m ? ml : 4));
Branch: 2 branches
BranchTrueFalseTotal
name = sv_2mortal(newSVpvn(m ? m : "HTTP", m ? ml : 4)); 10 0 10
name = sv_2mortal(newSVpvn(m ? m : "HTTP", m ? ml : 4)); 10 0 10
224  
225 10 ▶ s = otel_tracer_start(aTHX_ OTEL_TRACER,
Branch: 3 branches
BranchTrueFalseTotal
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 10 10
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 10 10
s = otel_tracer_start(aTHX_ OTEL_TRACER, 0 10 10
226 has_parent ? ctx.trace_id : NULL,
227 has_parent ? ctx.span_id : NULL,
228 0 has_parent ? (ctx.flags & OTEL_FLAG_SAMPLED) : 0,
229 name, OTEL_KIND_SERVER);
230 10 ▶ if (!s) return; /* not sampled: nothing was built */
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; /* not sampled: nothing was built */ 2 8 10
231  
232 /* Everything below happens ONLY for a sampled request. */
233 {
234 const char *v;
235 STRLEN vl;
236 8 const char *canon = otel_sc_method(m, ml);
237 8 SV *k = sv_2mortal(newSVpvs(SC_HTTP_METHOD));
238 8 ▶ otel_span_attr(aTHX_ s, k,
Branch: 1 branch
BranchTrueFalseTotal
otel_span_attr(aTHX_ s, k, 8 0 8
239 sv_2mortal(newSVpv(canon ? canon : "_OTHER", 0)));
240 8 ▶ if (!canon && m) {
Branch: 2 branches
BranchTrueFalseTotal
if (!canon && m) { 0 8 8
if (!canon && m) { 0 0 0
241 /* an unknown method is bounded to _OTHER, and the raw value kept
242 * separately where it cannot become a metric dimension */
243 0 SV *ko = sv_2mortal(newSVpvs(SC_HTTP_METHOD_ORIGINAL));
244 0 otel_span_attr(aTHX_ s, ko, sv_2mortal(newSVpvn(m, ml)));
245 }
246 8 ▶ if ((v = otel_env_str(aTHX_ env, "PATH_INFO", &vl))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "PATH_INFO", &vl))) { 8 0 8
247 8 SV *kk = sv_2mortal(newSVpvs(SC_URL_PATH));
248 8 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
249 }
250 8 ▶ if ((v = otel_env_str(aTHX_ env, "QUERY_STRING", &vl)) && vl) {
Branch: 2 branches
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "QUERY_STRING", &vl)) && vl) { 6 2 8
if ((v = otel_env_str(aTHX_ env, "QUERY_STRING", &vl)) && vl) { 1 5 6
251 1 SV *kk = sv_2mortal(newSVpvs(SC_URL_QUERY));
252 1 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
253 }
254 8 ▶ if ((v = otel_env_str(aTHX_ env, "psgi.url_scheme", &vl))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "psgi.url_scheme", &vl))) { 5 3 8
255 5 SV *kk = sv_2mortal(newSVpvs(SC_URL_SCHEME));
256 5 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
257 }
258 8 ▶ if ((v = otel_env_str(aTHX_ env, "HTTP_USER_AGENT", &vl))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "HTTP_USER_AGENT", &vl))) { 1 7 8
259 1 SV *kk = sv_2mortal(newSVpvs(SC_USER_AGENT));
260 1 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
261 }
262 /* REMOTE_ADDR, which Punk has already rewritten to the real client
263 * when a `proxy` policy is configured - so this is the right value
264 * for free, and the wrong one (the proxy) when it is not. */
265 8 ▶ if ((v = otel_env_str(aTHX_ env, "REMOTE_ADDR", &vl))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "REMOTE_ADDR", &vl))) { 1 7 8
266 1 SV *kk = sv_2mortal(newSVpvs(SC_CLIENT_ADDRESS));
267 1 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
268 }
269 8 ▶ if ((v = otel_env_str(aTHX_ env, "SERVER_NAME", &vl))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = otel_env_str(aTHX_ env, "SERVER_NAME", &vl))) { 5 3 8
270 5 SV *kk = sv_2mortal(newSVpvs(SC_SERVER_ADDRESS));
271 5 otel_span_attr(aTHX_ s, kk, sv_2mortal(newSVpvn(v, vl)));
272 }
273 }
274 8 otel_stash_span(aTHX_ A, c, s);
275 }
276  
277 /* http.server.request.duration, on EVERY request.
278 *
279 * The three attributes are the bounded ones and only those. http.route is the
280 * declared pattern or it is ABSENT - never url.path, which is the substitution
281 * that turns a bounded dimension into an unbounded one, and a metric label is
282 * where that costs the most: a scanner's million 404 paths become a million
283 * series, and the cardinality cap then drops whatever arrives next.
284 *
285 * `span` is the exemplar and is NULL for an unsampled request, which is
286 * exactly right: the measurement still counts, it just has no trace to point
287 * at. */
288 10 static void otel_metric_response(pTHX_ const pk_abi *A, SV *c, IV status,
289 SV *route, otel_span *span) {
290 otel_instrument *in;
291 HV *attrs;
292 double secs;
293 SV *env;
294 10 const char *m = NULL;
295 10 STRLEN ml = 0;
296  
297 10 ▶ if (!OTEL_METER || !OTEL_INSTR.metrics) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_METER || !OTEL_INSTR.metrics) return; 7 3 10
if (!OTEL_METER || !OTEL_INSTR.metrics) return; 0 7 7
298 7 secs = otel_unstash_duration(aTHX_ A, c);
299 7 ▶ if (secs < 0) return; /* never clocked: nothing to say */
Branch: 1 branch
BranchTrueFalseTotal
if (secs < 0) return; /* never clocked: nothing to say */ 0 7 7
300  
301 7 otel_meter_check_fork(aTHX_ OTEL_METER);
302 {
303 7 SV *name = sv_2mortal(newSVpvs(SC_HTTP_SERVER_DURATION));
304 7 SV *unit = sv_2mortal(newSVpvs("s"));
305 7 in = otel_meter_instrument(aTHX_ OTEL_METER, name,
306 OTEL_INSTR_HISTOGRAM, unit, NULL);
307 }
308 7 ▶ if (!in) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!in) return; 0 7 7
309  
310 7 attrs = (HV *)sv_2mortal((SV *)newHV());
311 7 env = A->env_of(aTHX_ c);
312 7 ▶ if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 7 0 7
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 7 0 7
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 7 0 7
313 7 m = otel_env_str(aTHX_ (HV *)SvRV(env), "REQUEST_METHOD", &ml);
314 {
315 7 const char *canon = otel_sc_method(m, ml);
316 7 ▶ (void)hv_stores(attrs, SC_HTTP_METHOD,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(attrs, SC_HTTP_METHOD, 7 0 7
317 newSVpv(canon ? canon : "_OTHER", 0));
318 }
319 7 ▶ if (route && SvOK(route))
Branch: 2 branches
BranchTrueFalseTotal
if (route && SvOK(route)) 6 1 7
if (route && SvOK(route)) 6 0 6
320 6 (void)hv_stores(attrs, SC_HTTP_ROUTE, newSVsv(route));
321 7 ▶ if (status > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (status > 0) 7 0 7
322 7 (void)hv_stores(attrs, SC_HTTP_STATUS, newSViv(status));
323  
324 7 otel_instr_record(aTHX_ in, secs,
325 otel_view_filter(aTHX_ OTEL_METER,
326 sv_2mortal(newSVpvs(SC_HTTP_SERVER_DURATION)), attrs),
327 span);
328 }
329  
330 10 static void otel_on_response(pTHX_ SV *c, SV *response, void *ud) {
331 10 const pk_abi *A = (const pk_abi *)ud;
332 otel_span *s;
333 IV status;
334 SV *route, *op;
335  
336 10 ▶ if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return;
Branch: 2 branches
BranchTrueFalseTotal
if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 10 0 10
if (OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 0 10 10
337  
338 /* THE METRIC FIRST, because it does not depend on there being a span and
339 * must not be skipped with one. The route and status are read once here
340 * and handed to both. */
341 10 route = A->route_pattern_of(aTHX_ c);
342 10 ▶ if (!(route && SvOK(route))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(route && SvOK(route))) { 8 2 10
if (!(route && SvOK(route))) { 0 8 8
343 2 SV *o = A->operation_of(aTHX_ c);
344 2 ▶ if (o && SvOK(o)) route = o; /* an API operation is bounded too */
Branch: 2 branches
BranchTrueFalseTotal
if (o && SvOK(o)) route = o; /* an API operation is bounded too */ 0 2 2
if (o && SvOK(o)) route = o; /* an API operation is bounded too */ 0 0 0
345 2 else route = NULL;
346 }
347 10 status = A->status_of(aTHX_ response);
348  
349 10 ▶ s = (OTEL_TRACER && OTEL_INSTR.server)
Branch: 1 branch
BranchTrueFalseTotal
s = (OTEL_TRACER && OTEL_INSTR.server) 10 0 10
350 20 ▶ ? otel_unstash_span(aTHX_ A, c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? otel_unstash_span(aTHX_ A, c) : NULL; 10 0 10
351  
352 10 otel_metric_response(aTHX_ A, c, status, route, s);
353  
354 10 ▶ if (!s) return; /* unsampled, or already finished */
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; /* unsampled, or already finished */ 2 8 10
355  
356 /* http.route is the DECLARED pattern, and absent when there is none.
357 * Never url.path - that substitution is how a bounded dimension becomes
358 * unbounded, and 404 traffic is where a scanner supplies the million
359 * distinct values. Read above, so the span and the metric cannot disagree
360 * about what the route was. */
361 15 ▶ if (route && SvOK(route)) {
Branch: 2 branches
BranchTrueFalseTotal
if (route && SvOK(route)) { 7 1 8
if (route && SvOK(route)) { 7 0 7
362 7 SV *k = sv_2mortal(newSVpvs(SC_HTTP_ROUTE));
363 7 otel_span_attr(aTHX_ s, k, route);
364 /* now the span can be named properly: "GET /users/:id" */
365 7 ▶ if (s->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->name) { 7 0 7
366 7 SV *n = newSVsv(s->name);
367 7 sv_catpvs(n, " ");
368 7 sv_catsv(n, route);
369 7 SvREFCNT_dec(s->name);
370 7 s->name = n;
371 }
372 }
373 1 ▶ else if ((op = A->operation_of(aTHX_ c)) && SvOK(op)) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((op = A->operation_of(aTHX_ c)) && SvOK(op)) { 0 1 1
else if ((op = A->operation_of(aTHX_ c)) && SvOK(op)) { 0 0 0
374 /* an API operation has no route pattern; its operationId is the
375 * bounded identifier that plays the same part */
376 0 SV *k = sv_2mortal(newSVpvs(SC_HTTP_ROUTE));
377 0 otel_span_attr(aTHX_ s, k, op);
378 0 ▶ if (s->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->name) { 0 0 0
379 0 SV *n = newSVsv(s->name);
380 0 sv_catpvs(n, " ");
381 0 sv_catsv(n, op);
382 0 SvREFCNT_dec(s->name);
383 0 s->name = n;
384 }
385 }
386  
387 8 status = A->status_of(aTHX_ response);
388 8 ▶ if (status > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status > 0) { 8 0 8
389 8 SV *k = sv_2mortal(newSVpvs(SC_HTTP_STATUS));
390 8 otel_span_attr(aTHX_ s, k, sv_2mortal(newSViv(status)));
391 8 s->status_code = otel_sc_server_status(status);
392 8 ▶ if (s->status_code == OTEL_STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->status_code == OTEL_STATUS_ERROR) { 5 3 8
393 5 SV *ek = sv_2mortal(newSVpvs(SC_ERROR_TYPE));
394 5 otel_span_attr(aTHX_ s, ek, sv_2mortal(newSViv(status)));
395 }
396 }
397 8 otel_span_end(aTHX_ s);
398 8 otel_tracer_enqueue(aTHX_ OTEL_TRACER, s);
399 8 OTEL_TRACER->ended++;
400 }
401  
402 /* ---- database spans ----------------------------------------------------- *
403 * Both database paths report the same way. The statement text is the PREPARED
404 * one - the observers in phase 1 do not pass bind values at all, so this
405 * cannot leak the literal data even by accident. */
406  
407 3 static otel_span *otel_db_start(pTHX_ const pk_abi *A, const char *sql,
408 STRLEN len, int nbind, const char *system) {
409 otel_span *s, *parent;
410 char op[32];
411 SV *name;
412 3 ▶ if (OTEL_SUPPRESSED || !OTEL_INSTR.db) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (OTEL_SUPPRESSED || !OTEL_INSTR.db) return NULL; 3 0 3
if (OTEL_SUPPRESSED || !OTEL_INSTR.db) return NULL; 3 0 3
if (OTEL_SUPPRESSED || !OTEL_INSTR.db) return NULL; 3 0 3
if (OTEL_SUPPRESSED || !OTEL_INSTR.db) return NULL; 0 3 3
413  
414 /* the span is named for the OPERATION, not the statement: a name is a
415 * grouping key, and one distinct name per distinct SQL string is another
416 * unbounded dimension */
417 3 ▶ if (otel_sc_db_operation(sql, len, op, sizeof op))
Branch: 1 branch
BranchTrueFalseTotal
if (otel_sc_db_operation(sql, len, op, sizeof op)) 3 0 3
418 3 name = sv_2mortal(newSVpv(op, 0));
419 else
420 0 name = sv_2mortal(newSVpvs("query"));
421  
422 /* THE REQUEST THAT ISSUED IT IS THE PARENT.
423 *
424 * Started with a NULL parent this opened a BRAND NEW TRACE for every
425 * statement: the query span was a root, and a store filled up with traces
426 * one span long while the request that ran the query sat in a different
427 * trace of its own. It is the same fault the outbound client span had, and
428 * the same fix, arriving late because the observer is handed no context
429 * and until pk_abi v5 there was nowhere to ask.
430 *
431 * Not a process-global: current_of is live only inside a dispatch frame,
432 * so a statement from a queue job or a continuation still reads NULL and
433 * is still an honest root. No parent, never the wrong one. */
434 3 parent = otel_current_span(aTHX_ A);
435 3 ▶ s = otel_tracer_start(aTHX_ OTEL_TRACER,
Branch: 3 branches
BranchTrueFalseTotal
s = otel_tracer_start(aTHX_ OTEL_TRACER, 1 2 3
s = otel_tracer_start(aTHX_ OTEL_TRACER, 1 2 3
s = otel_tracer_start(aTHX_ OTEL_TRACER, 1 2 3
436 parent ? parent->trace_id : NULL,
437 parent ? parent->span_id : NULL,
438 parent ? parent->sampled : 0,
439 name, OTEL_KIND_CLIENT);
440 3 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 3 3
441 {
442 3 SV *k = sv_2mortal(newSVpvs(SC_DB_QUERY_TEXT));
443 3 otel_span_attr(aTHX_ s, k, sv_2mortal(newSVpvn(sql, len)));
444 3 ▶ if (system) {
Branch: 1 branch
BranchTrueFalseTotal
if (system) { 3 0 3
445 3 SV *ks = sv_2mortal(newSVpvs(SC_DB_SYSTEM));
446 3 otel_span_attr(aTHX_ s, ks, sv_2mortal(newSVpv(system, 0)));
447 }
448 3 ▶ if (op[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (op[0]) { 3 0 3
449 3 SV *ko = sv_2mortal(newSVpvs(SC_DB_OPERATION));
450 3 otel_span_attr(aTHX_ s, ko, sv_2mortal(newSVpv(op, 0)));
451 }
452 PERL_UNUSED_VAR(nbind);
453 }
454 3 return s;
455 }
456  
457 3 static void otel_db_end(pTHX_ otel_span *s, int ok) {
458 3 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 3 3
459 3 ▶ if (!ok) s->status_code = OTEL_STATUS_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) s->status_code = OTEL_STATUS_ERROR; 0 3 3
460 3 otel_span_end(aTHX_ s);
461 3 ▶ if (OTEL_TRACER) {
Branch: 1 branch
BranchTrueFalseTotal
if (OTEL_TRACER) { 3 0 3
462 3 otel_tracer_enqueue(aTHX_ OTEL_TRACER, s);
463 3 OTEL_TRACER->ended++;
464 }
465 0 else otel_span_free(aTHX_ s);
466 }
467  
468 /* pk_abi's on_query: the shipped Punk::Model::DBI backend, and - since Punk
469 * 0.50 - every statement run through its handle, not only the six generated
470 * methods. `ud` is the table, so the parent can be looked up. */
471 3 static void *otel_on_query(pTHX_ const char *sql, STRLEN len, int nbind,
472 void *ud) {
473 3 return (void *)otel_db_start(aTHX_ (const pk_abi *)ud, sql, len, nbind,
474 "other_sql");
475 }
476  
477 3 static void otel_on_query_done(pTHX_ void *token, int ok, void *ud) {
478 PERL_UNUSED_ARG(ud);
479 3 otel_db_end(aTHX_ (otel_span *)token, ok);
480 3 }
481  
482 #endif /* OTEL_INSTR_H */
« OpenTelemetry.xs ↑ Summary xs/exporter.xs »