OpenTelemetry.xs

filter: Statementstmt 94.2% 33 / 35 Branchbran 59.6% 31 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.5% 64 / 87 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_trace.h ↑ Summary include/otel_instr.h »
1 /*
2 * OpenTelemetry.xs - root XS file.
3 *
4 * Thin wrapper: includes the C implementation headers, then pulls in the
5 * per-module XS fragments from xs/ via INCLUDE: (the Punk / Open::API
6 * layout). All the behaviour lives in include/; the .pm files are
7 * documentation.
8 */
9  
10 #define PERL_NO_GET_CONTEXT
11 #include "EXTERN.h"
12 #include "perl.h"
13 #include "XSUB.h"
14  
15 /* Build with -DOTEL_PB_ASSERT to have every embedded message check that what
16 * it wrote matches what it measured. Off in a normal build; on under the
17 * author tests, where the cost does not matter and a drifted size_/write_
18 * pair should be loud. */
19  
20 #include "frj_abi.h"
21  
22 /* ---- the File::Raw::JSON C ABI ------------------------------------------ *
23 * Only the OTLP/JSON encoder needs it, so it is resolved lazily on first use
24 * rather than at boot: a process exporting protobuf, which is the default,
25 * never loads File::Raw::JSON at all. Same consumer pattern Punk uses. */
26 static const frj_abi *OTEL_FRJ = NULL;
27 static int OTEL_FRJ_TRIED = 0;
28  
29 10 static const frj_abi *otel_frj(pTHX) {
30 10 ▶ if (!OTEL_FRJ && !OTEL_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!OTEL_FRJ && !OTEL_FRJ_TRIED) { 2 8 10
if (!OTEL_FRJ && !OTEL_FRJ_TRIED) { 2 0 2
31 2 dSP; int count; IV p = 0;
32 2 OTEL_FRJ_TRIED = 1;
33 2 eval_pv("require File::Raw::JSON;", FALSE);
34 2 SPAGAIN; /* the require runs arbitrary Perl; the stack may have moved */
35 2 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 2 0 2
if (!SvTRUE(ERRSV)) { 2 0 2
36 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 2 2
37 2 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
38 2 SPAGAIN;
39 2 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
40 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
41 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
42 2 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 2 0 2
43 2 const frj_abi *a = INT2PTR(const frj_abi *, p);
44 2 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) OTEL_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) OTEL_FRJ = a; 2 0 2
if (a && a->abi_version >= FRJ_ABI_VERSION) OTEL_FRJ = a; 2 0 2
45 }
46 }
47 }
48 10 ▶ if (!OTEL_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!OTEL_FRJ) 0 10 10
49 0 croak("Punk::OpenTelemetry: OTLP/JSON needs File::Raw::JSON with a "
50 "compatible C ABI (FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
51 10 return OTEL_FRJ;
52 }
53  
54 #include "otel_pb.h" /* varints, tags, length-delimited fields */
55 #include "otel_proto.h" /* the OTLP field numbers, pinned */
56 #include "otel_value.h" /* AnyValue / KeyValue from Perl scalars */
57 #include "otel_trace.h" /* the trace message tree */
58 #include "otel_json.h" /* ... and the same tree as OTLP/JSON */
59 #include "otel_export.h" /* ... and the OTLP/HTTP transport */
60  
61 /* the trace SDK (phase 3): ids, clocks, sampling, spans, the batch queue */
62 #include "otel_id.h" /* trace and span ids; never all-zero */
63 #include "otel_clock.h" /* wall once, monotonic for the duration */
64 #include "otel_sample.h" /* ParentBased(TraceIdRatioBased) */
65 #include "otel_span.h" /* the span struct, limits and drop counts */
66 #include "otel_resource.h" /* what produced this telemetry (needs the ids) */
67 #include "otel_tracer.h" /* the tracer and its bounded batch queue */
68  
69 /* context propagation (phase 4) */
70 #include "otel_ctx.h" /* the extracted context; invalid means ABSENT */
71 #include "otel_w3c.h" /* traceparent + tracestate */
72 #include "otel_b3.h" /* B3, single and multi */
73 #include "otel_jaeger.h" /* uber-trace-id */
74 #include "otel_baggage.h" /* W3C Baggage */
75  
76 /* metrics (phase 6). BEFORE the instrumentation, which records the HTTP
77 * duration histogram itself and so needs the meter's types. These need only
78 * the clock and the span, both above. */
79 #include "otel_expo.h" /* the base-2 exponential histogram */
80 #include "otel_metric.h" /* instruments, points, the cardinality cap */
81 #include "otel_meter.h" /* views, conflict detection, collection */
82  
83 /* instrumentation (phase 5): the hooks become spans, and one metric */
84 #include "otel_semconv.h" /* attribute names, pinned; method bounding */
85 #include "otel_instr.h" /* the server and database observers */
86 #include "otel_consume.h" /* pk_abi + fetch_abi, both optional */
87  
88 /* logs (phase 7), and the encoders for both other signals */
89 #include "otel_log.h" /* severity, the record queue, the tap */
90 #include "otel_signal_pb.h" /* metrics + logs, in protobuf */
91 #include "otel_grpc.h" /* OTLP over gRPC: framing and status */
92  
93 /* schema urls */
94 #include "otel_schema.h" /* converting between convention versions */
95  
96 /* configuration */
97 #include "otel_config.h" /* the OTEL_* surface, precedence, the boot line */
98  
99  
100 /* ---- the log tap -------------------------------------------------------- *
101 * Glue between two subsystems, so it lives in the file that includes both:
102 * the observer is typed by pk_abi (otel_instr.h) and emits into the logger
103 * (otel_log.h), and the logger header is included after the instrumentation
104 * one. */
105 /* The logger this process exports through. Set by the plugin when the logs
106 * signal is on, and NULL when it is off - which is what makes a log line cost
107 * a load and a branch on a deployment that has turned logs off. */
108 static otel_logger *OTEL_LOGGER = NULL;
109  
110 /* pk_abi's on_log_ctx: EVERY LINE THE APPLICATION LOGS, exported.
111 *
112 * This is why there is no separate "log to telemetry" call. An application
113 * already has a logger and already uses it; asking it to make a second call
114 * to a second logger means the two disagree the first time somebody adds a
115 * line to one of them, and the telemetry copy is always the one that gets
116 * forgotten. `$c->log->error(...)` is the whole interface.
117 *
118 * A TAP, NOT A SINK. The line still goes wherever it was going - Punk emits
119 * it and then hands over a copy - so turning telemetry on never takes an
120 * operator's logs away, and a collector outage never silences them.
121 *
122 * The span comes from the context, which is what v4 of the ABI exists to
123 * pass. A record logged outside a request, or after the response ended,
124 * carries no trace id rather than a stale one. */
125 22 static void otel_on_log(pTHX_ SV *c, const char *level, STRLEN llen, SV *msg,
126 HV *fields, void *ud) {
127 22 const pk_abi *A = (const pk_abi *)ud;
128 22 otel_span *span = NULL;
129 SV *lvl;
130  
131 /* The SDK's own diagnostics must not come back round through here: the
132 * exporter logs a failure, the failure becomes a record, the record is
133 * exported... */
134 22 ▶ if (!OTEL_LOGGER || OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!OTEL_LOGGER || OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 21 1 22
if (!OTEL_LOGGER || OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 6 15 21
if (!OTEL_LOGGER || OTEL_INSTR.suppress || !OTEL_INSTR.enabled) return; 0 6 6
135  
136 /* READ, NEVER TAKEN. otel_unstash_span CLEARS the slot - it is how the
137 * response side ends the span - so using it here would end the request's
138 * span by logging a line, and the trace would lose its root. */
139 6 span = otel_peek_span(aTHX_ A, c);
140  
141 6 lvl = sv_2mortal(newSVpvn(level, llen));
142  
143 /* `message` IS THE BODY, so it does not also become an attribute.
144 *
145 * Punk's structured form puts the message inside the record - and the
146 * record is what an observer is handed - so passing the hash through
147 * unchanged exported every line twice: once as the body and once as an
148 * attribute of the same name. That is bytes on every record, for ever,
149 * saying the thing the record already says.
150 *
151 * A COPY, because the hash belongs to the caller. Filtering in place
152 * would delete a key out of a live application's data structure. */
153 7 ▶ if (fields && HvUSEDKEYS(fields)) {
Branch: 3 branches
BranchTrueFalseTotal
if (fields && HvUSEDKEYS(fields)) { 1 5 6
if (fields && HvUSEDKEYS(fields)) { 0 1 1
if (fields && HvUSEDKEYS(fields)) { 1 0 1
154 1 HV *attrs = newHV();
155 HE *he;
156 1 hv_iterinit(fields);
157 3 ▶ while ((he = hv_iternext(fields))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(fields))) { 2 1 3
158 STRLEN kl;
159 2 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 2 2
160 2 ▶ if (kl == 7 && memcmp(k, "message", 7) == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 7 && memcmp(k, "message", 7) == 0) continue; 1 1 2
if (kl == 7 && memcmp(k, "message", 7) == 0) continue; 1 0 1
161 1 ▶ (void)hv_store_ent(attrs, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(attrs, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 1 0 1
(void)hv_store_ent(attrs, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 0 1 1
162 }
163 1 otel_logger_emit(aTHX_ OTEL_LOGGER, lvl, msg, attrs, span);
164 1 SvREFCNT_dec((SV *)attrs); /* emit took its own reference */
165 }
166 5 else otel_logger_emit(aTHX_ OTEL_LOGGER, lvl, msg, NULL, span);
167 }
168  
169 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry
170  
171 PROTOTYPES: DISABLE
172  
173 INCLUDE: xs/encode.xs
174 INCLUDE: xs/resource.xs
175 INCLUDE: xs/exporter.xs
176 INCLUDE: xs/tracer.xs
177 INCLUDE: xs/propagate.xs
178 INCLUDE: xs/instrument.xs
179 INCLUDE: xs/meter.xs
180 INCLUDE: xs/logger.xs
181 INCLUDE: xs/grpc.xs
182 INCLUDE: xs/schema.xs
183 INCLUDE: xs/config.xs
« include/otel_trace.h ↑ Summary include/otel_instr.h »