TypeScript
fix(observability): resources add OTel semconv runtime and browser keys (#23972)
Yordis Prieto20ee805de9ee
diff --git a/apps/desktop/src/app/DesktopObservability.test.ts b/apps/desktop/src/app/DesktopObservability.test.tsindex 6ddb31cb6ea..ab8ea0b8e9d 100644--- a/apps/desktop/src/app/DesktopObservability.test.ts+++ b/apps/desktop/src/app/DesktopObservability.test.ts@@ -342,4 +342,36 @@ describe("DesktopObservability", () => {Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),);});++ it.effect("describes the Node runtime and the desktop mode under semantic or t3code keys", () => {+ const requests: Array<ExportedRequest> = [];+ return Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const baseDir = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3-desktop-observability-test-",+ });+ const layerEnvironment = layerEnvironmentFor(baseDir);+ yield* writeObservabilitySettings(layerEnvironment, {+ otlpLogsUrl: "https://settings.example.com/v1/logs",+ });++ yield* Effect.scoped(+ Effect.logInfo("desktop resource under test").pipe(+ Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),+ ),+ );++ const body = requests.map((request) => request.body).join("\n");+ assert.include(body, '"key":"process.runtime.name","value":{"stringValue":"nodejs"}');+ assert.include(+ body,+ `"key":"process.runtime.version","value":{"stringValue":"${process.versions.node}"}`,+ );+ assert.include(body, '"key":"t3code.desktop.mode","value":{"stringValue":"development"}');+ assert.notInclude(body, "service.mode");+ }).pipe(+ Effect.scoped,+ Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),+ );+ });});diff --git a/apps/desktop/src/app/DesktopObservability.ts b/apps/desktop/src/app/DesktopObservability.tsindex 1d1e79a4fe9..365973f5a30 100644--- a/apps/desktop/src/app/DesktopObservability.ts+++ b/apps/desktop/src/app/DesktopObservability.ts@@ -2,6 +2,7 @@ import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";import {makeLocalFileTracer,makeTraceSink,+ nodeProcessRuntimeAttributes,processServiceInstanceId,type SignalExport,} from "@t3tools/shared/observability";@@ -607,13 +608,14 @@ const layerTelemetry = Layer.unwrap(attributes: {"service.namespace": "t3code","service.runtime": "desktop",- "service.mode": environment.isDevelopment ? "development" : "packaged",+ "t3code.desktop.mode": environment.isDevelopment ? "development" : "packaged","service.instance.id": processServiceInstanceId(endpoints.resourceAttributes),...releaseResourceAttributes(environment.isDevelopment ? "dev" : releaseChannelOf(serviceVersion),endpoints.resourceAttributes,),"host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname),+ ...nodeProcessRuntimeAttributes(),},};diff --git a/apps/server/src/cloud/relayTracing.ts b/apps/server/src/cloud/relayTracing.tsindex d5f1380f966..13a96db1320 100644--- a/apps/server/src/cloud/relayTracing.ts+++ b/apps/server/src/cloud/relayTracing.ts@@ -1,4 +1,7 @@-import { processServiceInstanceId } from "@t3tools/shared/observability";+import {+ nodeProcessRuntimeAttributes,+ processServiceInstanceId,+} from "@t3tools/shared/observability";import * as RelayTracing from "@t3tools/shared/relayTracing";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -21,12 +24,14 @@ const relayLayer = (resource: Omit<RelayTracing.RelayClientTracingResource, "serexport const layerHeadlessRelayClient = relayLayer({serviceName: "t3code-server",runtime: "node",+ attributes: nodeProcessRuntimeAttributes(),client: "headless-cli",});export const layerServerRelayBroker = relayLayer({serviceName: "t3code-server",runtime: "node",+ attributes: nodeProcessRuntimeAttributes(),client: "environment-server",component: "relay-broker",});diff --git a/apps/server/src/config.ts b/apps/server/src/config.tsindex 47a38b4020a..91d44330877 100644--- a/apps/server/src/config.ts+++ b/apps/server/src/config.ts@@ -12,6 +12,7 @@ import type { OtlpEndpointSource } from "@t3tools/contracts";import { sweepStalePendingAttachments } from "./attachmentStore.ts";import {DEFAULT_SIGNAL_EXPORT,+ nodeProcessRuntimeAttributes,processServiceInstanceId,type SignalExport,} from "@t3tools/shared/observability";@@ -128,6 +129,7 @@ export const otlpResource = ("service.instance.id": processServiceInstanceId(config.otelEnvironment.resourceAttributes),...releaseResourceAttributes(telemetryChannel, config.otelEnvironment.resourceAttributes),"host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,+ ...nodeProcessRuntimeAttributes(),},});diff --git a/apps/web/src/observability/clientTracing.ts b/apps/web/src/observability/clientTracing.tsindex 43a9ebd1f5b..bdfb8a62767 100644--- a/apps/web/src/observability/clientTracing.ts+++ b/apps/web/src/observability/clientTracing.ts@@ -15,6 +15,32 @@ import { isElectron } from "../env";import { APP_VERSION } from "~/branding";const DEFAULT_EXPORT_INTERVAL_MS = 1_000;+interface NavigatorUserAgentData {+ readonly platform: string;+ readonly mobile: boolean;+ readonly brands: ReadonlyArray<{ readonly brand: string; readonly version: string }>;+}++/**+ * The `browser.*` and `user_agent.original` attributes the OpenTelemetry browser+ * resource detector reports. `userAgentData` exists only in Chromium, which+ * includes the desktop app.+ */+const browserResourceAttributes = (): Record<string, unknown> => {+ if (typeof navigator === "undefined") return {};+ const userAgentData = (navigator as Navigator & { userAgentData?: NavigatorUserAgentData })+ .userAgentData;+ return {+ "user_agent.original": navigator.userAgent,+ "browser.language": navigator.language,+ ...(userAgentData && {+ "browser.platform": userAgentData.platform,+ "browser.mobile": userAgentData.mobile,+ "browser.brands": userAgentData.brands.map(({ brand, version }) => `${brand} ${version}`),+ }),+ };+};+// PR previews can't mark themselves in APP_VERSION without breaking the version-skew check against the server.const RELEASE_CHANNEL =import.meta.env.DEV || import.meta.env.VITE_RELEASE_CHANNEL === "dev"@@ -31,6 +57,7 @@ const CLIENT_TRACING_RESOURCE = {"service.runtime": "t3-web","t3code.client.surface": isElectron ? "desktop" : "web","service.version": APP_VERSION,+ ...browserResourceAttributes(),},} as const;diff --git a/docs/operations/observability.md b/docs/operations/observability.mdindex 51413e205da..ab27a5f87c6 100644--- a/docs/operations/observability.md+++ b/docs/operations/observability.md@@ -642,7 +642,11 @@ reports `dev` from a development bundle.Attributes specific to T3 Code use the `t3code.` prefix. The UI runs as `t3code-web` in both abrowser and the desktop window, since it is the same code, and `t3code.client.surface` is `desktop`or `web`. The server's `t3code.server.managed_by` is `desktop` when the desktop app launched it and-`standalone` otherwise.+`standalone` otherwise. The desktop main process reports `t3code.desktop.mode` as `development` or+`packaged` (formerly `service.mode`; dashboards filtering on that key must switch). Node processes+also report the semantic `process.runtime.name`, `process.runtime.version`, and+`process.runtime.description`, and UI traces report `browser.language`, `user_agent.original`, and,+in Chromium, `browser.platform`, `browser.mobile`, and `browser.brands`.Every resource also carries a `service.instance.id`: random per process for the server and desktop,per page load for the UI, and the Expo session on mobile. A `service.instance.id` indiff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.tsindex db1f50734be..4109442553e 100644--- a/packages/shared/src/observability.ts+++ b/packages/shared/src/observability.ts@@ -26,6 +26,18 @@ export interface SignalExport {readonly exportIntervalMs: number;}+/**+ * `process.runtime.*` resource attributes for a Node process, matching the+ * OpenTelemetry Node process detector. Electron embeds Node, so an Electron+ * process reports Node and names Electron in the description.+ */+export const nodeProcessRuntimeAttributes = (): Record<string, string> => ({+ "process.runtime.name": "nodejs",+ "process.runtime.version": process.versions.node,+ "process.runtime.description": process.versions.electron+ ? `Electron ${process.versions.electron}`+ : "Node.js",+});let generatedServiceInstanceId: string | undefined;/**