Skip to content

Commit 484c0c5

Browse files
committed
refactor(shared): model OTEL signal state as a Data.TaggedEnum
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 9a747a7 commit 484c0c5

2 files changed

Lines changed: 45 additions & 49 deletions

File tree

‎packages/shared/src/otelEnvironment.test.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ describe("OtelEnvironment", () => {
108108

109109
describe("endpoints", () => {
110110
const urlOf = (signal: OtelEnvironment.OtelSignal) =>
111-
signal._tag === "Export" ? signal.url : signal._tag;
111+
OtelEnvironment.OtelSignal.$match(signal, {
112+
Export: ({ url }) => url,
113+
Off: () => "Off",
114+
Unset: () => "Unset",
115+
});
112116
it.effect.each([
113117
{
114118
name: "nothing set",
@@ -198,9 +202,11 @@ describe("OtelEnvironment", () => {
198202

199203
const ENDPOINT = { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector:4318" };
200204
const exportOf = (signal: OtelEnvironment.OtelSignal): unknown =>
201-
signal._tag === "Export"
202-
? { protocol: signal.protocol, headers: signal.headers }
203-
: signal._tag;
205+
OtelEnvironment.OtelSignal.$match(signal, {
206+
Export: ({ protocol, headers }) => ({ protocol, headers }),
207+
Off: () => "Off",
208+
Unset: () => "Unset",
209+
});
204210
it.effect.each([
205211
{
206212
name: "nothing else set takes the specification's default protocol",
@@ -286,12 +292,11 @@ describe("OtelEnvironment", () => {
286292
disabled,
287293
logs,
288294
});
289-
const otelExport: OtelEnvironment.OtelSignal = {
290-
_tag: "Export",
295+
const otelExport = OtelEnvironment.OtelSignal.Export({
291296
url: "http://otel:4318/v1/logs",
292297
protocol: "http/protobuf",
293298
headers: { "x-key": "otel" },
294-
};
299+
});
295300
it.each([
296301
{
297302
name: "T3CODE_OTLP_*_URL wins over an OTEL endpoint",
@@ -301,7 +306,7 @@ describe("OtelEnvironment", () => {
301306
},
302307
{
303308
name: "T3CODE_OTLP_*_URL wins over a signal the OTEL variables turned off",
304-
otel: withLogs({ _tag: "Off" }),
309+
otel: withLogs(OtelEnvironment.OtelSignal.Off()),
305310
t3Url: "http://t3:4318/v1/logs",
306311
expected: { url: "http://t3:4318/v1/logs", export: t3Export },
307312
},
@@ -320,13 +325,13 @@ describe("OtelEnvironment", () => {
320325
},
321326
{
322327
name: "a signal the OTEL variables turned off does not fall through",
323-
otel: withLogs({ _tag: "Off" }),
328+
otel: withLogs(OtelEnvironment.OtelSignal.Off()),
324329
t3Url: undefined,
325330
expected: undefined,
326331
},
327332
{
328333
name: "an unset signal takes the first non-blank fallback",
329-
otel: withLogs({ _tag: "Unset" }),
334+
otel: withLogs(OtelEnvironment.OtelSignal.Unset()),
330335
t3Url: undefined,
331336
expected: { url: "http://settings:4318/v1/logs", export: t3Export },
332337
},

‎packages/shared/src/otelEnvironment.ts‎

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
import * as Config from "effect/Config";
1212
import * as ConfigProvider from "effect/ConfigProvider";
13+
import * as Data from "effect/Data";
1314
import * as Effect from "effect/Effect";
1415
import * as Option from "effect/Option";
1516
import * as Schema from "effect/Schema";
@@ -25,18 +26,16 @@ type OtlpSignalName = "TRACES" | "METRICS" | "LOGS";
2526
* claimed with an endpoint whose protocol or headers do not read, so it is
2627
* exported nowhere rather than to the bootstrap or Settings collector.
2728
*/
28-
export type OtelSignal =
29-
| { readonly _tag: "Unset" }
30-
| { readonly _tag: "Off" }
31-
| {
32-
readonly _tag: "Export";
33-
readonly url: string;
34-
readonly protocol: OtlpProtocol;
35-
readonly headers: Readonly<Record<string, string>> | undefined;
36-
};
37-
38-
const UNSET: OtelSignal = { _tag: "Unset" };
39-
const OFF: OtelSignal = { _tag: "Off" };
29+
export type OtelSignal = Data.TaggedEnum<{
30+
Unset: {};
31+
Off: {};
32+
Export: {
33+
readonly url: string;
34+
readonly protocol: OtlpProtocol;
35+
readonly headers: Readonly<Record<string, string>> | undefined;
36+
};
37+
}>;
38+
export const OtelSignal = Data.taggedEnum<OtelSignal>();
4039

4140
export interface OtelEnvironment {
4241
/** Whether OTLP export is off, whatever endpoint is configured. */
@@ -220,22 +219,21 @@ const signal = (name: OtlpSignalName, own: Settings, generic: Settings): Resolve
220219
const ownEndpoint = isClaimed(own.endpoint);
221220
const endpoint = ownEndpoint ? own.endpoint : generic.endpoint;
222221
if (endpoint.value === undefined) {
223-
return { signal: UNSET, used: [endpoint] };
222+
return { signal: OtelSignal.Unset(), used: [endpoint] };
224223
}
225224
const protocol = claimed(own.protocol, generic.protocol);
226225
const headers = claimed(own.headers, generic.headers);
227226
const used = [endpoint, protocol, headers];
228227
if (protocol.warning !== undefined || headers.warning !== undefined) {
229-
return { signal: OFF, used };
228+
return { signal: OtelSignal.Off(), used };
230229
}
231230
const url = ownEndpoint ? endpoint.value : withSignalPath(name, endpoint.value);
232231
return {
233-
signal: {
234-
_tag: "Export",
232+
signal: OtelSignal.Export({
235233
url: url.toString(),
236234
protocol: protocol.value ?? "http/protobuf",
237235
headers: headers.value,
238-
},
236+
}),
239237
used,
240238
};
241239
};
@@ -294,9 +292,9 @@ export const load: Effect.Effect<OtelEnvironment> = Config.all({
294292
disabled,
295293
warnings,
296294
resourceAttributes: resource.value,
297-
traces: signals?.traces.signal ?? UNSET,
298-
metrics: signals?.metrics.signal ?? UNSET,
299-
logs: signals?.logs.signal ?? UNSET,
295+
traces: signals?.traces.signal ?? OtelSignal.Unset(),
296+
metrics: signals?.metrics.signal ?? OtelSignal.Unset(),
297+
logs: signals?.logs.signal ?? OtelSignal.Unset(),
300298
};
301299
}),
302300
// Every read above falls back instead of failing, so this cannot happen.
@@ -329,24 +327,17 @@ export const resolveSignalEndpoint = (
329327
if (t3Url !== undefined) {
330328
return { url: t3Url, export: t3.export };
331329
}
332-
const claimedByOtel = otel[signal];
333-
switch (claimedByOtel._tag) {
334-
case "Export":
335-
return {
336-
url: claimedByOtel.url,
337-
export: {
338-
protocol: claimedByOtel.protocol,
339-
headers: claimedByOtel.headers,
340-
exportIntervalMs: t3.export.exportIntervalMs,
341-
},
342-
};
343-
case "Off":
344-
return undefined;
345-
case "Unset": {
330+
return OtelSignal.$match(otel[signal], {
331+
Export: ({ url, protocol, headers }): SignalEndpoint => ({
332+
url,
333+
export: { protocol, headers, exportIntervalMs: t3.export.exportIntervalMs },
334+
}),
335+
Off: () => undefined,
336+
Unset: () => {
346337
const url = fallbackUrls.map(blankAsUnset).find((candidate) => candidate !== undefined);
347338
return url === undefined ? undefined : { url, export: t3.export };
348-
}
349-
}
339+
},
340+
});
350341
};
351342

352343
/**
@@ -373,7 +364,7 @@ export const none: OtelEnvironment = {
373364
disabled: false,
374365
warnings: [],
375366
resourceAttributes: {},
376-
traces: UNSET,
377-
metrics: UNSET,
378-
logs: UNSET,
367+
traces: OtelSignal.Unset(),
368+
metrics: OtelSignal.Unset(),
369+
logs: OtelSignal.Unset(),
379370
};

0 commit comments

Comments
 (0)