Skip to content
TypeScript

fix(server): OTLP exports say which machine and build they came from (#19370)

Theo Browne42da166d1310
diff --git a/apps/desktop/src/app/DesktopObservability.ts b/apps/desktop/src/app/DesktopObservability.ts
index 6a44bce6c56..89ed6300205 100644
--- a/apps/desktop/src/app/DesktopObservability.ts
+++ b/apps/desktop/src/app/DesktopObservability.ts
@@ -4,6 +4,7 @@ import {
makeTraceSink,
type SignalExport,
} from "@t3tools/shared/observability";
+import * as HostProcess from "@t3tools/shared/HostProcess";
import * as SharedObservability from "@t3tools/shared/observability";
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
import {
@@ -625,12 +626,16 @@ const layerTelemetry = Layer.unwrap(
Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const endpoints = yield* resolveOtlpEndpoints;
+ // `host.name` keeps machines apart on a shared collector; one set in
+ // `OTEL_RESOURCE_ATTRIBUTES` wins, since explicit attributes override it.
const resource = {
serviceName: "t3code-desktop",
+ serviceVersion: environment.appVersion,
attributes: {
"service.namespace": "t3code",
"service.runtime": "desktop",
"service.mode": environment.isDevelopment ? "development" : "packaged",
+ "host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname),
},
};
diff --git a/apps/server/src/config.test.ts b/apps/server/src/config.test.ts
new file mode 100644
index 00000000000..d21a96c9f41
--- /dev/null
+++ b/apps/server/src/config.test.ts
@@ -0,0 +1,18 @@
+import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
+import { assert, describe, it } from "@effect/vitest";
+
+import * as ServerConfig from "./config.ts";
+
+describe("otlpResource", () => {
+ const resource = (resourceAttributes: Record<string, string>) =>
+ ServerConfig.otlpResource(
+ { mode: "web", otelEnvironment: { ...OtelEnvironment.none, resourceAttributes } },
+ "box-1",
+ );
+
+ it("names the host, unless OTEL_RESOURCE_ATTRIBUTES already does", () => {
+ assert.equal(resource({}).attributes["host.name"], "box-1");
+ assert.equal(resource({ "host.name": "from-env" }).attributes["host.name"], "from-env");
+ assert.equal(resource({ "host.name": "" }).attributes["host.name"], "box-1");
+ });
+});
diff --git a/apps/server/src/config.ts b/apps/server/src/config.ts
index ad2d182a3fa..d832d9dd575 100644
--- a/apps/server/src/config.ts
+++ b/apps/server/src/config.ts
@@ -19,6 +19,7 @@ import * as Schema from "effect/Schema";
import { sweepStalePendingAttachments } from "./attachmentStore.ts";
import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
+import packageJson from "../package.json" with { type: "json" };
export const DEFAULT_PORT = 3773;
@@ -118,14 +119,21 @@ export const make = (config: ServerConfig["Service"]) => ServerConfig.of(config)
/**
* Resource attributes shared by every OTLP exporter, so traces, metrics, and
- * logs report the same service identity to the collector.
+ * logs report the same service identity to the collector. `host.name` keeps
+ * servers apart when several export to one collector; a `host.name` set in
+ * `OTEL_RESOURCE_ATTRIBUTES` wins, since explicit attributes override it.
*/
-export const otlpResource = (config: ServerConfig["Service"]) => ({
+export const otlpResource = (
+ config: Pick<ServerConfig["Service"], "mode" | "otelEnvironment">,
+ hostName: string,
+) => ({
serviceName: "t3code-server",
+ serviceVersion: packageJson.version,
attributes: {
"service.namespace": "t3code",
"service.runtime": "t3-server",
"service.mode": config.mode,
+ "host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,
},
});
diff --git a/apps/server/src/observability/Observability.ts b/apps/server/src/observability/Observability.ts
index 9ae9875749b..80ef1f84b70 100644
--- a/apps/server/src/observability/Observability.ts
+++ b/apps/server/src/observability/Observability.ts
@@ -1,3 +1,4 @@
+import * as HostProcess from "@t3tools/shared/HostProcess";
import * as HttpObservability from "@t3tools/shared/httpObservability";
import { makeLocalFileTracer, makeTraceSink } from "@t3tools/shared/observability";
import * as SharedObservability from "@t3tools/shared/observability";
@@ -25,7 +26,7 @@ export const layer = Layer.unwrap(
// The trace serializer stays in the returned context because the browser
// trace forwarder exports on the same signal.
const layerSerialization = SharedObservability.layerOtlpSerialization(traces.protocol);
- const resource = ServerConfig.otlpResource(config);
+ const resource = ServerConfig.otlpResource(config, yield* HostProcess.Hostname);
const attribution = yield* ResourceAttribution.ResourceAttribution;
const layerTraceReferences = Layer.mergeAll(
diff --git a/apps/server/src/serverLogger.ts b/apps/server/src/serverLogger.ts
index 4dee5b7d1b6..35909c2d1e0 100644
--- a/apps/server/src/serverLogger.ts
+++ b/apps/server/src/serverLogger.ts
@@ -1,3 +1,4 @@
+import * as HostProcess from "@t3tools/shared/HostProcess";
import * as SharedObservability from "@t3tools/shared/observability";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
@@ -10,6 +11,7 @@ import * as ServerConfig from "./config.ts";
export const layer = Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
+ const hostName = yield* HostProcess.Hostname;
const layerMinimumLogLevel = Layer.succeed(References.MinimumLogLevel, config.logLevel);
const logs = config.otlpLogsExport;
@@ -20,7 +22,7 @@ export const layer = Effect.gen(function* () {
url: config.otlpLogsUrl,
exportInterval: `${logs.exportIntervalMs} millis`,
headers: logs.headers,
- resource: ServerConfig.otlpResource(config),
+ resource: ServerConfig.otlpResource(config, hostName),
});
// `Logger.layer` writes the whole logger set rather than adding to it, so