Skip to content

Commit e67abcf

Browse files
juliusmarmingeyordisclaude
authored
feat(observability): honor the OpenTelemetry kill switch (#13355)
Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
1 parent 9383f4a commit e67abcf

17 files changed

Lines changed: 472 additions & 12 deletions

‎apps/desktop/src/app/DesktopObservability.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, describe, it } from "@effect/vitest";
4+
import * as ConfigProvider from "effect/ConfigProvider";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
@@ -427,6 +428,36 @@ describe("DesktopObservability", () => {
427428
);
428429
});
429430

431+
it.effect("exports kill switch warnings through the configured logger", () => {
432+
const requests: Array<ExportedRequest> = [];
433+
return Effect.gen(function* () {
434+
const fileSystem = yield* FileSystem.FileSystem;
435+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
436+
prefix: "t3-desktop-observability-test-",
437+
});
438+
const environmentLayer = makeEnvironmentLayer(baseDir, true, {
439+
T3CODE_OTLP_LOGS_URL: "https://collector.example.com/v1/logs",
440+
});
441+
442+
yield* Effect.scoped(
443+
Effect.void.pipe(
444+
Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(environmentLayer))),
445+
),
446+
);
447+
448+
assert.include(requests[0]?.body ?? "", "OTEL_SDK_DISABLED=1 was read as false");
449+
}).pipe(
450+
Effect.scoped,
451+
Effect.provide(
452+
Layer.mergeAll(
453+
NodeServices.layer,
454+
collectorLayer(requests),
455+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "1" } })),
456+
),
457+
),
458+
);
459+
});
460+
430461
it.effect("reads every signal endpoint from Settings when the environment names none", () => {
431462
const requests: Array<ExportedRequest> = [];
432463
return Effect.gen(function* () {
@@ -491,4 +522,37 @@ describe("DesktopObservability", () => {
491522
Effect.provide(Layer.mergeAll(NodeServices.layer, collectorLayer(requests))),
492523
);
493524
});
525+
526+
it.effect("stops every export when the OpenTelemetry SDK is disabled", () => {
527+
const requests: Array<ExportedRequest> = [];
528+
return Effect.gen(function* () {
529+
const fileSystem = yield* FileSystem.FileSystem;
530+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
531+
prefix: "t3-desktop-observability-test-",
532+
});
533+
const environmentLayer = makeEnvironmentLayer(baseDir);
534+
yield* writeObservabilitySettings(environmentLayer, {
535+
otlpTracesUrl: "https://settings.example.com/v1/traces",
536+
otlpLogsUrl: "https://settings.example.com/v1/logs",
537+
});
538+
539+
yield* Effect.scoped(
540+
Effect.logInfo("desktop log stays local when disabled").pipe(
541+
Effect.withSpan("desktop-disabled-test"),
542+
Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(environmentLayer))),
543+
),
544+
);
545+
546+
assert.lengthOf(requests, 0);
547+
}).pipe(
548+
Effect.scoped,
549+
Effect.provide(
550+
Layer.mergeAll(
551+
NodeServices.layer,
552+
collectorLayer(requests),
553+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
554+
),
555+
),
556+
);
557+
});
494558
});

‎apps/desktop/src/app/DesktopObservability.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
makeTraceSink,
55
otlpSerializationLayer,
66
} from "@t3tools/shared/observability";
7+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
78
import {
89
parsePersistedServerObservabilitySettings,
910
type PersistedServerObservabilitySettings,
@@ -353,12 +354,18 @@ const readPersistedObservabilitySettings: Effect.Effect<
353354
* resolve traces against one revision of the file and logs against another.
354355
*/
355356
const resolveOtlpEndpoints = Effect.gen(function* () {
357+
const otel = yield* OtelEnvironment.load;
358+
if (otel.disabled) {
359+
return { traces: undefined, metrics: undefined, logs: undefined, warnings: otel.warnings };
360+
}
361+
356362
const environment = yield* DesktopEnvironment.DesktopEnvironment;
357363
const persisted = yield* readPersistedObservabilitySettings;
358364
return {
359365
traces: Option.getOrUndefined(environment.otlpTracesUrl) ?? persisted.otlpTracesUrl,
360366
metrics: Option.getOrUndefined(environment.otlpMetricsUrl) ?? persisted.otlpMetricsUrl,
361367
logs: Option.getOrUndefined(environment.otlpLogsUrl) ?? persisted.otlpLogsUrl,
368+
warnings: otel.warnings,
362369
};
363370
});
364371

@@ -671,7 +678,12 @@ const telemetryLayer = Layer.unwrap(
671678
// resource,
672679
// }).pipe(Layer.provide(serializationLayer));
673680

674-
return Layer.mergeAll(loggerLayer, tracerLayer);
681+
// Logged once the loggers above are installed, so the warnings use them.
682+
const otelWarningsLayer = Layer.effectDiscard(
683+
Effect.forEach(endpoints.warnings, (warning) => Effect.logWarning(warning)),
684+
);
685+
686+
return otelWarningsLayer.pipe(Layer.provideMerge(Layer.mergeAll(loggerLayer, tracerLayer)));
675687
}),
676688
);
677689

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,49 @@ describe("DesktopBackendConfiguration", () => {
946946
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
947947
);
948948

949+
it.effect("resolveWsl carries the kill switch into the distro", () =>
950+
Effect.gen(function* () {
951+
const fileSystem = yield* FileSystem.FileSystem;
952+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
953+
prefix: "t3-desktop-backend-config-test-",
954+
});
955+
956+
const previousWslEnv = process.env.WSLENV;
957+
const previousDisabled = process.env.OTEL_SDK_DISABLED;
958+
try {
959+
delete process.env.WSLENV;
960+
process.env.OTEL_SDK_DISABLED = "true";
961+
962+
yield* Effect.gen(function* () {
963+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
964+
const config = yield* configuration.resolveWsl({ port: 5050, distro: null });
965+
966+
assert.equal(config.env.OTEL_SDK_DISABLED, "true");
967+
assert.include((config.env.WSLENV ?? "").split(":"), "OTEL_SDK_DISABLED");
968+
}).pipe(
969+
Effect.provide(
970+
DesktopBackendConfiguration.layer.pipe(
971+
Layer.provideMerge(serverExposureLayer),
972+
Layer.provideMerge(DesktopAppSettings.layerTest()),
973+
Layer.provideMerge(DesktopWslServerTree.layerTest()),
974+
Layer.provideMerge(
975+
DesktopWslEnvironment.layerTest({
976+
isAvailable: true,
977+
windowsToWslPath: () => Option.some("/mnt/c/repo/apps/server/src/index.ts"),
978+
getDistroIp: () => Option.some("172.27.0.99"),
979+
}),
980+
),
981+
Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
982+
),
983+
),
984+
);
985+
} finally {
986+
restoreEnv("WSLENV", previousWslEnv);
987+
restoreEnv("OTEL_SDK_DISABLED", previousDisabled);
988+
}
989+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
990+
);
991+
949992
it.effect("resolveWsl preserves existing WSLENV entries when forwarding backend secrets", () =>
950993
Effect.gen(function* () {
951994
const fileSystem = yield* FileSystem.FileSystem;

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ const DESKTOP_BACKEND_ENV_NAMES = [
9696
const WSL_FORWARDED_ENV_NAMES = [
9797
"OPENAI_API_KEY",
9898
"ANTHROPIC_API_KEY",
99+
// Otherwise the WSL server keeps exporting to endpoints from the bootstrap.
100+
"T3CODE_OTEL_SDK_DISABLED",
101+
"OTEL_SDK_DISABLED",
99102
"T3CODE_OTLP_HEADERS",
100103
"T3CODE_OTLP_PROTOCOL",
101104
] as const;

‎apps/server/src/bin.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import * as NetService from "@t3tools/shared/Net";
1717
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
1818
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
19+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
1920
import { assert, it } from "@effect/vitest";
2021
import * as Effect from "effect/Effect";
2122
import * as DateTime from "effect/DateTime";
@@ -106,6 +107,7 @@ const makeCliTestServerConfig = (baseDir: string) =>
106107
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
107108
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
108109
otlpServiceName: "t3-server",
110+
otelEnvironment: OtelEnvironment.none,
109111
mode: "web",
110112
port: 0,
111113
host: "127.0.0.1",

‎apps/server/src/cli/config.test.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "@t3tools/contracts";
1919
import * as NetService from "@t3tools/shared/Net";
2020
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
21+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
2122
import * as NodeServices from "@effect/platform-node/NodeServices";
2223
import { deriveServerPaths } from "../config.ts";
2324
import { resolveServerConfig } from "./config.ts";
@@ -56,6 +57,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
5657
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
5758
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
5859
otlpServiceName: "t3-server",
60+
otelEnvironment: OtelEnvironment.none,
5961
devAllowedOrigins: [],
6062
} as const;
6163

@@ -666,6 +668,109 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
666668
}),
667669
);
668670

671+
it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
672+
Effect.gen(function* () {
673+
const fs = yield* FileSystem.FileSystem;
674+
const path = yield* Path.Path;
675+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-off-" });
676+
const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
677+
yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
678+
yield* fs.writeFileString(
679+
derivedPaths.settingsPath,
680+
// @effect-diagnostics-next-line preferSchemaOverJson:off
681+
`${JSON.stringify({
682+
observability: {
683+
otlpTracesUrl: "http://localhost:4318/v1/traces",
684+
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
685+
otlpLogsUrl: "http://localhost:4318/v1/logs",
686+
},
687+
})}\n`,
688+
);
689+
690+
const resolved = yield* resolveServerConfig(
691+
{
692+
mode: Option.some("desktop"),
693+
port: Option.some(4888),
694+
host: Option.none(),
695+
baseDir: Option.some(baseDir),
696+
cwd: Option.none(),
697+
devUrl: Option.none(),
698+
noBrowser: Option.none(),
699+
bootstrapFd: Option.none(),
700+
autoBootstrapProjectFromCwd: Option.none(),
701+
logWebSocketEvents: Option.none(),
702+
tailscaleServeEnabled: Option.none(),
703+
tailscaleServePort: Option.none(),
704+
},
705+
Option.none(),
706+
).pipe(
707+
Effect.provide(
708+
Layer.mergeAll(
709+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
710+
NetService.layer,
711+
),
712+
),
713+
);
714+
715+
// The switch beats every source, including an endpoint stored in Settings.
716+
expect(resolved.otlpTracesUrl).toBeUndefined();
717+
expect(resolved.otlpMetricsUrl).toBeUndefined();
718+
expect(resolved.otlpLogsUrl).toBeUndefined();
719+
expect(resolved.otelEnvironment.disabled).toBe(true);
720+
}),
721+
);
722+
723+
it.effect("lets T3CODE_OTEL_SDK_DISABLED=false override an ambient OTEL_SDK_DISABLED=true", () =>
724+
Effect.gen(function* () {
725+
const fs = yield* FileSystem.FileSystem;
726+
const path = yield* Path.Path;
727+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-on-" });
728+
const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
729+
yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
730+
yield* fs.writeFileString(
731+
derivedPaths.settingsPath,
732+
// @effect-diagnostics-next-line preferSchemaOverJson:off
733+
`${JSON.stringify({
734+
observability: {
735+
otlpTracesUrl: "http://localhost:4318/v1/traces",
736+
},
737+
})}\n`,
738+
);
739+
740+
const resolved = yield* resolveServerConfig(
741+
{
742+
mode: Option.some("desktop"),
743+
port: Option.some(4888),
744+
host: Option.none(),
745+
baseDir: Option.some(baseDir),
746+
cwd: Option.none(),
747+
devUrl: Option.none(),
748+
noBrowser: Option.none(),
749+
bootstrapFd: Option.none(),
750+
autoBootstrapProjectFromCwd: Option.none(),
751+
logWebSocketEvents: Option.none(),
752+
tailscaleServeEnabled: Option.none(),
753+
tailscaleServePort: Option.none(),
754+
},
755+
Option.none(),
756+
).pipe(
757+
Effect.provide(
758+
Layer.mergeAll(
759+
ConfigProvider.layer(
760+
ConfigProvider.fromEnv({
761+
env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
762+
}),
763+
),
764+
NetService.layer,
765+
),
766+
),
767+
);
768+
769+
expect(resolved.otelEnvironment.disabled).toBe(false);
770+
expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
771+
}),
772+
);
773+
669774
it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
670775
Effect.gen(function* () {
671776
const { join } = yield* Path.Path;

‎apps/server/src/cli/config.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
OtlpProtocol,
55
type SignalExport,
66
} from "@t3tools/shared/observability";
7+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
78
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
89
import { DesktopBackendBootstrap, PortSchema } from "@t3tools/contracts";
910
import * as Config from "effect/Config";
@@ -386,6 +387,8 @@ export const resolveServerConfig = (
386387
);
387388
const logLevel = Option.getOrElse(cliLogLevel, () => env.logLevel);
388389

390+
const otel = yield* OtelEnvironment.load;
391+
389392
// T3 Code's own OTLP variables name no signal, so the one answer they give
390393
// is the answer for all three.
391394
const signalExport: SignalExport = {
@@ -401,20 +404,24 @@ export const resolveServerConfig = (
401404
traceBatchWindowMs: env.traceBatchWindowMs,
402405
traceMaxBytes: env.traceMaxBytes,
403406
traceMaxFiles: env.traceMaxFiles,
404-
otlpTracesUrl:
405-
env.otlpTracesUrl ??
406-
bootstrap?.otlpTracesUrl ??
407-
persistedObservabilitySettings.otlpTracesUrl,
408-
otlpMetricsUrl:
409-
env.otlpMetricsUrl ??
410-
bootstrap?.otlpMetricsUrl ??
411-
persistedObservabilitySettings.otlpMetricsUrl,
412-
otlpLogsUrl:
413-
env.otlpLogsUrl ?? bootstrap?.otlpLogsUrl ?? persistedObservabilitySettings.otlpLogsUrl,
407+
otlpTracesUrl: otel.disabled
408+
? undefined
409+
: (env.otlpTracesUrl ??
410+
bootstrap?.otlpTracesUrl ??
411+
persistedObservabilitySettings.otlpTracesUrl),
412+
otlpMetricsUrl: otel.disabled
413+
? undefined
414+
: (env.otlpMetricsUrl ??
415+
bootstrap?.otlpMetricsUrl ??
416+
persistedObservabilitySettings.otlpMetricsUrl),
417+
otlpLogsUrl: otel.disabled
418+
? undefined
419+
: (env.otlpLogsUrl ?? bootstrap?.otlpLogsUrl ?? persistedObservabilitySettings.otlpLogsUrl),
414420
otlpTracesExport: signalExport,
415421
otlpMetricsExport: signalExport,
416422
otlpLogsExport: signalExport,
417423
otlpServiceName: env.otlpServiceName,
424+
otelEnvironment: otel,
418425
mode,
419426
port,
420427
cwd,

‎apps/server/src/cli/pair.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "@t3tools/contracts";
1717
import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
1818
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
19+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
1920
import {
2021
buildTailscaleHttpsBaseUrl,
2122
DEFAULT_TAILSCALE_SERVE_PORT,
@@ -326,6 +327,7 @@ const makePairServerConfig = Effect.fn(function* (input: {
326327
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
327328
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
328329
otlpServiceName: "t3-server",
330+
otelEnvironment: OtelEnvironment.none,
329331
mode: "web",
330332
port: state.port,
331333
host: state.host,

0 commit comments

Comments
 (0)