@devries-cameron20 / t3code Mirror · read-only / commits / 01fe8d9188
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 17:00 UTC

fix(observability): every exported resource names its service instance (#23973)

Signed-off-by: Yordis Prieto <yordisstudio@Yordiss-Mac-Studio.local> Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com> Co-authored-by: Yordis Prieto <yordisstudio@Yordiss-Mac-Studio.local> Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Yordis Prieto committed Oct 25, 2026 at 17:15 UTC 01fe8d91887af5e0669a0378b77a97c9114c43c6
13 files changed +79 -3
apps/desktop/src/app/DesktopObservability.ts
+2
@@ -2,6 +2,7 @@ import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
2 import {
3 makeLocalFileTracer,
4 makeTraceSink,
5 + processServiceInstanceId,
6 type SignalExport,
7 } from "@t3tools/shared/observability";
8 import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -607,6 +608,7 @@ const layerTelemetry = Layer.unwrap(
608 "service.namespace": "t3code",
609 "service.runtime": "desktop",
610 "service.mode": environment.isDevelopment ? "development" : "packaged",
611 + "service.instance.id": processServiceInstanceId(endpoints.resourceAttributes),
612 ...releaseResourceAttributes(
613 environment.isDevelopment ? "dev" : releaseChannelOf(serviceVersion),
614 endpoints.resourceAttributes,
apps/mobile/src/features/observability/tracing.test.ts
+1
@@ -27,6 +27,7 @@ it.effect("does not let OTLP serialization failures alter application effects",
27 },
28 {
29 appVariant: "test",
30 + serviceInstanceId: "test-instance",
31 serviceVersion: "1.2.3",
32 },
33 ).pipe(Layer.provide(layerRemoteHttpClient(fetchFn)));
apps/mobile/src/features/observability/tracing.ts
+3
@@ -12,6 +12,7 @@ export interface TracingConfig {
12
13 export interface TracingResource {
14 readonly serviceVersion?: string;
15 + readonly serviceInstanceId: string;
16 readonly appVariant: string;
17 }
18
@@ -39,6 +40,7 @@ export function layerFromConfig(config: TracingConfig | null, resource: TracingR
40 serviceName: "t3code-mobile",
41 serviceVersion: resource.serviceVersion,
42 runtime: "react-native",
43 + serviceInstanceId: resource.serviceInstanceId,
44 client: `mobile-${resource.appVariant}`,
45 attributes: channel && releaseResourceAttributes(channel),
46 });
@@ -46,6 +48,7 @@ export function layerFromConfig(config: TracingConfig | null, resource: TracingR
48
49 export const layer = layerFromConfig(resolveTracingConfig(), {
50 serviceVersion: Constants.expoConfig?.version,
51 + serviceInstanceId: Constants.sessionId,
52 appVariant:
53 typeof Constants.expoConfig?.extra?.appVariant === "string"
54 ? Constants.expoConfig.extra.appVariant
apps/server/src/cloud/relayTracing.ts
+16 -2
@@ -1,16 +1,30 @@
1 +import { processServiceInstanceId } from "@t3tools/shared/observability";
2 import * as RelayTracing from "@t3tools/shared/relayTracing";
3 +import * as Effect from "effect/Effect";
4 +import * as Layer from "effect/Layer";
5
6 +import * as ServerConfig from "../config.ts";
7 import { resolveRelayClientTracingConfig } from "./publicConfig.ts";
8
9 const relayClientTracingConfig = resolveRelayClientTracingConfig();
10
7 -export const layerHeadlessRelayClient = RelayTracing.layer(relayClientTracingConfig, {
11 +const relayLayer = (resource: Omit<RelayTracing.RelayClientTracingResource, "serviceInstanceId">) =>
12 + Layer.unwrap(
13 + Effect.map(ServerConfig.ServerConfig, (config) =>
14 + RelayTracing.layer(relayClientTracingConfig, {
15 + ...resource,
16 + serviceInstanceId: processServiceInstanceId(config.otelEnvironment.resourceAttributes),
17 + }),
18 + ),
19 + );
20 +
21 +export const layerHeadlessRelayClient = relayLayer({
22 serviceName: "t3code-server",
23 runtime: "node",
24 client: "headless-cli",
25 });
26
13 -export const layerServerRelayBroker = RelayTracing.layer(relayClientTracingConfig, {
27 +export const layerServerRelayBroker = relayLayer({
28 serviceName: "t3code-server",
29 runtime: "node",
30 client: "environment-server",
apps/server/src/config.ts
+6 -1
@@ -10,7 +10,11 @@ import * as Schema from "effect/Schema";
10
11 import type { OtlpEndpointSource } from "@t3tools/contracts";
12 import { sweepStalePendingAttachments } from "./attachmentStore.ts";
13 -import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
13 +import {
14 + DEFAULT_SIGNAL_EXPORT,
15 + processServiceInstanceId,
16 + type SignalExport,
17 +} from "@t3tools/shared/observability";
18 import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
19 import { type ReleaseChannel, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
20 import packageJson from "../package.json" with { type: "json" };
@@ -121,6 +125,7 @@ export const otlpResource = (
125 "service.namespace": "t3code",
126 "service.runtime": "t3-server",
127 "t3code.server.managed_by": config.mode === "desktop" ? "desktop" : "standalone",
128 + "service.instance.id": processServiceInstanceId(config.otelEnvironment.resourceAttributes),
129 ...releaseResourceAttributes(telemetryChannel, config.otelEnvironment.resourceAttributes),
130 "host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,
131 },
apps/server/src/serverLogger.test.ts
+15
@@ -151,6 +151,21 @@ const printThroughConsole = (fields: object) =>
151 });
152
153 describe("ServerLoggerLive", () => {
154 + it.effect("exports the operator's service instance id instead of a generated one", () =>
155 + Effect.gen(function* () {
156 + const { requests } = yield* logInSpanThrough({
157 + otlpLogsUrl: "https://collector.example.com/v1/logs",
158 + otelEnvironment: {
159 + ...OtelEnvironment.none,
160 + resourceAttributes: { "service.instance.id": "pod-7" },
161 + },
162 + });
163 +
164 + const body = requests.map((request) => request.body).join("\n");
165 + assert.include(body, '"key":"service.instance.id","value":{"stringValue":"pod-7"}');
166 + }),
167 + );
168 +
169 it.effect("exports a resource that names whether the desktop app launched the server", () =>
170 Effect.gen(function* () {
171 const { requests } = yield* logInSpanThrough({
apps/web/src/lib/runtime.ts
+1
@@ -22,6 +22,7 @@ const layerRelayTracing = RelayTracing.layer(resolveRelayTracingConfig(), {
22 serviceName: "t3code-web",
23 serviceVersion: import.meta.env.APP_VERSION,
24 runtime: "browser",
25 + serviceInstanceId: ClientTracer.serviceInstanceId,
26 client: typeof window !== "undefined" && window.desktopBridge ? "desktop" : "web",
27 }).pipe(Layer.provide(layerHttpClient));
28
apps/web/src/observability/clientTracer.ts
+5
@@ -1,6 +1,11 @@
1 import * as Layer from "effect/Layer";
2 import * as Tracer from "effect/Tracer";
3
4 +import { randomUUID } from "../lib/utils";
5 +
6 +/** `service.instance.id` for this page load, shared by every resource the UI exports. */
7 +export const serviceInstanceId = randomUUID();
8 +
9 let delegate: Tracer.Tracer | null = null;
10
11 export function setDelegate(next: Tracer.Tracer | null): void {
apps/web/src/observability/clientTracing.ts
+1
@@ -26,6 +26,7 @@ const CLIENT_TRACING_RESOURCE = {
26 serviceName: "t3code-web",
27 attributes: {
28 "service.namespace": "t3code",
29 + "service.instance.id": ClientTracer.serviceInstanceId,
30 ...releaseResourceAttributes(RELEASE_CHANNEL),
31 "service.runtime": "t3-web",
32 "t3code.client.surface": isElectron ? "desktop" : "web",
docs/operations/observability.md
+4
@@ -644,6 +644,10 @@ browser and the desktop window, since it is the same code, and `t3code.client.su
644 or `web`. The server's `t3code.server.managed_by` is `desktop` when the desktop app launched it and
645 `standalone` otherwise.
646
647 +Every resource also carries a `service.instance.id`: random per process for the server and desktop,
648 +per page load for the UI, and the Expo session on mobile. A `service.instance.id` in
649 +`OTEL_RESOURCE_ATTRIBUTES` replaces the generated one on the server and desktop.
650 +
651 If the OTLP URLs are unset, local tracing still works, metrics stay in-process only, and logs stay
652 on stdout only.
653
packages/shared/src/observability.test.ts
+8
@@ -24,8 +24,16 @@ import {
24 type TraceRecord,
25 OtlpHeadersFromString,
26 truncateTraceAttributes,
27 + processServiceInstanceId,
28 } from "./observability.ts";
29
30 +describe("processServiceInstanceId", () => {
31 + it("stays the same for the process unless the operator set one", () => {
32 + expect(processServiceInstanceId()).toBe(processServiceInstanceId({}));
33 + expect(processServiceInstanceId({ "service.instance.id": "pod-7" })).toBe("pod-7");
34 + });
35 +});
36 +
37 describe("errorTag", () => {
38 it("reports structural tags without retaining arbitrary values", () => {
39 assert.equal(errorTag({ _tag: "AcpRequestError" }), "AcpRequestError");
packages/shared/src/observability.ts
+15
@@ -26,6 +26,21 @@ export interface SignalExport {
26 readonly exportIntervalMs: number;
27 }
28
29 +let generatedServiceInstanceId: string | undefined;
30 +
31 +/**
32 + * `service.instance.id` shared by every resource this process exports. Effect lets code
33 + * attributes beat `OTEL_RESOURCE_ATTRIBUTES`, so callers pass the operator's in to keep it winning.
34 + */
35 +export const processServiceInstanceId = (
36 + operatorAttributes: Readonly<Record<string, string>> = {},
37 +): string => {
38 + if (operatorAttributes["service.instance.id"]) return operatorAttributes["service.instance.id"];
39 + // @effect-diagnostics-next-line cryptoRandomUUID:off -- Resources are built synchronously, outside any Effect.
40 + generatedServiceInstanceId ??= crypto.randomUUID();
41 + return generatedServiceInstanceId;
42 +};
43 +
44 export const DEFAULT_SIGNAL_EXPORT: SignalExport = {
45 protocol: "http/json",
46 headers: undefined,
packages/shared/src/relayTracing.ts
+2
@@ -18,6 +18,7 @@ export interface RelayClientTracingResource {
18 readonly serviceName: string;
19 readonly serviceVersion?: string;
20 readonly runtime: string;
21 + readonly serviceInstanceId: string;
22 readonly client: string;
23 readonly component?: string;
24 readonly attributes?: Readonly<Record<string, string>> | undefined;
@@ -166,6 +167,7 @@ export function layer(
167 serviceVersion: resource.serviceVersion,
168 attributes: {
169 "service.namespace": "t3code",
170 + "service.instance.id": resource.serviceInstanceId,
171 "service.runtime": resource.runtime,
172 "t3code.component": resource.component ?? "relay-client",
173 "t3code.client.surface": resource.client,