|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
2 | 2 | import * as ConfigProvider from "effect/ConfigProvider"; |
3 | 3 | import * as Effect from "effect/Effect"; |
4 | | -import * as Layer from "effect/Layer"; |
5 | 4 |
|
6 | 5 | import * as OtelEnvironment from "./otelEnvironment.ts"; |
7 | 6 |
|
8 | | -const withEnv = (env: Record<string, string>) => |
9 | | - Effect.provide(Layer.mergeAll(ConfigProvider.layer(ConfigProvider.fromEnv({ env })))); |
| 7 | +const load = (env: Record<string, string>) => |
| 8 | + OtelEnvironment.load.pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env })))); |
10 | 9 |
|
11 | | -describe("OtelEnvironment", () => { |
12 | | - it.effect("stays enabled when nothing is configured", () => |
13 | | - Effect.gen(function* () { |
14 | | - const resolved = yield* OtelEnvironment.load.pipe(withEnv({})); |
15 | | - assert.strictEqual(resolved.disabled, false); |
16 | | - assert.deepStrictEqual(resolved.warnings, []); |
17 | | - }), |
18 | | - ); |
19 | | - |
20 | | - it.effect("OTEL_SDK_DISABLED disables only on the exact string true", () => |
21 | | - Effect.gen(function* () { |
22 | | - const trueValue = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "true" })); |
23 | | - assert.isTrue(trueValue.disabled); |
24 | | - |
25 | | - const upper = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "True" })); |
26 | | - assert.isTrue(upper.disabled); |
27 | | - |
28 | | - const numeric = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "1" })); |
29 | | - assert.isFalse(numeric.disabled); |
30 | | - |
31 | | - const yes = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "yes" })); |
32 | | - assert.isFalse(yes.disabled); |
33 | | - }), |
34 | | - ); |
35 | | - |
36 | | - it.effect("warns about a value the specification reads as the opposite of its meaning", () => |
37 | | - Effect.gen(function* () { |
38 | | - const affirmative = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "yes" })); |
39 | | - assert.isFalse(affirmative.disabled); |
40 | | - assert.include(affirmative.warnings.join("\n"), "OTEL_SDK_DISABLED=yes was read as false"); |
41 | | - |
42 | | - const spelled = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "false" })); |
43 | | - assert.isFalse(spelled.disabled); |
44 | | - assert.deepStrictEqual(spelled.warnings, []); |
45 | | - }), |
46 | | - ); |
47 | | - |
48 | | - it.effect("keeps complaining about a bad standard value when T3 Code's name answered", () => |
49 | | - Effect.gen(function* () { |
50 | | - const resolved = yield* OtelEnvironment.load.pipe( |
51 | | - withEnv({ T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "yes" }), |
52 | | - ); |
53 | | - assert.isFalse(resolved.disabled); |
54 | | - assert.include(resolved.warnings.join("\n"), "OTEL_SDK_DISABLED=yes was read as false"); |
55 | | - }), |
56 | | - ); |
57 | | - |
58 | | - it.effect("T3CODE_OTEL_SDK_DISABLED accepts the values every T3CODE_* boolean accepts", () => |
59 | | - Effect.gen(function* () { |
60 | | - const numeric = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "1" })); |
61 | | - assert.isTrue(numeric.disabled); |
62 | | - |
63 | | - const yes = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "yes" })); |
64 | | - assert.isTrue(yes.disabled); |
65 | | - |
66 | | - const off = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "off" })); |
67 | | - assert.isFalse(off.disabled); |
| 10 | +const SPEC_OFF = |
| 11 | + "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway"; |
| 12 | +const T3_OFF = |
| 13 | + "T3CODE_OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it"; |
| 14 | +const specIgnored = (value: string) => |
| 15 | + `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`; |
68 | 16 |
|
69 | | - // Config.Boolean's literals, because the specification constrains the |
70 | | - // names it defines and not ours, and T3 Code's own variables should not |
71 | | - // disagree with each other about what a yes looks like. |
72 | | - const shortYes = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "y" })); |
73 | | - assert.isTrue(shortYes.disabled); |
74 | | - |
75 | | - const shortNo = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "n" })); |
76 | | - assert.isFalse(shortNo.disabled); |
77 | | - |
78 | | - // The standard name is case-insensitive by specification, so ours cannot |
79 | | - // be the stricter of the two. |
80 | | - const shouted = yield* OtelEnvironment.load.pipe( |
81 | | - withEnv({ T3CODE_OTEL_SDK_DISABLED: "TRUE" }), |
82 | | - ); |
83 | | - assert.isTrue(shouted.disabled); |
84 | | - }), |
85 | | - ); |
86 | | - |
87 | | - it.effect("an unreadable T3CODE_OTEL_SDK_DISABLED warns and falls through", () => |
88 | | - Effect.gen(function* () { |
89 | | - const resolved = yield* OtelEnvironment.load.pipe( |
90 | | - withEnv({ T3CODE_OTEL_SDK_DISABLED: "maybe", OTEL_SDK_DISABLED: "true" }), |
91 | | - ); |
92 | | - assert.isTrue(resolved.disabled); |
93 | | - assert.include(resolved.warnings.join("\n"), "T3CODE_OTEL_SDK_DISABLED=maybe"); |
94 | | - }), |
95 | | - ); |
96 | | - |
97 | | - it.effect("T3CODE_OTEL_SDK_DISABLED=false overrides an ambient OTEL_SDK_DISABLED=true", () => |
98 | | - Effect.gen(function* () { |
99 | | - const resolved = yield* OtelEnvironment.load.pipe( |
100 | | - withEnv({ T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" }), |
101 | | - ); |
102 | | - assert.isFalse(resolved.disabled); |
103 | | - assert.deepStrictEqual(resolved.warnings, []); |
104 | | - }), |
105 | | - ); |
106 | | - |
107 | | - it.effect("a set but blank value means unset", () => |
108 | | - Effect.gen(function* () { |
109 | | - const resolved = yield* OtelEnvironment.load.pipe( |
110 | | - withEnv({ T3CODE_OTEL_SDK_DISABLED: " ", OTEL_SDK_DISABLED: "true" }), |
111 | | - ); |
112 | | - assert.isTrue(resolved.disabled); |
113 | | - assert.deepStrictEqual(resolved.warnings, [ |
114 | | - "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway", |
115 | | - ]); |
116 | | - }), |
117 | | - ); |
118 | | - |
119 | | - it.effect("padding around a value is not part of the value", () => |
120 | | - Effect.gen(function* () { |
121 | | - // The specification is silent on whitespace, and other SDKs read a |
122 | | - // boolean as trim then compare, so a variable a shell or .env file padded |
123 | | - // still says what the operator wrote. Reading " " as unset and " true " |
124 | | - // as a bad value would also be two different answers to the same |
125 | | - // question. |
126 | | - const padded = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: " true " })); |
127 | | - assert.isTrue(padded.disabled); |
128 | | - |
129 | | - const paddedBad = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: " yes " })); |
130 | | - assert.isFalse(paddedBad.disabled); |
131 | | - assert.deepStrictEqual(paddedBad.warnings, [ |
132 | | - "OTEL_SDK_DISABLED=yes 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", |
133 | | - ]); |
134 | | - }), |
135 | | - ); |
136 | | - |
137 | | - it.effect("names the switch that turned telemetry off", () => |
| 17 | +describe("OtelEnvironment", () => { |
| 18 | + it.effect.each([ |
| 19 | + { name: "nothing set", env: {}, disabled: false, warnings: [] }, |
| 20 | + // OTEL_SDK_DISABLED follows the specification: only `true`, case-insensitively. |
| 21 | + { name: "spec true", env: { OTEL_SDK_DISABLED: "true" }, disabled: true, warnings: [SPEC_OFF] }, |
| 22 | + { name: "spec True", env: { OTEL_SDK_DISABLED: "True" }, disabled: true, warnings: [SPEC_OFF] }, |
| 23 | + { |
| 24 | + name: "spec padded", |
| 25 | + env: { OTEL_SDK_DISABLED: " true " }, |
| 26 | + disabled: true, |
| 27 | + warnings: [SPEC_OFF], |
| 28 | + }, |
| 29 | + { name: "spec false", env: { OTEL_SDK_DISABLED: "false" }, disabled: false, warnings: [] }, |
| 30 | + { |
| 31 | + name: "spec 1", |
| 32 | + env: { OTEL_SDK_DISABLED: "1" }, |
| 33 | + disabled: false, |
| 34 | + warnings: [specIgnored("1")], |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "spec padded yes", |
| 38 | + env: { OTEL_SDK_DISABLED: " yes " }, |
| 39 | + disabled: false, |
| 40 | + warnings: [specIgnored("yes")], |
| 41 | + }, |
| 42 | + // T3CODE_OTEL_SDK_DISABLED takes Config.Boolean's values, case-insensitively. |
| 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 }) => |
138 | 76 | Effect.gen(function* () { |
139 | | - const viaT3 = yield* OtelEnvironment.load.pipe(withEnv({ T3CODE_OTEL_SDK_DISABLED: "true" })); |
140 | | - assert.deepStrictEqual(viaT3.warnings, [ |
141 | | - "T3CODE_OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it", |
142 | | - ]); |
143 | | - |
144 | | - const viaSpec = yield* OtelEnvironment.load.pipe(withEnv({ OTEL_SDK_DISABLED: "true" })); |
145 | | - assert.deepStrictEqual(viaSpec.warnings, [ |
146 | | - "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway", |
147 | | - ]); |
| 77 | + const resolved = yield* load(env); |
| 78 | + assert.strictEqual(resolved.disabled, disabled); |
| 79 | + assert.deepStrictEqual(resolved.warnings, warnings); |
148 | 80 | }), |
149 | 81 | ); |
150 | 82 | }); |
0 commit comments