include/otel_config.h

filter: Statementstmt 98.9% 181 / 183 Branchbran 76.6% 138 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.8% 319 / 363 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_metric.h ↑ Summary xs/config.xs »
1 /* otel_config.h - the OTEL_* environment surface, and precedence.
2 *
3 * WHY THIS IS A WHOLE FILE.
4 *
5 * Nobody configures a telemetry SDK by calling its constructor. They set
6 * environment variables, because that is what their deployment tool sets, and
7 * because the OTEL_* names are the one part of OpenTelemetry that is the same
8 * in every language. An SDK that reads only its own options is an SDK that
9 * every operator has to learn separately, and the whole point of the spec's
10 * environment surface is that they do not have to.
11 *
12 * So: roughly thirty variables, parsed here, once, into a plain hash - and
13 * then merged under whatever the application declared.
14 *
15 * THE PRECEDENCE, and what it is based on.
16 *
17 * keyword > punk.yml > environment > default
18 *
19 * The spec defines three configuration interfaces (programmatic, environment
20 * variable, declarative file) and says programmatic configuration is the
21 * foundation the others "SHOULD be built on top of". It states NO precedence
22 * between programmatic and environment configuration. The one precedence rule
23 * it does give is about the declarative config file: when
24 * OTEL_EXPERIMENTAL_CONFIG_FILE is set, that file takes precedence over all
25 * the SDK configuration environment variables.
26 *
27 * So the order above matches the spec where it speaks (a file beats the
28 * environment) and follows its stated principle where it does not
29 * (programmatic is the foundation). It is also Punk's own convention, which
30 * layers punk.yml under what the app class declared.
31 *
32 * Note for anyone reading the POD: punk.yml's `otel:` block is NOT the spec's
33 * declarative configuration format, and the two must not be conflated.
34 *
35 * TWO THINGS THAT MUST NOT GO WRONG.
36 *
37 * - OTEL_SDK_DISABLED=true has to make the whole thing inert. It is the
38 * switch an operator reaches for at three in the morning, and it is the
39 * one piece of this dist that must not itself fail. It is checked before
40 * anything is built and before a single hook is registered.
41 *
42 * - OTEL_EXPORTER_OTLP_HEADERS carries credentials. Every header value is
43 * marked secret here so that the diagnostic, which prints everything
44 * else, cannot print those. A telemetry layer that leaks the token it
45 * authenticates with has done more harm than the telemetry was worth.
46 *
47 * Needs otel_trace.h (otel_h / otel_h_hv / otel_hv_of) and otel_resource.h
48 * (otel_res_parse_env_attrs).
49 */
50  
51 #ifndef OTEL_CONFIG_H
52 #define OTEL_CONFIG_H
53  
54 /* An environment variable, or NULL. An EMPTY value is absent, not empty:
55 * "OTEL_SERVICE_NAME=" in a compose file means the operator did not set it,
56 * and honouring it as a service named "" produces telemetry nothing can find.
57 * Mortal SV. */
58 1677 static SV *otel_cfg_env(pTHX_ const char *name) {
59 1677 const char *v = PerlEnv_getenv(name);
60 1677 ▶ if (!v || !*v) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !*v) return NULL; 38 1639 1677
if (!v || !*v) return NULL; 1 37 38
61 37 return sv_2mortal(newSVpv(v, 0));
62 }
63  
64 /* The spec's boolean: "true" is true, case-insensitively, and everything else
65 * is false. Deliberately not Perl truth - OTEL_SDK_DISABLED=false must not
66 * disable the SDK, and it would under any looser rule. */
67 39 static int otel_cfg_bool(pTHX_ const char *name, int dflt) {
68 39 SV *v = otel_cfg_env(aTHX_ name);
69 STRLEN l;
70 const char *p;
71 39 ▶ if (!v) return dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return dflt; 26 13 39
72 13 p = SvPV_const(v, l);
73 13 ▶ while (l && isSPACE(*p)) { p++; l--; }
Branch: 2 branches
BranchTrueFalseTotal
while (l && isSPACE(*p)) { p++; l--; } 13 0 13
while (l && isSPACE(*p)) { p++; l--; } 0 13 13
74 13 ▶ while (l && isSPACE(p[l - 1])) l--;
Branch: 2 branches
BranchTrueFalseTotal
while (l && isSPACE(p[l - 1])) l--; 13 0 13
while (l && isSPACE(p[l - 1])) l--; 0 13 13
75 5 ▶ return (l == 4 && (p[0] == 't' || p[0] == 'T') && (p[1] == 'r' || p[1] == 'R')
Branch: 4 branches
BranchTrueFalseTotal
return (l == 4 && (p[0] == 't' || p[0] == 'T') && (p[1] == 'r' || p[1] == 'R') 2 3 5
return (l == 4 && (p[0] == 't' || p[0] == 'T') && (p[1] == 'r' || p[1] == 'R') 2 0 2
return (l == 4 && (p[0] == 't' || p[0] == 'T') && (p[1] == 'r' || p[1] == 'R') 1 4 5
return (l == 4 && (p[0] == 't' || p[0] == 'T') && (p[1] == 'r' || p[1] == 'R') 1 0 1
76 18 ▶ && (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E'));
Branch: 5 branches
BranchTrueFalseTotal
&& (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E')); 5 8 13
&& (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E')); 1 4 5
&& (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E')); 1 0 1
&& (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E')); 1 4 5
&& (p[2] == 'u' || p[2] == 'U') && (p[3] == 'e' || p[3] == 'E')); 1 0 1
77 }
78  
79 /* An integer variable. A value that is not a number is IGNORED rather than
80 * taken as zero: OTEL_BSP_MAX_QUEUE_SIZE=lots must not silently become a
81 * queue of nothing, which is indistinguishable from the exporter being
82 * broken. */
83 663 static void otel_cfg_iv(pTHX_ HV *out, const char *key, const char *name,
84 IV dflt) {
85 663 SV *v = otel_cfg_env(aTHX_ name);
86 663 IV iv = dflt;
87 663 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 5 658 663
88 STRLEN l;
89 5 const char *p = SvPV_const(v, l);
90 5 STRLEN i = 0;
91 5 int ok = 0;
92 5 IV acc = 0;
93 5 ▶ while (i < l && isSPACE(p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l && isSPACE(p[i])) i++; 5 0 5
while (i < l && isSPACE(p[i])) i++; 0 5 5
94 19 ▶ for (; i < l && isDIGIT(p[i]); i++) { acc = acc * 10 + (p[i] - '0'); ok = 1; }
Branch: 2 branches
BranchTrueFalseTotal
for (; i < l && isDIGIT(p[i]); i++) { acc = acc * 10 + (p[i] - '0'); ok = 1; } 16 3 19
for (; i < l && isDIGIT(p[i]); i++) { acc = acc * 10 + (p[i] - '0'); ok = 1; } 14 2 16
95 5 ▶ while (i < l && isSPACE(p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l && isSPACE(p[i])) i++; 2 3 5
while (i < l && isSPACE(p[i])) i++; 0 2 2
96 5 ▶ if (ok && i == l) iv = acc;
Branch: 2 branches
BranchTrueFalseTotal
if (ok && i == l) iv = acc; 4 1 5
if (ok && i == l) iv = acc; 3 1 4
97 }
98 663 (void)hv_store(out, key, (I32)strlen(key), newSViv(iv), 0);
99 663 }
100  
101 /* A comma-separated list, whitespace around each item eaten, empty items
102 * dropped. Stored as an arrayref. */
103 39 static void otel_cfg_list(pTHX_ HV *out, const char *key, const char *name,
104 const char *dflt) {
105 39 SV *v = otel_cfg_env(aTHX_ name);
106 39 AV *av = newAV();
107 39 STRLEN l, i = 0;
108 const char *p;
109 41 ▶ if (v) p = SvPV_const(v, l);
Branch: 1 branch
BranchTrueFalseTotal
if (v) p = SvPV_const(v, l); 2 37 39
110 37 ▶ else if (dflt) { p = dflt; l = strlen(dflt); }
Branch: 1 branch
BranchTrueFalseTotal
else if (dflt) { p = dflt; l = strlen(dflt); } 37 0 37
111 0 else { (void)hv_store(out, key, (I32)strlen(key), newRV_noinc((SV *)av), 0);
112 0 return; }
113  
114 120 ▶ while (i < l) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < l) { 81 39 120
115 STRLEN start, end;
116 83 ▶ while (i < l && isSPACE(p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l && isSPACE(p[i])) i++; 83 0 83
while (i < l && isSPACE(p[i])) i++; 2 81 83
117 81 start = i;
118 828 ▶ while (i < l && p[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l && p[i] != ',') i++; 789 39 828
while (i < l && p[i] != ',') i++; 747 42 789
119 81 end = i;
120 81 ▶ if (i < l) i++;
Branch: 1 branch
BranchTrueFalseTotal
if (i < l) i++; 42 39 81
121 84 ▶ while (end > start && isSPACE(p[end - 1])) end--;
Branch: 2 branches
BranchTrueFalseTotal
while (end > start && isSPACE(p[end - 1])) end--; 83 1 84
while (end > start && isSPACE(p[end - 1])) end--; 3 80 83
122 81 ▶ if (end > start) av_push(av, newSVpvn(p + start, end - start));
Branch: 1 branch
BranchTrueFalseTotal
if (end > start) av_push(av, newSVpvn(p + start, end - start)); 80 1 81
123 }
124 39 (void)hv_store(out, key, (I32)strlen(key), newRV_noinc((SV *)av), 0);
125 }
126  
127 /* A plain string variable, absent when unset (no key at all, so a later merge
128 * can tell "unset" from "set to the default"). */
129 741 static void otel_cfg_str(pTHX_ HV *out, const char *key, const char *name,
130 const char *dflt) {
131 741 SV *v = otel_cfg_env(aTHX_ name);
132 741 ▶ if (v) (void)hv_store(out, key, (I32)strlen(key), newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_store(out, key, (I32)strlen(key), newSVsv(v), 0); 13 728 741
133 728 ▶ else if (dflt)
Branch: 1 branch
BranchTrueFalseTotal
else if (dflt) 193 535 728
134 193 (void)hv_store(out, key, (I32)strlen(key), newSVpv(dflt, 0), 0);
135 741 }
136  
137 /* Percent-decoding, for header values. The spec carries OTLP headers in the
138 * W3C Baggage encoding, so a value containing a comma, an equals or a space
139 * arrives percent-encoded, and a token that survives the split but not the
140 * decode authenticates against nothing. An invalid escape is left ALONE
141 * rather than dropped: a literal '%' in a password is likelier than a
142 * deliberate truncation. */
143 5 static SV *otel_cfg_pct_decode(pTHX_ SV *in) {
144 STRLEN l, i;
145 5 const char *p = SvPV_const(in, l);
146 SV *out;
147 16 ▶ for (i = 0; i < l; i++) if (p[i] == '%') break;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l; i++) if (p[i] == '%') break; 1 11 12
for (i = 0; i < l; i++) if (p[i] == '%') break; 12 4 16
148 5 ▶ if (i == l) return in; /* nothing to do */
Branch: 1 branch
BranchTrueFalseTotal
if (i == l) return in; /* nothing to do */ 4 1 5
149 1 out = sv_2mortal(newSVpvn("", 0));
150 1 ▶ SvGROW(out, l + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, l + 1); 1 0 1
SvGROW(out, l + 1); 1 0 1
151 14 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 13 1 14
152 13 ▶ if (p[i] == '%' && i + 2 < l && isXDIGIT(p[i + 1]) && isXDIGIT(p[i + 2])) {
Branch: 4 branches
BranchTrueFalseTotal
if (p[i] == '%' && i + 2 < l && isXDIGIT(p[i + 1]) && isXDIGIT(p[i + 2])) { 2 11 13
if (p[i] == '%' && i + 2 < l && isXDIGIT(p[i + 1]) && isXDIGIT(p[i + 2])) { 2 0 2
if (p[i] == '%' && i + 2 < l && isXDIGIT(p[i + 1]) && isXDIGIT(p[i + 2])) { 2 0 2
if (p[i] == '%' && i + 2 < l && isXDIGIT(p[i + 1]) && isXDIGIT(p[i + 2])) { 2 0 2
153 char b[3];
154 2 b[0] = p[i + 1]; b[1] = p[i + 2]; b[2] = '\0';
155 2 sv_catpvf(out, "%c", (int)strtol(b, NULL, 16));
156 2 i += 2;
157 }
158 11 else sv_catpvn(out, p + i, 1);
159 }
160 1 return out;
161 }
162  
163 /* OTEL_EXPORTER_OTLP_HEADERS and its per-signal forms: key=value pairs in the
164 * same comma-separated shape as OTEL_RESOURCE_ATTRIBUTES, with the values
165 * percent-decoded.
166 *
167 * The result is stored under `key` and its NAMES are recorded in `secret_of`
168 * - see otel_config_diagnostic. Nothing else in this file needs to know which
169 * values are credentials; the one place that prints does. */
170 156 static void otel_cfg_headers(pTHX_ HV *out, const char *key, const char *name) {
171 156 SV *v = otel_cfg_env(aTHX_ name);
172 HV *h;
173 HE *he;
174 STRLEN l;
175 const char *p;
176 156 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 153 3 156
177 3 h = newHV();
178 3 p = SvPV_const(v, l);
179 3 otel_res_parse_env_attrs(aTHX_ h, p, l);
180 3 hv_iterinit(h);
181 11 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 5 3 8
182 5 SV *val = HeVAL(he);
183 5 SV *dec = otel_cfg_pct_decode(aTHX_ val);
184 5 ▶ if (dec != val) sv_setsv(val, dec);
Branch: 1 branch
BranchTrueFalseTotal
if (dec != val) sv_setsv(val, dec); 1 4 5
185 }
186 3 (void)hv_store(out, key, (I32)strlen(key), newRV_noinc((SV *)h), 0);
187 }
188  
189 /* The whole environment surface, as one hash.
190 *
191 * Read ONCE, at boot. Re-reading %ENV per request would be both slower and
192 * wrong: a worker that picked up a mid-flight change would disagree with its
193 * siblings, and telemetry that disagrees about its own configuration is worse
194 * than telemetry that is uniformly stale. */
195 39 static HV *otel_config_from_env(pTHX) {
196 39 HV *out = newHV();
197 HV *sub;
198  
199 39 (void)hv_stores(out, "disabled",
200 newSViv(otel_cfg_bool(aTHX_ "OTEL_SDK_DISABLED", 0)));
201  
202 /* identity */
203 39 otel_cfg_str(aTHX_ out, "service_name", "OTEL_SERVICE_NAME", NULL);
204 {
205 39 SV *ra = otel_cfg_env(aTHX_ "OTEL_RESOURCE_ATTRIBUTES");
206 39 ▶ if (ra) {
Branch: 1 branch
BranchTrueFalseTotal
if (ra) { 1 38 39
207 1 HV *h = newHV();
208 STRLEN l;
209 1 const char *p = SvPV_const(ra, l);
210 1 otel_res_parse_env_attrs(aTHX_ h, p, l);
211 1 (void)hv_stores(out, "resource_attributes", newRV_noinc((SV *)h));
212 }
213 }
214  
215 /* context propagation. The spec's default is tracecontext AND baggage;
216 * a deployment that drops baggage loses it silently, which is why the
217 * default is spelled out here rather than left to the propagator. */
218 39 otel_cfg_list(aTHX_ out, "propagators", "OTEL_PROPAGATORS",
219 "tracecontext,baggage");
220  
221 /* sampling */
222 39 otel_cfg_str(aTHX_ out, "sampler", "OTEL_TRACES_SAMPLER",
223 "parentbased_always_on");
224 39 otel_cfg_str(aTHX_ out, "sampler_arg", "OTEL_TRACES_SAMPLER_ARG", NULL);
225  
226 /* transport. The general endpoint has the signal path APPENDED; a
227 * per-signal endpoint is used EXACTLY as given. That asymmetry is the
228 * spec's, and it is the single most common thing to get wrong - see
229 * otel_export.h, which implements it. */
230 39 otel_cfg_str(aTHX_ out, "endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", NULL);
231 39 otel_cfg_str(aTHX_ out, "protocol", "OTEL_EXPORTER_OTLP_PROTOCOL",
232 "http/protobuf");
233 39 otel_cfg_str(aTHX_ out, "compression", "OTEL_EXPORTER_OTLP_COMPRESSION",
234 "none");
235 39 otel_cfg_iv(aTHX_ out, "timeout", "OTEL_EXPORTER_OTLP_TIMEOUT", 10000);
236 39 otel_cfg_headers(aTHX_ out, "headers", "OTEL_EXPORTER_OTLP_HEADERS");
237  
238 39 sub = newHV();
239 39 otel_cfg_str(aTHX_ sub, "traces", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", NULL);
240 39 otel_cfg_str(aTHX_ sub, "metrics", "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", NULL);
241 39 otel_cfg_str(aTHX_ sub, "logs", "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", NULL);
242 39 ▶ if (HvUSEDKEYS(sub)) (void)hv_stores(out, "endpoints", newRV_noinc((SV *)sub));
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "endpoints", newRV_noinc((SV *)sub)); 0 39 39
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "endpoints", newRV_noinc((SV *)sub)); 1 38 39
243 38 else SvREFCNT_dec((SV *)sub);
244  
245 39 sub = newHV();
246 39 otel_cfg_str(aTHX_ sub, "traces", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", NULL);
247 39 otel_cfg_str(aTHX_ sub, "metrics", "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL", NULL);
248 39 otel_cfg_str(aTHX_ sub, "logs", "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL", NULL);
249 39 ▶ if (HvUSEDKEYS(sub)) (void)hv_stores(out, "protocols", newRV_noinc((SV *)sub));
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "protocols", newRV_noinc((SV *)sub)); 0 39 39
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "protocols", newRV_noinc((SV *)sub)); 0 39 39
250 39 else SvREFCNT_dec((SV *)sub);
251  
252 39 sub = newHV();
253 39 otel_cfg_str(aTHX_ sub, "traces", "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION", NULL);
254 39 otel_cfg_str(aTHX_ sub, "metrics", "OTEL_EXPORTER_OTLP_METRICS_COMPRESSION", NULL);
255 39 otel_cfg_str(aTHX_ sub, "logs", "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION", NULL);
256 39 ▶ if (HvUSEDKEYS(sub)) (void)hv_stores(out, "compressions", newRV_noinc((SV *)sub));
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "compressions", newRV_noinc((SV *)sub)); 0 39 39
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "compressions", newRV_noinc((SV *)sub)); 0 39 39
257 39 else SvREFCNT_dec((SV *)sub);
258  
259 39 sub = newHV();
260 39 otel_cfg_headers(aTHX_ sub, "traces", "OTEL_EXPORTER_OTLP_TRACES_HEADERS");
261 39 otel_cfg_headers(aTHX_ sub, "metrics", "OTEL_EXPORTER_OTLP_METRICS_HEADERS");
262 39 otel_cfg_headers(aTHX_ sub, "logs", "OTEL_EXPORTER_OTLP_LOGS_HEADERS");
263 39 ▶ if (HvUSEDKEYS(sub)) (void)hv_stores(out, "signal_headers", newRV_noinc((SV *)sub));
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "signal_headers", newRV_noinc((SV *)sub)); 0 39 39
if (HvUSEDKEYS(sub)) (void)hv_stores(out, "signal_headers", newRV_noinc((SV *)sub)); 1 38 39
264 38 else SvREFCNT_dec((SV *)sub);
265  
266 /* the batch span processor */
267 39 sub = newHV();
268 39 otel_cfg_iv(aTHX_ sub, "schedule_delay", "OTEL_BSP_SCHEDULE_DELAY", 5000);
269 39 otel_cfg_iv(aTHX_ sub, "export_timeout", "OTEL_BSP_EXPORT_TIMEOUT", 30000);
270 39 otel_cfg_iv(aTHX_ sub, "max_queue_size", "OTEL_BSP_MAX_QUEUE_SIZE", 2048);
271 39 otel_cfg_iv(aTHX_ sub, "max_export_batch_size", "OTEL_BSP_MAX_EXPORT_BATCH_SIZE", 512);
272 39 (void)hv_stores(out, "bsp", newRV_noinc((SV *)sub));
273  
274 /* the batch log record processor. Its schedule delay defaults to 1s, not
275 * the span processor's 5s - the spec's numbers, and different on purpose:
276 * a log people are watching for is worth sending sooner than a span they
277 * will look at afterwards. */
278 39 sub = newHV();
279 39 otel_cfg_iv(aTHX_ sub, "schedule_delay", "OTEL_BLRP_SCHEDULE_DELAY", 1000);
280 39 otel_cfg_iv(aTHX_ sub, "export_timeout", "OTEL_BLRP_EXPORT_TIMEOUT", 30000);
281 39 otel_cfg_iv(aTHX_ sub, "max_queue_size", "OTEL_BLRP_MAX_QUEUE_SIZE", 2048);
282 39 otel_cfg_iv(aTHX_ sub, "max_export_batch_size", "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", 512);
283 39 (void)hv_stores(out, "blrp", newRV_noinc((SV *)sub));
284  
285 /* metrics */
286 39 otel_cfg_iv(aTHX_ out, "metric_export_interval", "OTEL_METRIC_EXPORT_INTERVAL", 60000);
287 39 otel_cfg_iv(aTHX_ out, "metric_export_timeout", "OTEL_METRIC_EXPORT_TIMEOUT", 30000);
288 39 otel_cfg_str(aTHX_ out, "temporality_preference",
289 "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE", "cumulative");
290 39 otel_cfg_str(aTHX_ out, "exemplar_filter", "OTEL_METRICS_EXEMPLAR_FILTER",
291 "trace_based");
292  
293 /* limits. 0 means unlimited for the length limits, which is why they are
294 * absent by default rather than set to something enormous. */
295 39 otel_cfg_iv(aTHX_ out, "attribute_count_limit",
296 "OTEL_ATTRIBUTE_COUNT_LIMIT", 128);
297 39 otel_cfg_str(aTHX_ out, "attribute_value_length_limit",
298 "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT", NULL);
299 39 otel_cfg_iv(aTHX_ out, "span_attribute_count_limit",
300 "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", 128);
301 39 otel_cfg_str(aTHX_ out, "span_attribute_value_length_limit",
302 "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", NULL);
303 39 otel_cfg_iv(aTHX_ out, "span_event_count_limit",
304 "OTEL_SPAN_EVENT_COUNT_LIMIT", 128);
305 39 otel_cfg_iv(aTHX_ out, "span_link_count_limit",
306 "OTEL_SPAN_LINK_COUNT_LIMIT", 128);
307 39 otel_cfg_iv(aTHX_ out, "event_attribute_count_limit",
308 "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT", 128);
309 39 otel_cfg_iv(aTHX_ out, "link_attribute_count_limit",
310 "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT", 128);
311  
312 39 return out;
313 }
314  
315 /* Merge `src` over `dst`, in place. `src` wins.
316 *
317 * ONE LEVEL DEEP for a hash value, because the sub-hashes here are namespaces
318 * rather than values: an app that sets one per-signal endpoint means "that
319 * one", not "and forget whatever the environment said about the other two".
320 * An ARRAY replaces wholesale, because a propagator list is a single decision
321 * - merging two lists gives an order nobody chose. */
322 40 static void otel_config_merge(pTHX_ HV *dst, HV *src) {
323 HE *he;
324 40 ▶ if (!dst || !src) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!dst || !src) return; 40 0 40
if (!dst || !src) return; 0 40 40
325 40 hv_iterinit(src);
326 419 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 379 40 419
327 STRLEN kl;
328 379 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 379 379
329 379 SV *sv = HeVAL(he);
330 379 SV **have = hv_fetch(dst, k, (I32)kl, 0);
331 379 ▶ if (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV 379 0 379
if (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV 62 317 379
if (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV 42 20 62
332 42 ▶ && have && *have && SvROK(*have)
Branch: 3 branches
BranchTrueFalseTotal
&& have && *have && SvROK(*have) 1 41 42
&& have && *have && SvROK(*have) 1 0 1
&& have && *have && SvROK(*have) 1 0 1
333 1 ▶ && SvTYPE(SvRV(*have)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*have)) == SVt_PVHV) { 1 0 1
334 1 otel_config_merge(aTHX_ (HV *)SvRV(*have), (HV *)SvRV(sv));
335 1 continue;
336 }
337 378 (void)hv_store(dst, k, (I32)kl, newSVsv(sv), 0);
338 }
339 }
340  
341 /* The boot diagnostic.
342 *
343 * One line, at info, stating: enabled or disabled, the service name, the
344 * protocol, the endpoint, and the sampler with its argument. Almost every
345 * OpenTelemetry support question is answered by those five facts, and almost
346 * no SDK prints them - so the first hour of every investigation is spent
347 * establishing what the SDK thought it was doing.
348 *
349 * HEADERS ARE NEVER PRINTED. Not the values, not even truncated: a token with
350 * its first eight characters shown is a token in the log. The COUNT is
351 * printed, because "did my headers arrive" is a real question and a number
352 * answers it without answering anything else. */
353 21 static SV *otel_config_diagnostic(pTHX_ HV *cfg) {
354 21 SV *out = sv_2mortal(newSVpvn("", 0));
355 SV *v;
356 HV *h;
357 21 int nh = 0;
358  
359 21 v = otel_h(aTHX_ cfg, "disabled");
360 21 ▶ if (v && SvTRUE(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvTRUE(v)) { 21 0 21
if (v && SvTRUE(v)) { 2 19 21
361 2 sv_catpvs(out, "OpenTelemetry disabled (OTEL_SDK_DISABLED)");
362 2 return out;
363 }
364 19 sv_catpvs(out, "OpenTelemetry enabled");
365  
366 19 v = otel_h(aTHX_ cfg, "service_name");
367 35 ▶ sv_catpvf(out, " service=%s",
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(out, " service=%s", 16 3 19
sv_catpvf(out, " service=%s", 16 0 16
368 (v && SvOK(v)) ? SvPV_nolen(v) : "unknown_service");
369  
370 19 v = otel_h(aTHX_ cfg, "protocol");
371 38 ▶ if (v && SvOK(v)) sv_catpvf(out, " protocol=%s", SvPV_nolen(v));
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) sv_catpvf(out, " protocol=%s", SvPV_nolen(v)); 19 0 19
if (v && SvOK(v)) sv_catpvf(out, " protocol=%s", SvPV_nolen(v)); 19 0 19
372  
373 19 v = otel_h(aTHX_ cfg, "endpoint");
374 25 ▶ sv_catpvf(out, " endpoint=%s", (v && SvOK(v)) ? SvPV_nolen(v) : "(default)");
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(out, " endpoint=%s", (v && SvOK(v)) ? SvPV_nolen(v) : "(default)"); 6 13 19
sv_catpvf(out, " endpoint=%s", (v && SvOK(v)) ? SvPV_nolen(v) : "(default)"); 6 0 6
375  
376 19 v = otel_h(aTHX_ cfg, "sampler");
377 19 ▶ if (v && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { 19 0 19
if (v && SvOK(v)) { 19 0 19
378 19 SV *arg = otel_h(aTHX_ cfg, "sampler_arg");
379 19 sv_catpvf(out, " sampler=%s", SvPV_nolen(v));
380 20 ▶ if (arg && SvOK(arg)) sv_catpvf(out, ":%s", SvPV_nolen(arg));
Branch: 2 branches
BranchTrueFalseTotal
if (arg && SvOK(arg)) sv_catpvf(out, ":%s", SvPV_nolen(arg)); 1 18 19
if (arg && SvOK(arg)) sv_catpvf(out, ":%s", SvPV_nolen(arg)); 1 0 1
381 }
382  
383 /* THE PROPAGATORS ARE NOT PRINTED, and that is deliberate.
384 *
385 * This line existed to answer "what did the SDK think it was doing", and
386 * printing a setting in it asserts the setting does something. OTEL_PROPAGATORS
387 * is parsed and stored, and the automatic instrumentation then reads and
388 * injects W3C traceparent and nothing else - so an operator reading
389 * `propagators=b3` here would be told the opposite of the truth, in the
390 * one line they are meant to be able to trust.
391 *
392 * B3, Jaeger and Baggage are implemented and reachable through
393 * Punk::OpenTelemetry::Propagate, for a caller doing it by hand. When the
394 * instrumentation honours the list, this comes back. */
395  
396 /* the count, never the contents */
397 19 h = otel_h_hv(aTHX_ cfg, "headers");
398 19 ▶ if (h) nh += (int)HvUSEDKEYS(h);
Branch: 2 branches
BranchTrueFalseTotal
if (h) nh += (int)HvUSEDKEYS(h); 2 17 19
if (h) nh += (int)HvUSEDKEYS(h); 0 2 2
399 19 h = otel_h_hv(aTHX_ cfg, "signal_headers");
400 19 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 1 18 19
401 HE *he;
402 1 hv_iterinit(h);
403 3 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 1 1 2
404 1 HV *s = otel_hv_of(aTHX_ HeVAL(he));
405 1 ▶ if (s) nh += (int)HvUSEDKEYS(s);
Branch: 2 branches
BranchTrueFalseTotal
if (s) nh += (int)HvUSEDKEYS(s); 1 0 1
if (s) nh += (int)HvUSEDKEYS(s); 0 1 1
406 }
407 }
408 19 ▶ if (nh) sv_catpvf(out, " headers=%d", nh);
Branch: 1 branch
BranchTrueFalseTotal
if (nh) sv_catpvf(out, " headers=%d", nh); 2 17 19
409  
410 19 return out;
411 }
412  
413 #endif /* OTEL_CONFIG_H */
« include/otel_metric.h ↑ Summary xs/config.xs »