xs/instrument.xs

filter: Statementstmt 100% 73 / 73 Branchbran 70.5% 55 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 128 / 151 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/meter.xs ↑ Summary include/otel_signal_pb.h »
1 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Instrument
2  
3 PROTOTYPES: DISABLE
4  
5 # Wire the SDK to the hooks the ecosystem grew for it: Punk's pk_abi request,
6 # response and query observers, Fetch's outbound observer, and DBIx::Loop's
7 # statement observer.
8 #
9 # Every registration is process-global and permanent, which is what those
10 # tables offer; install() is therefore idempotent and is meant to be called
11 # once, at boot, from the worker that will serve.
12  
13 # install($tracer, %opt) -> a hashref of which points went live.
14 #
15 # Options: server, client, db (each 1 by default).
16 SV *
17 install(tracer, ...)
18 SV *tracer
19 CODE:
20 {
21 24 HV *out = newHV();
22 int i;
23 24 int want_server = 1, want_client = 1, want_db = 1, want_metrics = 1;
24 24 SV *meter = NULL;
25  
26 24 ▶ if (!(SvROK(tracer) && SvIOK(SvRV(tracer))))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(tracer) && SvIOK(SvRV(tracer)))) 23 1 24
if (!(SvROK(tracer) && SvIOK(SvRV(tracer)))) 0 23 23
27 1 croak("Punk::OpenTelemetry::Instrument::install: "
28 "expected a Punk::OpenTelemetry::Tracer");
29 100 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 77 23 100
30 77 const char *k = SvPV_nolen(ST(i));
31 77 int v = SvTRUE(ST(i + 1)) ? 1 : 0;
32 77 ▶ if (strEQ(k, "server")) want_server = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "server")) want_server = v; 15 62 77
33 62 ▶ else if (strEQ(k, "client")) want_client = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "client")) want_client = v; 16 46 62
34 46 ▶ else if (strEQ(k, "db")) want_db = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "db")) want_db = v; 15 31 46
35 31 ▶ else if (strEQ(k, "metrics")) want_metrics = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "metrics")) want_metrics = v; 15 16 31
36 /* the meter is an object, not a flag, so it is read before SvTRUE
37 * turns it into one */
38 16 ▶ else if (strEQ(k, "meter")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "meter")) { 16 0 16
39 16 SV *mv = ST(i + 1);
40 16 ▶ if (SvROK(mv) && SvIOK(SvRV(mv)) && SvIV(SvRV(mv))) meter = mv;
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(mv) && SvIOK(SvRV(mv)) && SvIV(SvRV(mv))) meter = mv; 16 0 16
if (SvROK(mv) && SvIOK(SvRV(mv)) && SvIV(SvRV(mv))) meter = mv; 16 0 16
if (SvROK(mv) && SvIOK(SvRV(mv)) && SvIV(SvRV(mv))) meter = mv; 16 0 16
41 }
42 }
43 23 OTEL_TRACER = INT2PTR(otel_tracer *, SvIV(SvRV(tracer)));
44 23 OTEL_INSTR.server = want_server;
45 23 OTEL_INSTR.client = want_client;
46 23 OTEL_INSTR.db = want_db;
47 23 OTEL_INSTR.metrics = want_metrics;
48 /* The HTTP duration histogram goes here. Without a meter the
49 * instrumentation records nothing and the clock is never stashed -
50 * a deployment with the metrics signal off pays nothing at all. */
51 23 ▶ OTEL_METER = meter ? INT2PTR(otel_meter *, SvIV(SvRV(meter))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
OTEL_METER = meter ? INT2PTR(otel_meter *, SvIV(SvRV(meter))) : NULL; 16 7 23
52  
53 /* ---- Punk: server spans and the shipped DBI backend ------------- */
54 {
55 23 const pk_abi *A = otel_pk(aTHX);
56 27 ▶ if (A && !OTEL_PK_INSTALLED) {
Branch: 2 branches
BranchTrueFalseTotal
if (A && !OTEL_PK_INSTALLED) { 23 0 23
if (A && !OTEL_PK_INSTALLED) { 4 19 23
57 4 int ok = A->on_request(aTHX_ otel_on_request, (void *)A)
58 4 ▶ && A->on_response(aTHX_ otel_on_response, (void *)A);
Branch: 2 branches
BranchTrueFalseTotal
&& A->on_response(aTHX_ otel_on_response, (void *)A); 4 0 4
&& A->on_response(aTHX_ otel_on_response, (void *)A); 4 0 4
59 4 ▶ if (ok) OTEL_PK_INSTALLED = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) OTEL_PK_INSTALLED = 1; 4 0 4
60 4 (void)hv_stores(out, "server", newSViv(ok ? 1 : 0));
61 /* the table as `ud`, so the observer can ask current_of
62 * which request issued the statement (v5) */
63 4 ▶ if (A->abi_version >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (A->abi_version >= 2) 4 0 4
64 4 OTEL_PK_DB = A->on_query(aTHX_ otel_on_query,
65 otel_on_query_done, (void *)A);
66 /* THE LOGS SIGNAL, from the logger the application already
67 * uses. v4 is the version that hands an observer the context,
68 * and without the context a record cannot be correlated - so
69 * an older Punk exports nothing here rather than exporting
70 * lines with no trace id, which would look like working
71 * correlation that never joins. */
72 4 ▶ if (A->abi_version >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (A->abi_version >= 4) 4 0 4
73 4 OTEL_PK_LOGS = A->on_log_ctx(aTHX_ otel_on_log, (void *)A);
74 }
75 19 else (void)hv_stores(out, "server",
76 newSViv(OTEL_PK_INSTALLED ? 1 : 0));
77 /* Reported every time, not only on the call that registered. */
78 23 (void)hv_stores(out, "db_punk", newSViv(OTEL_PK_DB));
79 23 (void)hv_stores(out, "logs", newSViv(OTEL_PK_LOGS));
80 }
81  
82 /* ---- DBIx::Loop: the other database path ------------------------ *
83 * Three comments in this distribution said this was wired up and it
84 * never was. An application on the async model backend had no
85 * database spans at all, and nothing said why. */
86 {
87 23 const dbil_abi *D = otel_dl(aTHX);
88 23 ▶ if (D && !OTEL_DL_INSTALLED) {
Branch: 2 branches
BranchTrueFalseTotal
if (D && !OTEL_DL_INSTALLED) { 23 0 23
if (D && !OTEL_DL_INSTALLED) { 4 19 23
89 4 OTEL_DL_DB = D->on_exec(aTHX_ otel_dl_start, otel_dl_done,
90 (void *)"other_sql");
91 4 ▶ if (OTEL_DL_DB) OTEL_DL_INSTALLED = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (OTEL_DL_DB) OTEL_DL_INSTALLED = 1; 4 0 4
92 }
93 23 (void)hv_stores(out, "db_loop", newSViv(OTEL_DL_DB));
94 }
95 {
96 /* Truthful about the one metric this dist records by itself: it
97 * needs a meter handed in, not merely the signal switched on. */
98 23 ▶ (void)hv_stores(out, "metrics",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(out, "metrics", 16 7 23
(void)hv_stores(out, "metrics", 16 0 16
99 newSViv(OTEL_METER && OTEL_INSTR.metrics ? 1 : 0));
100 }
101  
102 /* ---- Fetch: client spans, and the traceparent that makes a trace
103 * cross a process boundary at all ---------------------------------- */
104 {
105 23 const fetch_abi *F = otel_ft(aTHX);
106 27 ▶ if (F && !OTEL_FT_INSTALLED) {
Branch: 2 branches
BranchTrueFalseTotal
if (F && !OTEL_FT_INSTALLED) { 23 0 23
if (F && !OTEL_FT_INSTALLED) { 4 19 23
107 4 int ok = F->on_request(aTHX_ otel_ft_start, otel_ft_done, NULL);
108 4 ▶ if (ok) OTEL_FT_INSTALLED = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) OTEL_FT_INSTALLED = 1; 4 0 4
109 4 (void)hv_stores(out, "client", newSViv(ok ? 1 : 0));
110 }
111 19 else (void)hv_stores(out, "client",
112 newSViv(OTEL_FT_INSTALLED ? 1 : 0));
113 }
114  
115 23 RETVAL = newRV_noinc((SV *)out);
116 }
117 OUTPUT:
118 RETVAL
119  
120 # Turn a point on or off after installation. The registrations stay; the
121 # callbacks return immediately. An application drowning in database spans can
122 # silence those without losing its server spans.
123 void
124 configure(...)
125 CODE:
126 {
127 int i;
128 12 ▶ for (i = 0; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < items; i += 2) { 6 6 12
129 6 const char *k = SvPV_nolen(ST(i));
130 6 int v = SvTRUE(ST(i + 1)) ? 1 : 0;
131 6 ▶ if (strEQ(k, "server")) OTEL_INSTR.server = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "server")) OTEL_INSTR.server = v; 0 6 6
132 6 ▶ else if (strEQ(k, "client")) OTEL_INSTR.client = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "client")) OTEL_INSTR.client = v; 0 6 6
133 6 ▶ else if (strEQ(k, "db")) OTEL_INSTR.db = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "db")) OTEL_INSTR.db = v; 2 4 6
134 4 ▶ else if (strEQ(k, "metrics")) OTEL_INSTR.metrics = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "metrics")) OTEL_INSTR.metrics = v; 2 2 4
135 2 ▶ else if (strEQ(k, "enabled")) OTEL_INSTR.enabled = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "enabled")) OTEL_INSTR.enabled = v; 2 0 2
136 }
137 }
138  
139 void
140 config()
141 PPCODE:
142 4 ▶ EXTEND(SP, 10);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 10); 0 4 4
143 4 mPUSHp("server", 6); mPUSHi(OTEL_INSTR.server);
144 4 mPUSHp("client", 6); mPUSHi(OTEL_INSTR.client);
145 4 mPUSHp("db", 2); mPUSHi(OTEL_INSTR.db);
146 4 mPUSHp("metrics", 7); mPUSHi(OTEL_INSTR.metrics);
147 4 mPUSHp("enabled", 7); mPUSHi(OTEL_INSTR.enabled);
148  
149 # The recursion guard, reachable from Perl.
150 #
151 # The exporter sends spans over HTTP with Fetch, and Fetch is instrumented, so
152 # an export would otherwise produce a client span, which is queued, and
153 # exported... The first collector outage becomes an infinite loop of telemetry
154 # about failing to send telemetry. Everything the SDK does on its own behalf
155 # runs inside this.
156 void
157 suppress_begin()
158 CODE:
159 27 otel_suppress_begin();
160  
161 void
162 suppress_end()
163 CODE:
164 28 otel_suppress_end();
165  
166 IV
167 suppressed()
168 CODE:
169 5 ▶ RETVAL = OTEL_INSTR.suppress;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = OTEL_INSTR.suppress; 0 5 5
170 OUTPUT:
171 RETVAL
172  
173 # ---- the semantic conventions, reachable for the tests ----------------------
174 # These are pure functions over strings and are where the cardinality rules
175 # live, so they are asserted directly rather than inferred from a span.
176  
177 # The canonical method, or "_OTHER" for anything not in the known set. The
178 # method is client-controlled and otherwise unbounded, and it lands in a
179 # metric dimension in the metrics phase.
180 SV *
181 method(m)
182 SV *m
183 CODE:
184 {
185 STRLEN l;
186 14 const char *s = SvPV_const(m, l);
187 14 const char *c = otel_sc_method(s, l);
188 14 ▶ RETVAL = newSVpv(c ? c : "_OTHER", 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVpv(c ? c : "_OTHER", 0); 9 5 14
189 }
190 OUTPUT:
191 RETVAL
192  
193 # The span status implied by an HTTP status, per span kind. A 4xx does NOT
194 # fail a SERVER span - the server worked and said no - but it DOES fail a
195 # CLIENT span, where it is a failure of the call this process made.
196 IV
197 server_status(code)
198 IV code
199 CODE:
200 7 ▶ RETVAL = otel_sc_server_status(code);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = otel_sc_server_status(code); 0 7 7
201 OUTPUT:
202 RETVAL
203  
204 IV
205 client_status(code)
206 IV code
207 CODE:
208 3 ▶ RETVAL = otel_sc_client_status(code);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = otel_sc_client_status(code); 0 3 3
209 OUTPUT:
210 RETVAL
211  
212 # db.operation.name: the leading keyword, uppercased and bounded.
213 SV *
214 db_operation(sql)
215 SV *sql
216 CODE:
217 {
218 STRLEN l;
219 8 const char *s = SvPV_const(sql, l);
220 char op[32];
221 8 ▶ if (otel_sc_db_operation(s, l, op, sizeof op)) RETVAL = newSVpv(op, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (otel_sc_db_operation(s, l, op, sizeof op)) RETVAL = newSVpv(op, 0); 6 2 8
222 2 else XSRETURN_UNDEF;
223 }
224 OUTPUT:
225 RETVAL
226  
227 SV *
228 schema_url()
229 CODE:
230 33 RETVAL = newSVpvs(OTEL_SCHEMA_URL);
231 OUTPUT:
232 RETVAL
« xs/meter.xs ↑ Summary include/otel_signal_pb.h »