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/unstable/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 // OTEL_SDK_DISABLED follows the specification: only `true`, case-insensitively.
23 { name: "spec true", env: { OTEL_SDK_DISABLED: "true" }, disabled: true, warnings: [SPEC_OFF] },
24 { name: "spec True", env: { OTEL_SDK_DISABLED: "True" }, disabled: true, warnings: [SPEC_OFF] },
25 {
26 name: "spec padded",
27 env: { OTEL_SDK_DISABLED: " true " },
28 disabled: true,
29 warnings: [SPEC_OFF],
30 },
31 { name: "spec false", env: { OTEL_SDK_DISABLED: "false" }, disabled: false, warnings: [] },
32 {
33 name: "spec 1",
34 env: { OTEL_SDK_DISABLED: "1" },
35 disabled: false,
36 warnings: [specIgnored("1")],
37 },
38 {
39 name: "spec padded yes",
40 env: { OTEL_SDK_DISABLED: " yes " },
41 disabled: false,
42 warnings: [specIgnored("yes")],
43 },
44 // T3CODE_OTEL_SDK_DISABLED takes Config.Boolean's values, case-insensitively.
45 { name: "t3 1", env: { T3CODE_OTEL_SDK_DISABLED: "1" }, disabled: true, warnings: [T3_OFF] },
46 {
47 name: "t3 TRUE",
48 env: { T3CODE_OTEL_SDK_DISABLED: "TRUE" },
49 disabled: true,
50 warnings: [T3_OFF],
51 },
52 { name: "t3 n", env: { T3CODE_OTEL_SDK_DISABLED: "n" }, disabled: false, warnings: [] },
53 {
54 name: "t3 false overrides spec true",
55 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
56 disabled: false,
57 warnings: [],
58 },
59 {
60 name: "blank t3 falls through",
61 env: { T3CODE_OTEL_SDK_DISABLED: " ", OTEL_SDK_DISABLED: "true" },
62 disabled: true,
63 warnings: [SPEC_OFF],
64 },
65 {
66 name: "unreadable t3 warns and falls through",
67 env: { T3CODE_OTEL_SDK_DISABLED: "maybe", OTEL_SDK_DISABLED: "true" },
68 disabled: true,
69 warnings: ["T3CODE_OTEL_SDK_DISABLED=maybe is not a yes or a no and was ignored", SPEC_OFF],
70 },
71 {
72 name: "bad spec value still warns when t3 answered",
73 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "yes" },
74 disabled: false,
75 warnings: [specIgnored("yes")],
76 },
77 ])("$name", ({ env, disabled, warnings }) =>
78 Effect.gen(function* () {
79 const resolved = yield* load(env);
80 assert.strictEqual(resolved.disabled, disabled);
81 assert.deepStrictEqual(resolved.warnings, warnings);
82 }),
83 );
84
85 it.effect.each([
86 { name: "unset", env: {}, resourceAttributes: {}, warnings: [] },
87 {
88 name: "a percent-encoded list",
89 env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,message=hello%20world" },
90 resourceAttributes: { team: "core", message: "hello world" },
91 warnings: [],
92 },
93 {
94 name: "a list that does not decode",
95 env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,broken=%zz" },
96 resourceAttributes: {},
97 warnings: [
98 "OTEL_RESOURCE_ATTRIBUTES is not a list of percent-encoded key=value pairs and was ignored",
99 ],
100 },
101 ])("resource attributes: $name", ({ env, resourceAttributes, warnings }) =>
102 Effect.gen(function* () {
103 const resolved = yield* load(env);
104 assert.deepStrictEqual(resolved.resourceAttributes, resourceAttributes);
105 assert.deepStrictEqual(resolved.warnings, warnings);
106 }),
107 );
108
109 describe("endpoints", () => {
110 const urlOf = (signal: OtelEnvironment.OtelSignal) =>
111 OtelEnvironment.OtelSignal.$match(signal, {
112 Export: ({ url }) => url,
113 Off: () => "Off",
114 Unset: () => "Unset",
115 });
116 it.effect.each([
117 {
118 name: "nothing set",
119 env: {},
120 traces: "Unset",
121 metrics: "Unset",
122 logs: "Unset",
123 warnings: [],
124 },
125 {
126 name: "generic endpoint appends each signal's path, keeping the query",
127 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base?api_key=secret" },
128 traces: "https://collector:4318/base/v1/traces?api_key=secret",
129 metrics: "https://collector:4318/base/v1/metrics?api_key=secret",
130 logs: "https://collector:4318/base/v1/logs?api_key=secret",
131 warnings: [],
132 },
133 {
134 name: "a per-signal endpoint is used verbatim",
135 env: { OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/custom" },
136 traces: "https://tempo:4318/custom",
137 metrics: "Unset",
138 logs: "Unset",
139 warnings: [],
140 },
141 {
142 name: "a per-signal endpoint beats the generic one",
143 env: {
144 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/custom",
145 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
146 },
147 traces: "https://tempo:4318/custom",
148 metrics: "https://collector:4318/base/v1/metrics",
149 logs: "https://collector:4318/base/v1/logs",
150 warnings: [],
151 },
152 {
153 name: "a blank per-signal endpoint falls through to the generic one",
154 env: {
155 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: " ",
156 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
157 },
158 traces: "https://collector:4318/base/v1/traces",
159 metrics: "https://collector:4318/base/v1/metrics",
160 logs: "https://collector:4318/base/v1/logs",
161 warnings: [],
162 },
163 {
164 name: "an invalid per-signal endpoint warns and does not fall through",
165 env: {
166 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "not-a-url",
167 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
168 },
169 traces: "Off",
170 metrics: "https://collector:4318/base/v1/metrics",
171 logs: "https://collector:4318/base/v1/logs",
172 warnings: [
173 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
174 ],
175 },
176 {
177 name: "an invalid generic endpoint warns without leaking its query",
178 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "not-a-url?api_key=secret" },
179 traces: "Off",
180 metrics: "Off",
181 logs: "Off",
182 warnings: [
183 "OTEL_EXPORTER_OTLP_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
184 ],
185 },
186 {
187 name: "an endpoint without a scheme is not an http URL",
188 env: { OTEL_EXPORTER_OTLP_ENDPOINT: "localhost:4318" },
189 traces: "Off",
190 metrics: "Off",
191 logs: "Off",
192 warnings: [
193 "OTEL_EXPORTER_OTLP_ENDPOINT is not an http or https URL, so the signals it configures are not exported",
194 ],
195 },
196 {
197 name: "the kill switch wins outright over a valid endpoint",
198 env: {
199 T3CODE_OTEL_SDK_DISABLED: "true",
200 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318/base",
201 },
202 traces: "Unset",
203 metrics: "Unset",
204 logs: "Unset",
205 warnings: [T3_OFF],
206 },
207 ])("$name", ({ env, traces, metrics, logs, warnings }) =>
208 Effect.gen(function* () {
209 const resolved = yield* load(env);
210 assert.strictEqual(urlOf(resolved.traces), traces);
211 assert.strictEqual(urlOf(resolved.metrics), metrics);
212 assert.strictEqual(urlOf(resolved.logs), logs);
213 assert.deepStrictEqual(resolved.warnings, warnings);
214 }),
215 );
216
217 const ENDPOINT = { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318" };
218 const exportOf = (signal: OtelEnvironment.OtelSignal): unknown =>
219 OtelEnvironment.OtelSignal.$match(signal, {
220 Export: ({ protocol, headers }) => ({ protocol, headers }),
221 Off: () => "Off",
222 Unset: () => "Unset",
223 });
224 it.effect.each([
225 {
226 name: "nothing else set takes the specification's default protocol",
227 env: ENDPOINT,
228 traces: { protocol: "http/protobuf", headers: undefined },
229 logs: { protocol: "http/protobuf", headers: undefined },
230 warnings: [],
231 },
232 {
233 name: "headers are comma-separated pairs with percent-encoded values",
234 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS: "api-key=a%20b,tenant=t3" },
235 traces: { protocol: "http/protobuf", headers: { "api-key": "a b", tenant: "t3" } },
236 logs: { protocol: "http/protobuf", headers: { "api-key": "a b", tenant: "t3" } },
237 warnings: [],
238 },
239 {
240 name: "a per-signal protocol and headers beat the generic ones",
241 env: {
242 ...ENDPOINT,
243 OTEL_EXPORTER_OTLP_PROTOCOL: "http/json",
244 OTEL_EXPORTER_OTLP_HEADERS: "api-key=shared",
245 OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: "http/protobuf",
246 OTEL_EXPORTER_OTLP_TRACES_HEADERS: "api-key=traces%20only",
247 },
248 traces: { protocol: "http/protobuf", headers: { "api-key": "traces only" } },
249 logs: { protocol: "http/json", headers: { "api-key": "shared" } },
250 warnings: [],
251 },
252 {
253 name: "an unsupported protocol turns off the signals it configures",
254 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: "grpc" },
255 traces: { protocol: "http/protobuf", headers: undefined },
256 logs: "Off",
257 warnings: [
258 "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL is not http/protobuf or http/json, so the signals it configures are not exported",
259 ],
260 },
261 {
262 name: "a protocol reads case-insensitively",
263 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL: "HTTP/JSON" },
264 traces: { protocol: "http/json", headers: undefined },
265 logs: { protocol: "http/json", headers: undefined },
266 warnings: [],
267 },
268 {
269 name: "undecodable headers turn off every signal once, without leaking them",
270 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz" },
271 traces: "Off",
272 logs: "Off",
273 warnings: [
274 "OTEL_EXPORTER_OTLP_HEADERS is not a list of key=value pairs with percent-encoded values, so the signals it configures are not exported",
275 ],
276 },
277 {
278 name: "a header without a value separator turns its signal off",
279 env: { ...ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_HEADERS: "Authorization" },
280 traces: { protocol: "http/protobuf", headers: undefined },
281 logs: "Off",
282 warnings: [
283 "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",
284 ],
285 },
286 {
287 name: "blank per-signal headers leave the generic ones in charge",
288 env: {
289 ...ENDPOINT,
290 OTEL_EXPORTER_OTLP_HEADERS: "api-key=shared",
291 OTEL_EXPORTER_OTLP_LOGS_HEADERS: " ",
292 },
293 traces: { protocol: "http/protobuf", headers: { "api-key": "shared" } },
294 logs: { protocol: "http/protobuf", headers: { "api-key": "shared" } },
295 warnings: [],
296 },
297 {
298 name: "generic headers every signal overrides say nothing",
299 env: {
300 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://tempo:4318/v1/traces",
301 OTEL_EXPORTER_OTLP_TRACES_HEADERS: "api-key=traces",
302 OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz",
303 },
304 traces: { protocol: "http/protobuf", headers: { "api-key": "traces" } },
305 logs: "Unset",
306 warnings: [],
307 },
308 {
309 name: "protocol and headers say nothing for a signal no endpoint names",
310 env: { OTEL_EXPORTER_OTLP_PROTOCOL: "grpc", OTEL_EXPORTER_OTLP_HEADERS: "api-key=%zz" },
311 traces: "Unset",
312 logs: "Unset",
313 warnings: [],
314 },
315 ])("$name", ({ env, traces, logs, warnings }) =>
316 Effect.gen(function* () {
317 const resolved = yield* load(env);
318 assert.deepStrictEqual(exportOf(resolved.traces), traces);
319 assert.deepStrictEqual(exportOf(resolved.logs), logs);
320 assert.deepStrictEqual(resolved.warnings, warnings);
321 }),
322 );
323 });
324
325 describe("resolveSignalEndpoint", () => {
326 const t3Export = {
327 protocol: "http/json",
328 headers: { "x-key": "t3" },
329 exportIntervalMs: 5_000,
330 } as const;
331 const withLogs = (logs: OtelEnvironment.OtelSignal, disabled = false) => ({
332 ...OtelEnvironment.none,
333 disabled,
334 logs,
335 });
336 const otelExport = OtelEnvironment.OtelSignal.Export({
337 url: "http://otel:4318/v1/logs",
338 protocol: "http/protobuf",
339 headers: { "x-key": "otel" },
340 });
341 it.each([
342 {
343 name: "T3CODE_OTLP_*_URL wins over an OTEL endpoint",
344 otel: withLogs(otelExport),
345 t3Url: "http://t3:4318/v1/logs",
346 expected: { url: "http://t3:4318/v1/logs", export: t3Export },
347 },
348 {
349 name: "T3CODE_OTLP_*_URL wins over a signal the OTEL variables turned off",
350 otel: withLogs(OtelEnvironment.OtelSignal.Off()),
351 t3Url: "http://t3:4318/v1/logs",
352 expected: { url: "http://t3:4318/v1/logs", export: t3Export },
353 },
354 {
355 name: "an OTEL endpoint brings its headers and protocol over the fallback",
356 otel: withLogs(otelExport),
357 t3Url: " ",
358 expected: {
359 url: "http://otel:4318/v1/logs",
360 export: {
361 protocol: "http/protobuf" as const,
362 headers: { "x-key": "otel" },
363 exportIntervalMs: 5_000,
364 },
365 },
366 },
367 {
368 name: "a signal the OTEL variables turned off does not fall through",
369 otel: withLogs(OtelEnvironment.OtelSignal.Off()),
370 t3Url: undefined,
371 expected: undefined,
372 },
373 {
374 name: "an unset signal takes the first non-blank fallback",
375 otel: withLogs(OtelEnvironment.OtelSignal.Unset()),
376 t3Url: undefined,
377 expected: { url: "http://settings:4318/v1/logs", export: t3Export },
378 },
379 {
380 name: "the kill switch wins over everything",
381 otel: withLogs(otelExport, true),
382 t3Url: "http://t3:4318/v1/logs",
383 expected: undefined,
384 },
385 ])("$name", ({ otel, t3Url, expected }) => {
386 assert.deepStrictEqual(
387 OtelEnvironment.resolveSignalEndpoint(
388 otel,
389 "logs",
390 { url: t3Url, export: t3Export },
391 "",
392 "http://settings:4318/v1/logs",
393 ),
394 expected,
395 );
396 });
397 });
398
399 describe("layerResourceAttributes", () => {
400 it.effect.each([
401 { name: "a list that does not decode", raw: "team=%zz", attributes: [] },
402 { name: "encoded separators", raw: "a%2Cb=x%3Dy", attributes: ["a,b"] },
403 ])("lets the exporters' own read succeed with $name", ({ raw, attributes }) =>
404 Effect.gen(function* () {
405 const env = ConfigProvider.layer(
406 ConfigProvider.fromEnv({ env: { OTEL_RESOURCE_ATTRIBUTES: raw } }),
407 );
408 const otel = yield* OtelEnvironment.load.pipe(Effect.provide(env));
409 const resource = yield* OtlpResource.fromConfig({ serviceName: "t3" }).pipe(
410 Effect.provide(
411 Layer.provide(OtelEnvironment.layerResourceAttributes(otel.resourceAttributes), env),
412 ),
413 );
414 assert.deepStrictEqual(
415 resource.attributes.map((attribute) => attribute.key),
416 [...attributes, "service.name"],
417 );
418 }),
419 );
420 });
421});