Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

otelEnvironment.test.ts

131 lines5.1 KB

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("layerResourceAttributes", () => {
110 it.effect.each([
111 { name: "a list that does not decode", raw: "team=%zz", attributes: [] },
112 { name: "encoded separators", raw: "a%2Cb=x%3Dy", attributes: ["a,b"] },
113 ])("lets the exporters' own read succeed with $name", ({ raw, attributes }) =>
114 Effect.gen(function* () {
115 const env = ConfigProvider.layer(
116 ConfigProvider.fromEnv({ env: { OTEL_RESOURCE_ATTRIBUTES: raw } }),
117 );
118 const otel = yield* OtelEnvironment.load.pipe(Effect.provide(env));
119 const resource = yield* OtlpResource.fromConfig({ serviceName: "t3" }).pipe(
120 Effect.provide(
121 Layer.provide(OtelEnvironment.layerResourceAttributes(otel.resourceAttributes), env),
122 ),
123 );
124 assert.deepStrictEqual(
125 resource.attributes.map((attribute) => attribute.key),
126 [...attributes, "service.name"],
127 );
128 }),
129 );
130 });
131});