Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as ConfigProvider from "effect/ConfigProvider";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as OtlpResource from "effect/observability/OtlpResource";
6
7import * as OtelEnvironment from "./otelEnvironment.ts";
8
9const load = (env: Record<string, string>) =>
10 OtelEnvironment.load.pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))));
11
12const SPEC_OFF =
13 "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway";
14const T3_OFF =
15 "T3CODE_OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it";
16const specIgnored = (value: string) =>
17 `OTEL_SDK_DISABLED=${value} was read as false; the OpenTelemetry specification recognizes only the string true, so use OTEL_SDK_DISABLED=true or T3CODE_OTEL_SDK_DISABLED to say it any other way`;
18
19describe("OtelEnvironment", () => {
20 it.effect.each([
21 { name: "nothing set", env: {}, disabled: false, warnings: [] },
22 { name: "spec true", env: { OTEL_SDK_DISABLED: "true" }, disabled: true, warnings: [SPEC_OFF] },
23 { name: "spec True", env: { OTEL_SDK_DISABLED: "True" }, disabled: true, warnings: [SPEC_OFF] },
24 {
25 name: "spec padded",
26 env: { OTEL_SDK_DISABLED: " true " },
27 disabled: true,
28 warnings: [SPEC_OFF],
29 },
30 { name: "spec false", env: { OTEL_SDK_DISABLED: "false" }, disabled: false, warnings: [] },
31 {
32 name: "spec 1",
33 env: { OTEL_SDK_DISABLED: "1" },
34 disabled: false,
35 warnings: [specIgnored("1")],
36 },
37 {
38 name: "spec padded yes",
39 env: { OTEL_SDK_DISABLED: " yes " },
40 disabled: false,
41 warnings: [specIgnored("yes")],
42 },
43 { name: "t3 1", env: { T3CODE_OTEL_SDK_DISABLED: "1" }, disabled: true, warnings: [T3_OFF] },
44 {
45 name: "t3 TRUE",
46 env: { T3CODE_OTEL_SDK_DISABLED: "TRUE" },
47 disabled: true,
48 warnings: [T3_OFF],
49 },
50 { name: "t3 n", env: { T3CODE_OTEL_SDK_DISABLED: "n" }, disabled: false, warnings: [] },
51 {
52 name: "t3 false overrides spec true",
53 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
54 disabled: false,
55 warnings: [],
56 },
57 {
58 name: "blank t3 falls through",
59 env: { T3CODE_OTEL_SDK_DISABLED: " ", OTEL_SDK_DISABLED: "true" },
60 disabled: true,
61 warnings: [SPEC_OFF],
62 },
63 {
64 name: "unreadable t3 warns and falls through",
65 env: { T3CODE_OTEL_SDK_DISABLED: "maybe", OTEL_SDK_DISABLED: "true" },
66 disabled: true,
67 warnings: ["T3CODE_OTEL_SDK_DISABLED=maybe is not a yes or a no and was ignored", SPEC_OFF],
68 },
69 {
70 name: "bad spec value still warns when t3 answered",
71 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "yes" },
72 disabled: false,
73 warnings: [specIgnored("yes")],
74 },
75 ])("$name", ({ env, disabled, warnings }) =>
76 Effect.gen(function* () {
77 const resolved = yield* load(env);
78 assert.strictEqual(resolved.disabled, disabled);
79 assert.deepStrictEqual(resolved.warnings, warnings);
80 }),
81 );
82
83 it.effect.each([
84 { name: "unset", env: {}, resourceAttributes: {}, warnings: [] },
85 {
86 name: "a percent-encoded list",
87 env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,message=hello%20world" },
88 resourceAttributes: { team: "core", message: "hello world" },
89 warnings: [],
90 },
91 {
92 name: "a list that does not decode",
93 env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,broken=%zz" },
94 resourceAttributes: {},
95 warnings: [
96 "OTEL_RESOURCE_ATTRIBUTES is not a list of percent-encoded key=value pairs and was ignored",
97 ],
98 },
99 ])("resource attributes: $name", ({ env, resourceAttributes, warnings }) =>
100 Effect.gen(function* () {
101 const resolved = yield* load(env);
102 assert.deepStrictEqual(resolved.resourceAttributes, resourceAttributes);
103 assert.deepStrictEqual(resolved.warnings, warnings);
104 }),
105 );
106
107 describe("endpoints", () => {
108 const urlOf = (signal: OtelEnvironment.OtelSignal) =>
109 OtelEnvironment.OtelSignal.$match(signal, {
110 Export: ({ url }) => url,
111 Off: () => "Off",
112 Unset: () => "Unset",
113 });
114 it.effect.each([
115 {
116 name: "nothing set",
117 env: {},
118 traces: "Unset",
119 metrics: "Unset",
120 logs: "Unset",
121 warnings: [],
122 },
123 {
124 name: "generic endpoint appends each signal's path, keeping the query",
125 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base?api_key=secret" },
126 traces: "https://collector:4318/base/v1/traces?api_key=secret",
127 metrics: "https://collector:4318/base/v1/metrics?api_key=secret",
128 logs: "https://collector:4318/base/v1/logs?api_key=secret",
129 warnings: [],
130 },
131 {
132 name: "a per-signal endpoint is used verbatim",
133 env: { OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/custom" },
134 traces: "https://tempo:4318/custom",
135 metrics: "Unset",
136 logs: "Unset",
137 warnings: [],
138 },
139 {
140 name: "a per-signal endpoint beats the generic one",
141 env: {
142 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/custom",
143 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
144 },
145 traces: "https://tempo:4318/custom",
146 metrics: "https://collector:4318/base/v1/metrics",
147 logs: "https://collector:4318/base/v1/logs",
148 warnings: [],
149 },
150 {
151 name: "a blank per-signal endpoint falls through to the generic one",
152 env: {
153 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: " ",
154 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
155 },
156 traces: "https://collector:4318/base/v1/traces",
157 metrics: "https://collector:4318/base/v1/metrics",
158 logs: "https://collector:4318/base/v1/logs",
159 warnings: [],
160 },
161 {
162 name: "an invalid per-signal endpoint warns and does not fall through",
163 env: {
164 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "not-a-url",
165 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
166 },
167 traces: "Off",
168 metrics: "https://collector:4318/base/v1/metrics",
169 logs: "https://collector:4318/base/v1/logs",
170 warnings: [
171 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
172 ],
173 },
174 {
175 name: "an invalid generic endpoint warns without leaking its query",
176 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "not-a-url?api_key=secret" },
177 traces: "Off",
178 metrics: "Off",
179 logs: "Off",
180 warnings: [
181 "OTEL_EXPORTER_OTLP_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
182 ],
183 },
184 {
185 name: "an endpoint without a scheme is not an http URL",
186 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "localhost:4318" },
187 traces: "Off",
188 metrics: "Off",
189 logs: "Off",
190 warnings: [
191 "OTEL_EXPORTER_OTLP_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
192 ],
193 },
194 {
195 name: "the kill switch wins outright over a valid endpoint",
196 env: {
197 T3CODE_OTEL_SDK_DISABLED: "true",
198 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
199 },
200 traces: "Unset",
201 metrics: "Unset",
202 logs: "Unset",
203 warnings: [T3_OFF],
204 },
205 {
206 name: "an exporter of none turns off only its signal",
207 env: {
208 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318",
209 OTEL_LOGS_EXPORTER: "none",
210 },
211 traces: "https://collector:4318/v1/traces",
212 metrics: "https://collector:4318/v1/metrics",
213 logs: "Off",
214 warnings: [],
215 },
216 {
217 name: "an exporter of none turns its signal off with no endpoint named",
218 env: { OTEL_METRICS_EXPORTER: " NONE " },
219 traces: "Unset",
220 metrics: "Off",
221 logs: "Unset",
222 warnings: [],
223 },
224 {
225 name: "an exporter of none leaves its signal's endpoint unread",
226 env: { OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "not-a-url", OTEL_TRACES_EXPORTER: "none" },
227 traces: "Off",
228 metrics: "Unset",
229 logs: "Unset",
230 warnings: [],
231 },
232 {
233 name: "an exporter of otlp or blank is the default",
234 env: {
235 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318",
236 OTEL_TRACES_EXPORTER: "OTLP",
237 OTEL_LOGS_EXPORTER: " ",
238 },
239 traces: "https://collector:4318/v1/traces",
240 metrics: "https://collector:4318/v1/metrics",
241 logs: "https://collector:4318/v1/logs",
242 warnings: [],
243 },
244 {
245 name: "an exporter T3 Code does not have is ignored with a warning",
246 env: {
247 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318",
248 OTEL_METRICS_EXPORTER: "prometheus",
249 },
250 traces: "https://collector:4318/v1/traces",
251 metrics: "https://collector:4318/v1/metrics",
252 logs: "https://collector:4318/v1/logs",
253 warnings: [
254 "OTEL_METRICS_EXPORTER names prometheus, which T3 Code does not export to, so it was ignored",
255 ],
256 },
257 {
258 name: "a list honors none and otlp and names what it ignored",
259 env: {
260 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318",
261 OTEL_TRACES_EXPORTER: "console,none",
262 OTEL_LOGS_EXPORTER: "otlp,console,otlpp",
263 },
264 traces: "Off",
265 metrics: "https://collector:4318/v1/metrics",
266 logs: "https://collector:4318/v1/logs",
267 warnings: [
268 "OTEL_TRACES_EXPORTER names console, which T3 Code does not export to, so it was ignored",
269 "OTEL_LOGS_EXPORTER names console, otlpp, which T3 Code does not export to, so they were ignored",
270 ],
271 },
272 ])("$name", ({ env, traces, metrics, logs, warnings }) =>
273 Effect.gen(function* () {
274 const resolved = yield* load(env);
275 assert.strictEqual(urlOf(resolved.traces), traces);
276 assert.strictEqual(urlOf(resolved.metrics), metrics);
277 assert.strictEqual(urlOf(resolved.logs), logs);
278 assert.deepStrictEqual(resolved.warnings, warnings);
279 }),
280 );
281
282 const ENDPOINT = { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318" };
283 const exportOf = (signal: OtelEnvironment.OtelSignal): unknown =>
284 OtelEnvironment.OtelSignal.$match(signal, {
285 Export: ({ protocol, headers }) => ({ protocol, headers }),
286 Off: () => "Off",
287 Unset: () => "Unset",
288 });
289 it.effect.each([
290 {
291 name: "nothing else set takes the specification's default protocol",
292 env: ENDPOINT,
293 traces: { protocol: "http/protobuf", headers: undefined },
294 logs: { protocol: "http/protobuf", headers: undefined },
295 warnings: [],
296 },
297 {
298 name: "headers are comma-separated pairs with percent-encoded values",
299 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS: "api-key=a%20b,tenant=t3" },
300 traces: { protocol: "http/protobuf", headers: { "api-key": "a b", tenant: "t3" } },
301 logs: { protocol: "http/protobuf", headers: { "api-key": "a b", tenant: "t3" } },
302 warnings: [],
303 },
304 {
305 name: "a per-signal protocol and headers beat the generic ones",
306 env: {
307 ...ENDPOINT,
308 OTEL_EXPORTER_OTLP_PROTOCOL: "http/json",
309 OTEL_EXPORTER_OTLP_HEADERS: "api-key=shared",
310 OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: "http/protobuf",
311 OTEL_EXPORTER_OTLP_TRACES_HEADERS: "api-key=traces%20only",
312 },
313 traces: { protocol: "http/protobuf", headers: { "api-key": "traces only" } },
314 logs: { protocol: "http/json", headers: { "api-key": "shared" } },
315 warnings: [],
316 },
317 {
318 name: "an unsupported protocol turns off the signals it configures",
319 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: "grpc" },
320 traces: { protocol: "http/protobuf", headers: undefined },
321 logs: "Off",
322 warnings: [
323 "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL is not http/protobuf or http/json, so the signals it configures are not exported",
324 ],
325 },
326 {
327 name: "a protocol reads case-insensitively",
328 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL: "HTTP/JSON" },
329 traces: { protocol: "http/json", headers: undefined },
330 logs: { protocol: "http/json", headers: undefined },
331 warnings: [],
332 },
333 {
334 name: "undecodable headers turn off every signal once, without leaking them",
335 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz" },
336 traces: "Off",
337 logs: "Off",
338 warnings: [
339 "OTEL_EXPORTER_OTLP_HEADERS is not a list of key=value pairs with percent-encoded values, so the signals it configures are not exported",
340 ],
341 },
342 {
343 name: "a header without a value separator turns its signal off",
344 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_HEADERS: "Authorization" },
345 traces: { protocol: "http/protobuf", headers: undefined },
346 logs: "Off",
347 warnings: [
348 "OTEL_EXPORTER_OTLP_LOGS_HEADERS is not a list of key=value pairs with percent-encoded values, so the signals it configures are not exported",
349 ],
350 },
351 {
352 name: "blank per-signal headers leave the generic ones in charge",
353 env: {
354 ...ENDPOINT,
355 OTEL_EXPORTER_OTLP_HEADERS: "api-key=shared",
356 OTEL_EXPORTER_OTLP_LOGS_HEADERS: " ",
357 },
358 traces: { protocol: "http/protobuf", headers: { "api-key": "shared" } },
359 logs: { protocol: "http/protobuf", headers: { "api-key": "shared" } },
360 warnings: [],
361 },
362 {
363 name: "generic headers every signal overrides say nothing",
364 env: {
365 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/v1/traces",
366 OTEL_EXPORTER_OTLP_TRACES_HEADERS: "api-key=traces",
367 OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz",
368 },
369 traces: { protocol: "http/protobuf", headers: { "api-key": "traces" } },
370 logs: "Unset",
371 warnings: [],
372 },
373 {
374 name: "protocol and headers say nothing for a signal no endpoint names",
375 env: { OTEL_EXPORTER_OTLP_PROTOCOL: "grpc", OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz" },
376 traces: "Unset",
377 logs: "Unset",
378 warnings: [],
379 },
380 ])("$name", ({ env, traces, logs, warnings }) =>
381 Effect.gen(function* () {
382 const resolved = yield* load(env);
383 assert.deepStrictEqual(exportOf(resolved.traces), traces);
384 assert.deepStrictEqual(exportOf(resolved.logs), logs);
385 assert.deepStrictEqual(resolved.warnings, warnings);
386 }),
387 );
388 });
389
390 describe("resolveSignalEndpoint", () => {
391 const t3Export = {
392 protocol: "http/json",
393 headers: { "x-key": "t3" },
394 exportIntervalMs: 5_000,
395 } as const;
396 const withLogs = (logs: OtelEnvironment.OtelSignal, disabled = false) => ({
397 ...OtelEnvironment.none,
398 disabled,
399 logs,
400 });
401 const otelExport = OtelEnvironment.OtelSignal.Export({
402 url: "http://otel:4318/v1/logs",
403 protocol: "http/protobuf",
404 headers: { "x-key": "otel" },
405 });
406 it.each([
407 {
408 name: "T3CODE_OTLP_*_URL wins over an OTEL endpoint",
409 otel: withLogs(otelExport),
410 t3Url: "http://t3:4318/v1/logs",
411 expected: { url: "http://t3:4318/v1/logs", export: t3Export },
412 },
413 {
414 name: "T3CODE_OTLP_*_URL wins over a signal the OTEL variables turned off",
415 otel: withLogs(OtelEnvironment.OtelSignal.Off()),
416 t3Url: "http://t3:4318/v1/logs",
417 expected: { url: "http://t3:4318/v1/logs", export: t3Export },
418 },
419 {
420 name: "an OTEL endpoint brings its headers and protocol over the fallback",
421 otel: withLogs(otelExport),
422 t3Url: " ",
423 expected: {
424 url: "http://otel:4318/v1/logs",
425 export: {
426 protocol: "http/protobuf" as const,
427 headers: { "x-key": "otel" },
428 exportIntervalMs: 5_000,
429 },
430 },
431 },
432 {
433 name: "a signal the OTEL variables turned off does not fall through",
434 otel: withLogs(OtelEnvironment.OtelSignal.Off()),
435 t3Url: undefined,
436 expected: undefined,
437 },
438 {
439 name: "an unset signal takes the first non-blank fallback",
440 otel: withLogs(OtelEnvironment.OtelSignal.Unset()),
441 t3Url: undefined,
442 expected: { url: "http://settings:4318/v1/logs", export: t3Export },
443 },
444 {
445 name: "the kill switch wins over everything",
446 otel: withLogs(otelExport, true),
447 t3Url: "http://t3:4318/v1/logs",
448 expected: undefined,
449 },
450 ])("$name", ({ otel, t3Url, expected }) => {
451 assert.deepStrictEqual(
452 OtelEnvironment.resolveSignalEndpoint(
453 otel,
454 "logs",
455 { url: t3Url, export: t3Export },
456 "",
457 "http://settings:4318/v1/logs",
458 ),
459 expected,
460 );
461 });
462 });
463
464 it.effect("an exporter of none keeps the Settings endpoint from re-enabling its signal", () =>
465 Effect.gen(function* () {
466 const otel = yield* load({ OTEL_LOGS_EXPORTER: "none" });
467 const t3 = {
468 url: undefined,
469 export: { protocol: "http/json", headers: undefined, exportIntervalMs: 10_000 },
470 } as const;
471 assert.strictEqual(
472 OtelEnvironment.resolveSignalEndpoint(otel, "logs", t3, "http://settings:4318/v1/logs"),
473 undefined,
474 );
475 assert.strictEqual(
476 OtelEnvironment.resolveSignalEndpoint(otel, "traces", t3, "http://settings:4318/v1/traces")
477 ?.url,
478 "http://settings:4318/v1/traces",
479 );
480 }),
481 );
482
483 describe("layerResourceAttributes", () => {
484 it.effect.each([
485 { name: "a list that does not decode", raw: "team=%zz", attributes: [] },
486 { name: "encoded separators", raw: "a%2Cb=x%3Dy", attributes: ["a,b"] },
487 ])("lets the exporters' own read succeed with $name", ({ raw, attributes }) =>
488 Effect.gen(function* () {
489 const layerEnv = ConfigProvider.layer(
490 ConfigProvider.fromEnv({ env: { OTEL_RESOURCE_ATTRIBUTES: raw } }),
491 );
492 const otel = yield* OtelEnvironment.load.pipe(Effect.provide(layerEnv));
493 const resource = yield* OtlpResource.fromConfig({ serviceName: "t3" }).pipe(
494 Effect.provide(
495 Layer.provide(
496 OtelEnvironment.layerResourceAttributes(otel.resourceAttributes),
497 layerEnv,
498 ),
499 ),
500 );
501 assert.deepStrictEqual(
502 resource.attributes
503 .map((attribute) => attribute.key)
504 .filter((key) => !key.startsWith("telemetry.sdk.")),
505 [...attributes, "service.name"],
506 );
507 }),
508 );
509 });
510});
511
512describe("isRelayTracingEnabled", () => {
513 it("defaults on and respects the T3CODE SDK override of the standard kill switch", () => {
514 assert.isTrue(OtelEnvironment.isRelayTracingEnabled({}));
515 assert.isFalse(OtelEnvironment.isRelayTracingEnabled({ OTEL_SDK_DISABLED: "true" }));
516 assert.isTrue(
517 OtelEnvironment.isRelayTracingEnabled({
518 OTEL_SDK_DISABLED: "true",
519 T3CODE_OTEL_SDK_DISABLED: "false",
520 }),
521 );
522 assert.isFalse(
523 OtelEnvironment.isRelayTracingEnabled({
524 OTEL_SDK_DISABLED: "false",
525 T3CODE_OTEL_SDK_DISABLED: "true",
526 }),
527 );
528 });
529
530 it.effect("product opt-out beats SDK enable without disabling user-configured tracing", () =>
531 Effect.gen(function* () {
532 const env = {
533 T3CODE_TELEMETRY_ENABLED: "false",
534 T3CODE_OTEL_SDK_DISABLED: "false",
535 OTEL_SDK_DISABLED: "true",
536 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://user.example.test/v1/traces",
537 };
538 assert.isFalse(OtelEnvironment.isRelayTracingEnabled(env));
539
540 const otel = yield* load(env);
541 assert.isFalse(otel.disabled);
542 assert.deepStrictEqual(
543 otel.traces,
544 OtelEnvironment.OtelSignal.Export({
545 url: env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
546 protocol: "http/protobuf",
547 headers: undefined,
548 }),
549 );
550 }),
551 );
552});