Skip to content

Commit 42da166

Browse files
t3dotggclaude
andauthored
fix(server): OTLP exports say which machine and build they came from (#19370)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e6bbfdb commit 42da166

5 files changed

Lines changed: 38 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
makeTraceSink,
55
type SignalExport,
66
} from "@t3tools/shared/observability";
7+
import * as HostProcess from "@t3tools/shared/HostProcess";
78
import * as SharedObservability from "@t3tools/shared/observability";
89
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
910
import {
@@ -625,12 +626,16 @@ const layerTelemetry = Layer.unwrap(
625626
Effect.gen(function* () {
626627
const environment = yield* DesktopEnvironment.DesktopEnvironment;
627628
const endpoints = yield* resolveOtlpEndpoints;
629+
// `host.name` keeps machines apart on a shared collector; one set in
630+
// `OTEL_RESOURCE_ATTRIBUTES` wins, since explicit attributes override it.
628631
const resource = {
629632
serviceName: "t3code-desktop",
633+
serviceVersion: environment.appVersion,
630634
attributes: {
631635
"service.namespace": "t3code",
632636
"service.runtime": "desktop",
633637
"service.mode": environment.isDevelopment ? "development" : "packaged",
638+
"host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname),
634639
},
635640
};
636641

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
2+
import { assert, describe, it } from "@effect/vitest";
3+
4+
import * as ServerConfig from "./config.ts";
5+
6+
describe("otlpResource", () => {
7+
const resource = (resourceAttributes: Record<string, string>) =>
8+
ServerConfig.otlpResource(
9+
{ mode: "web", otelEnvironment: { ...OtelEnvironment.none, resourceAttributes } },
10+
"box-1",
11+
);
12+
13+
it("names the host, unless OTEL_RESOURCE_ATTRIBUTES already does", () => {
14+
assert.equal(resource({}).attributes["host.name"], "box-1");
15+
assert.equal(resource({ "host.name": "from-env" }).attributes["host.name"], "from-env");
16+
assert.equal(resource({ "host.name": "" }).attributes["host.name"], "box-1");
17+
});
18+
});

‎apps/server/src/config.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as Schema from "effect/Schema";
1919
import { sweepStalePendingAttachments } from "./attachmentStore.ts";
2020
import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
2121
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
22+
import packageJson from "../package.json" with { type: "json" };
2223

2324
export const DEFAULT_PORT = 3773;
2425

@@ -118,14 +119,21 @@ export const make = (config: ServerConfig["Service"]) => ServerConfig.of(config)
118119

119120
/**
120121
* Resource attributes shared by every OTLP exporter, so traces, metrics, and
121-
* logs report the same service identity to the collector.
122+
* logs report the same service identity to the collector. `host.name` keeps
123+
* servers apart when several export to one collector; a `host.name` set in
124+
* `OTEL_RESOURCE_ATTRIBUTES` wins, since explicit attributes override it.
122125
*/
123-
export const otlpResource = (config: ServerConfig["Service"]) => ({
126+
export const otlpResource = (
127+
config: Pick<ServerConfig["Service"], "mode" | "otelEnvironment">,
128+
hostName: string,
129+
) => ({
124130
serviceName: "t3code-server",
131+
serviceVersion: packageJson.version,
125132
attributes: {
126133
"service.namespace": "t3code",
127134
"service.runtime": "t3-server",
128135
"service.mode": config.mode,
136+
"host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,
129137
},
130138
});
131139

‎apps/server/src/observability/Observability.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
12
import * as HttpObservability from "@t3tools/shared/httpObservability";
23
import { makeLocalFileTracer, makeTraceSink } from "@t3tools/shared/observability";
34
import * as SharedObservability from "@t3tools/shared/observability";
@@ -25,7 +26,7 @@ export const layer = Layer.unwrap(
2526
// The trace serializer stays in the returned context because the browser
2627
// trace forwarder exports on the same signal.
2728
const layerSerialization = SharedObservability.layerOtlpSerialization(traces.protocol);
28-
const resource = ServerConfig.otlpResource(config);
29+
const resource = ServerConfig.otlpResource(config, yield* HostProcess.Hostname);
2930
const attribution = yield* ResourceAttribution.ResourceAttribution;
3031

3132
const layerTraceReferences = Layer.mergeAll(

‎apps/server/src/serverLogger.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
12
import * as SharedObservability from "@t3tools/shared/observability";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
@@ -10,6 +11,7 @@ import * as ServerConfig from "./config.ts";
1011

1112
export const layer = Effect.gen(function* () {
1213
const config = yield* ServerConfig.ServerConfig;
14+
const hostName = yield* HostProcess.Hostname;
1315
const layerMinimumLogLevel = Layer.succeed(References.MinimumLogLevel, config.logLevel);
1416

1517
const logs = config.otlpLogsExport;
@@ -20,7 +22,7 @@ export const layer = Effect.gen(function* () {
2022
url: config.otlpLogsUrl,
2123
exportInterval: `${logs.exportIntervalMs} millis`,
2224
headers: logs.headers,
23-
resource: ServerConfig.otlpResource(config),
25+
resource: ServerConfig.otlpResource(config, hostName),
2426
});
2527

2628
// `Logger.layer` writes the whole logger set rather than adding to it, so

0 commit comments

Comments
 (0)