Skip to content
TypeScript

refactor(observability): hold OTLP export settings per signal (#12657)

Yordis Prietofa1e1715538f
diff --git a/apps/server/src/bin.test.ts b/apps/server/src/bin.test.ts
index 795bb05db14..9bc20fff84e 100644
--- a/apps/server/src/bin.test.ts
+++ b/apps/server/src/bin.test.ts
@@ -15,6 +15,7 @@ import {
} from "@t3tools/contracts";
import * as NetService from "@t3tools/shared/Net";
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
+import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as DateTime from "effect/DateTime";
@@ -101,10 +102,10 @@ const makeCliTestServerConfig = (baseDir: string) =>
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
mode: "web",
port: 0,
host: "127.0.0.1",
diff --git a/apps/server/src/cli/config.test.ts b/apps/server/src/cli/config.test.ts
index aea753c3398..34ea7f68536 100644
--- a/apps/server/src/cli/config.test.ts
+++ b/apps/server/src/cli/config.test.ts
@@ -17,6 +17,7 @@ import {
type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
} from "@t3tools/contracts";
import * as NetService from "@t3tools/shared/Net";
+import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { deriveServerPaths } from "../config.ts";
import { resolveServerConfig } from "./config.ts";
@@ -51,10 +52,10 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
devAllowedOrigins: [],
} as const;
@@ -764,7 +765,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
),
);
- expect(resolved.otlpHeaders).toEqual({
+ expect(resolved.otlpTracesExport.headers).toEqual({
authorization: "Basic abc==",
"x-tenant": "t3",
});
@@ -808,7 +809,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
),
);
- expect(resolved.otlpHeaders).toEqual({
+ expect(resolved.otlpTracesExport.headers).toEqual({
authorization: "Bearer abc==",
"x-tenant": "t3",
});
@@ -816,7 +817,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
}),
);
- it.effect("reads the OTLP protocol from env", () =>
+ it.effect("gives every signal the protocol named without one", () =>
Effect.gen(function* () {
const { join } = yield* Path.Path;
const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-protocol-base");
@@ -848,7 +849,11 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
),
);
- expect(resolved.otlpProtocol).toBe("http/protobuf");
+ expect([
+ resolved.otlpTracesExport.protocol,
+ resolved.otlpMetricsExport.protocol,
+ resolved.otlpLogsExport.protocol,
+ ]).toEqual(["http/protobuf", "http/protobuf", "http/protobuf"]);
}),
);
diff --git a/apps/server/src/cli/config.ts b/apps/server/src/cli/config.ts
index 32f465b6fb0..09a30aeb19e 100644
--- a/apps/server/src/cli/config.ts
+++ b/apps/server/src/cli/config.ts
@@ -1,5 +1,9 @@
import * as NetService from "@t3tools/shared/Net";
-import { OtlpHeadersFromString, OtlpProtocol } from "@t3tools/shared/observability";
+import {
+ OtlpHeadersFromString,
+ OtlpProtocol,
+ type SignalExport,
+} from "@t3tools/shared/observability";
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
import { DesktopBackendBootstrap, PortSchema } from "@t3tools/contracts";
import * as Config from "effect/Config";
@@ -382,6 +386,14 @@ export const resolveServerConfig = (
);
const logLevel = Option.getOrElse(cliLogLevel, () => env.logLevel);
+ // T3 Code's own OTLP variables name no signal, so the one answer they give
+ // is the answer for all three.
+ const signalExport: SignalExport = {
+ protocol: env.otlpProtocol,
+ headers: env.otlpHeaders,
+ exportIntervalMs: env.otlpExportIntervalMs,
+ };
+
const config: ServerConfig.ServerConfig["Service"] = {
logLevel,
traceMinLevel: env.traceMinLevel,
@@ -399,10 +411,10 @@ export const resolveServerConfig = (
persistedObservabilitySettings.otlpMetricsUrl,
otlpLogsUrl:
env.otlpLogsUrl ?? bootstrap?.otlpLogsUrl ?? persistedObservabilitySettings.otlpLogsUrl,
- otlpExportIntervalMs: env.otlpExportIntervalMs,
+ otlpTracesExport: signalExport,
+ otlpMetricsExport: signalExport,
+ otlpLogsExport: signalExport,
otlpServiceName: env.otlpServiceName,
- otlpHeaders: env.otlpHeaders,
- otlpProtocol: env.otlpProtocol,
mode,
port,
cwd,
diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts
index 9ff9a8e13b3..493e6b71941 100644
--- a/apps/server/src/cli/pair.ts
+++ b/apps/server/src/cli/pair.ts
@@ -15,6 +15,7 @@ import {
PortSchema,
} from "@t3tools/contracts";
import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
+import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
import {
buildTailscaleHttpsBaseUrl,
DEFAULT_TAILSCALE_SERVE_PORT,
@@ -321,10 +322,10 @@ const makePairServerConfig = Effect.fn(function* (input: {
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
mode: "web",
port: state.port,
host: state.host,
diff --git a/apps/server/src/config.ts b/apps/server/src/config.ts
index 4835ebb40e4..5762ccdb6ff 100644
--- a/apps/server/src/config.ts
+++ b/apps/server/src/config.ts
@@ -17,7 +17,7 @@ import type * as Redacted from "effect/Redacted";
import * as Schema from "effect/Schema";
import { sweepStalePendingAttachments } from "./attachmentStore.ts";
-import { OtlpProtocol } from "@t3tools/shared/observability";
+import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
export const DEFAULT_PORT = 3773;
@@ -73,10 +73,15 @@ export class ServerConfig extends Context.Service<
readonly otlpTracesUrl: string | undefined;
readonly otlpMetricsUrl: string | undefined;
readonly otlpLogsUrl: string | undefined;
- readonly otlpExportIntervalMs: number;
+ /**
+ * How each signal is exported. Read instead of a process-wide setting so
+ * the wire format, credential, and schedule travel with the endpoint they
+ * were configured beside.
+ */
+ readonly otlpTracesExport: SignalExport;
+ readonly otlpMetricsExport: SignalExport;
+ readonly otlpLogsExport: SignalExport;
readonly otlpServiceName: string;
- readonly otlpHeaders: Readonly<Record<string, string>> | undefined;
- readonly otlpProtocol: OtlpProtocol;
readonly mode: RuntimeMode;
readonly port: number;
readonly host: string | undefined;
@@ -212,10 +217,10 @@ const makeTest = Effect.fn("ServerConfig.makeTest")(function* (
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
cwd,
baseDir,
...derivedPaths,
diff --git a/apps/server/src/environment/ServerEnvironment.test.ts b/apps/server/src/environment/ServerEnvironment.test.ts
index 0ccf8de691e..b4758e98006 100644
--- a/apps/server/src/environment/ServerEnvironment.test.ts
+++ b/apps/server/src/environment/ServerEnvironment.test.ts
@@ -10,6 +10,8 @@ import * as Option from "effect/Option";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
+import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import {
PUBLISH_AGENT_ACTIVITY_SECRET,
@@ -54,10 +56,10 @@ const makeServerConfig = Effect.fn(function* (baseDir: string) {
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
cwd: process.cwd(),
baseDir,
mode: "web",
diff --git a/apps/server/src/http.ts b/apps/server/src/http.ts
index 879ca66ca60..7533b8c1db1 100644
--- a/apps/server/src/http.ts
+++ b/apps/server/src/http.ts
@@ -322,7 +322,7 @@ export const otlpTracesProxyRouteLayer = HttpRouter.add(
const request = yield* HttpServerRequest.HttpServerRequest;
const config = yield* ServerConfig.ServerConfig;
const otlpTracesUrl = config.otlpTracesUrl;
- const otlpHeaders = config.otlpHeaders;
+ const otlpHeaders = config.otlpTracesExport.headers;
const browserTraceCollector = yield* BrowserTraceCollector.BrowserTraceCollector;
const httpClient = yield* HttpClient.HttpClient;
const serialization = yield* OtlpSerialization.OtlpSerialization;
diff --git a/apps/server/src/observability/Layers/Observability.ts b/apps/server/src/observability/Layers/Observability.ts
index 0c9acdfb1ee..8f7b607745f 100644
--- a/apps/server/src/observability/Layers/Observability.ts
+++ b/apps/server/src/observability/Layers/Observability.ts
@@ -20,7 +20,11 @@ import * as BrowserTraceCollector from "../BrowserTraceCollector.ts";
export const ObservabilityLive = Layer.unwrap(
Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
- const serializationLayer = otlpSerializationLayer(config.otlpProtocol);
+ const traces = config.otlpTracesExport;
+ const metrics = config.otlpMetricsExport;
+ // The trace serializer stays in the returned context because the browser
+ // trace forwarder exports on the same signal.
+ const serializationLayer = otlpSerializationLayer(traces.protocol);
const resource = ServerConfig.otlpResource(config);
const attribution = yield* ResourceAttribution.ResourceAttribution;
@@ -51,8 +55,8 @@ export const ObservabilityLive = Layer.unwrap(
? undefined
: yield* OtlpTracer.make({
url: config.otlpTracesUrl,
- exportInterval: `${config.otlpExportIntervalMs} millis`,
- headers: config.otlpHeaders,
+ exportInterval: `${traces.exportIntervalMs} millis`,
+ headers: traces.headers,
resource,
});
@@ -77,10 +81,10 @@ export const ObservabilityLive = Layer.unwrap(
? Layer.empty
: OtlpMetrics.layer({
url: config.otlpMetricsUrl,
- exportInterval: `${config.otlpExportIntervalMs} millis`,
- headers: config.otlpHeaders,
+ exportInterval: `${metrics.exportIntervalMs} millis`,
+ headers: metrics.headers,
resource,
- }).pipe(Layer.provideMerge(serializationLayer));
+ }).pipe(Layer.provide(otlpSerializationLayer(metrics.protocol)));
return Layer.mergeAll(ServerLoggerLive, traceReferencesLayer, tracerLayer, metricsLayer);
}),
diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts
index 2618020d388..5d46c866a16 100644
--- a/apps/server/src/server.test.ts
+++ b/apps/server/src/server.test.ts
@@ -218,7 +218,7 @@ import {
transferBudgetViolations,
} from "../integration/TransferBudgetReport.integration.ts";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
-import { otlpSerializationLayer } from "@t3tools/shared/observability";
+import { DEFAULT_SIGNAL_EXPORT, otlpSerializationLayer } from "@t3tools/shared/observability";
const defaultProjectId = ProjectId.make("project-default");
const defaultThreadId = ThreadId.make("thread-default");
@@ -578,10 +578,10 @@ const buildAppUnderTest = (options?: {
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
mode: "desktop",
port: 0,
host: "127.0.0.1",
@@ -1076,7 +1076,7 @@ const buildAppUnderTest = (options?: {
...options?.layers?.browserTraceCollector,
}),
),
- Layer.provide(otlpSerializationLayer(config.otlpProtocol)),
+ Layer.provide(otlpSerializationLayer(config.otlpTracesExport.protocol)),
Layer.provide(
Layer.mock(ServerLifecycleEvents.ServerLifecycleEvents)({
publish: (event) => Effect.succeed({ ...(event as any), sequence: 1 }),
@@ -5299,7 +5299,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
yield* buildAppUnderTest({
config: {
otlpTracesUrl: collector.url,
- otlpProtocol: "http/protobuf",
+ otlpTracesExport: { ...DEFAULT_SIGNAL_EXPORT, protocol: "http/protobuf" },
},
layers: {
browserTraceCollector: {
diff --git a/apps/server/src/serverLogger.test.ts b/apps/server/src/serverLogger.test.ts
index 59ca908cb4a..cbb5056ed31 100644
--- a/apps/server/src/serverLogger.test.ts
+++ b/apps/server/src/serverLogger.test.ts
@@ -8,6 +8,8 @@ import * as Tracer from "effect/Tracer";
import * as HttpClient from "effect/unstable/http/HttpClient";
import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
+import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
+
import * as ServerConfig from "./config.ts";
import { ServerLoggerLive } from "./serverLogger.ts";
@@ -51,10 +53,10 @@ const configLayer = (overrides: Partial<ServerConfig.ServerConfig["Service"]>) =
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
- otlpExportIntervalMs: 10_000,
+ otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
+ otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
+ otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otlpServiceName: "t3-server",
- otlpHeaders: undefined,
- otlpProtocol: "http/json",
cwd: baseDir,
baseDir,
...derivedPaths,
@@ -155,12 +157,15 @@ describe("ServerLoggerLive", () => {
}),
);
- it.effect("sends the headers and wire format the rest of OTLP export already uses", () =>
+ it.effect("sends the headers and wire format the log signal asked for", () =>
Effect.gen(function* () {
const requests = yield* logThrough({
otlpLogsUrl: "https://collector.example.com/v1/logs",
- otlpProtocol: "http/protobuf",
- otlpHeaders: { "x-scope": "logs" },
+ otlpLogsExport: {
+ ...DEFAULT_SIGNAL_EXPORT,
+ protocol: "http/protobuf",
+ headers: { "x-scope": "logs" },
+ },
});
assert.lengthOf(requests, 1);
diff --git a/apps/server/src/serverLogger.ts b/apps/server/src/serverLogger.ts
index 389a9535efa..6d19907c20c 100644
--- a/apps/server/src/serverLogger.ts
+++ b/apps/server/src/serverLogger.ts
@@ -12,13 +12,14 @@ export const ServerLoggerLive = Effect.gen(function* () {
const config = yield* ServerConfig;
const minimumLogLevelLayer = Layer.succeed(References.MinimumLogLevel, config.logLevel);
+ const logs = config.otlpLogsExport;
const otlpLogger =
config.otlpLogsUrl === undefined
? undefined
: OtlpLogger.make({
url: config.otlpLogsUrl,
- exportInterval: `${config.otlpExportIntervalMs} millis`,
- headers: config.otlpHeaders,
+ exportInterval: `${logs.exportIntervalMs} millis`,
+ headers: logs.headers,
resource: otlpResource(config),
});
@@ -41,7 +42,7 @@ export const ServerLoggerLive = Effect.gen(function* () {
{ mergeWithExisting: false },
).pipe(
Layer.provide(OtlpExporter.layerFlusher),
- Layer.provide(otlpSerializationLayer(config.otlpProtocol)),
+ Layer.provide(otlpSerializationLayer(logs.protocol)),
);
return Layer.mergeAll(loggerLayer, minimumLogLevelLayer);
diff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.ts
index 62121399853..6393ed0879d 100644
--- a/packages/shared/src/observability.ts
+++ b/packages/shared/src/observability.ts
@@ -16,6 +16,25 @@ export type OtlpProtocol = typeof OtlpProtocol.Type;
export const otlpSerializationLayer = (protocol: OtlpProtocol) =>
protocol === "http/protobuf" ? OtlpSerialization.layerProtobuf : OtlpSerialization.layerJson;
+/**
+ * How one signal is exported, once whichever source named that signal's
+ * endpoint has been resolved. Held per signal rather than per process, so a
+ * wire format or a credential cannot be paired by hand with an endpoint that
+ * came from somewhere else.
+ */
+export interface SignalExport {
+ readonly protocol: OtlpProtocol;
+ readonly headers: Readonly<Record<string, string>> | undefined;
+ readonly exportIntervalMs: number;
+}
+
+/** What T3 Code exports with when nothing configured a signal. */
+export const DEFAULT_SIGNAL_EXPORT: SignalExport = {
+ protocol: "http/json",
+ headers: undefined,
+ exportIntervalMs: 10_000,
+};
+
const FLUSH_BUFFER_THRESHOLD = 256;
const textEncoder = new TextEncoder();