Skip to content

Commit a960f56

Browse files
yordisjuliusmarmingeclaude
authored
fix(observability): every app reports its own environment and release channel (#24340)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
1 parent 1646a47 commit a960f56

15 files changed

Lines changed: 129 additions & 15 deletions

File tree

‎.github/workflows/web-preview.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ jobs:
8282
--archive=tgz \
8383
--yes \
8484
--token "$VERCEL_TOKEN" \
85-
--scope "$vercel_scope"
85+
--scope "$vercel_scope" \
86+
--build-env "VITE_RELEASE_CHANNEL=preview"
8687
)"
8788
printf '%s\n' "$deployment_output"
8889
# Vercel can print lockfile warnings to stdout before the deployment URL.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import * as HostProcess from "@t3tools/shared/HostProcess";
88
import * as SharedObservability from "@t3tools/shared/observability";
99
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
10+
import { releaseChannelOf, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
1011
import {
1112
parsePersistedServerObservabilitySettings,
1213
type PersistedServerObservabilitySettings,
@@ -595,14 +596,21 @@ const layerTelemetry = Layer.unwrap(
595596
Effect.gen(function* () {
596597
const environment = yield* DesktopEnvironment.DesktopEnvironment;
597598
const endpoints = yield* resolveOtlpEndpoints;
599+
// Unpackaged runs launch a script, so Electron reports its own version.
600+
const serviceVersion = environment.isPackaged
601+
? environment.appVersion
602+
: desktopPackageJson.version;
598603
const resource = {
599604
serviceName: "t3code-desktop",
600-
// Unpackaged runs launch a script, so Electron reports its own version.
601-
serviceVersion: environment.isPackaged ? environment.appVersion : desktopPackageJson.version,
605+
serviceVersion,
602606
attributes: {
603607
"service.namespace": "t3code",
604608
"service.runtime": "desktop",
605609
"service.mode": environment.isDevelopment ? "development" : "packaged",
610+
...releaseResourceAttributes(
611+
environment.isDevelopment ? "dev" : releaseChannelOf(serviceVersion),
612+
endpoints.resourceAttributes,
613+
),
606614
"host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname),
607615
},
608616
};

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ describe("DesktopBackendConfiguration", () => {
678678
),
679679
);
680680

681-
it.effect("resolveWsl carries the kill switch into the distro", () =>
681+
it.effect("resolveWsl carries the kill switch and resource attributes into the distro", () =>
682682
Effect.gen(function* () {
683683
const fileSystem = yield* FileSystem.FileSystem;
684684
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
@@ -688,10 +688,12 @@ describe("DesktopBackendConfiguration", () => {
688688
const previousWslEnv = process.env.WSLENV;
689689
const previousDisabled = process.env.OTEL_SDK_DISABLED;
690690
const previousTelemetry = process.env.T3CODE_TELEMETRY_ENABLED;
691+
const previousResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES;
691692
try {
692693
delete process.env.WSLENV;
693694
process.env.OTEL_SDK_DISABLED = "true";
694695
process.env.T3CODE_TELEMETRY_ENABLED = "false";
696+
process.env.OTEL_RESOURCE_ATTRIBUTES = "deployment.environment.name=staging";
695697

696698
yield* Effect.gen(function* () {
697699
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
@@ -701,6 +703,8 @@ describe("DesktopBackendConfiguration", () => {
701703
assert.include((config.env.WSLENV ?? "").split(":"), "OTEL_SDK_DISABLED");
702704
assert.equal(config.env.T3CODE_TELEMETRY_ENABLED, "false");
703705
assert.include((config.env.WSLENV ?? "").split(":"), "T3CODE_TELEMETRY_ENABLED");
706+
assert.equal(config.env.OTEL_RESOURCE_ATTRIBUTES, "deployment.environment.name=staging");
707+
assert.include((config.env.WSLENV ?? "").split(":"), "OTEL_RESOURCE_ATTRIBUTES");
704708
}).pipe(
705709
Effect.provide(
706710
layerBackendConfiguration().pipe(
@@ -722,6 +726,7 @@ describe("DesktopBackendConfiguration", () => {
722726
restoreEnv("WSLENV", previousWslEnv);
723727
restoreEnv("OTEL_SDK_DISABLED", previousDisabled);
724728
restoreEnv("T3CODE_TELEMETRY_ENABLED", previousTelemetry);
729+
restoreEnv("OTEL_RESOURCE_ATTRIBUTES", previousResourceAttributes);
725730
}
726731
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
727732
);

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const WSL_FORWARDED_ENV_NAMES = [
110110
"OTEL_TRACES_EXPORTER",
111111
"OTEL_METRICS_EXPORTER",
112112
"OTEL_LOGS_EXPORTER",
113+
"OTEL_RESOURCE_ATTRIBUTES",
113114
] as const;
114115

115116
const WSL_SERVER_SYSTEM_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";

‎apps/mobile/src/features/observability/tracing.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Constants from "expo-constants";
2+
import { type ReleaseChannel, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
23
import * as RelayTracing from "@t3tools/shared/relayTracing";
34

45
import { hasTracingPublicConfig, resolveCloudPublicConfig } from "../cloud/publicConfig";
@@ -14,6 +15,12 @@ export interface TracingResource {
1415
readonly appVariant: string;
1516
}
1617

18+
const RELEASE_CHANNEL_BY_VARIANT: Readonly<Record<string, ReleaseChannel | undefined>> = {
19+
development: "dev",
20+
preview: "preview",
21+
production: "stable",
22+
};
23+
1724
export function resolveTracingConfig(): TracingConfig | null {
1825
const config = resolveCloudPublicConfig();
1926
if (!hasTracingPublicConfig(config)) {
@@ -24,11 +31,16 @@ export function resolveTracingConfig(): TracingConfig | null {
2431
}
2532

2633
export function layerFromConfig(config: TracingConfig | null, resource: TracingResource) {
34+
const channel =
35+
typeof __DEV__ !== "undefined" && __DEV__
36+
? "dev"
37+
: RELEASE_CHANNEL_BY_VARIANT[resource.appVariant];
2738
return RelayTracing.layer(config, {
2839
serviceName: "t3code-mobile",
2940
serviceVersion: resource.serviceVersion,
3041
runtime: "react-native",
3142
client: `mobile-${resource.appVariant}`,
43+
attributes: channel && releaseResourceAttributes(channel),
3244
});
3345
}
3446

‎apps/server/src/config.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import type { OtlpEndpointSource } from "@t3tools/contracts";
1212
import { sweepStalePendingAttachments } from "./attachmentStore.ts";
1313
import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
1414
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
15+
import { type ReleaseChannel, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
1516
import packageJson from "../package.json" with { type: "json" };
1617

18+
// Official release workflows set this telemetry channel; other builds report dev.
19+
// Unlike __T3CODE_BUILD_CHANNEL__, this is not the sign-in page's latest/nightly channel.
20+
declare const __T3CODE_TELEMETRY_CHANNEL__: ReleaseChannel | undefined;
21+
export const telemetryChannel: ReleaseChannel =
22+
typeof __T3CODE_TELEMETRY_CHANNEL__ === "undefined" ? "dev" : __T3CODE_TELEMETRY_CHANNEL__;
23+
1724
export const DEFAULT_PORT = 3773;
1825

1926
export const RuntimeMode = Schema.Literals(["web", "desktop"]);
@@ -114,6 +121,7 @@ export const otlpResource = (
114121
"service.namespace": "t3code",
115122
"service.runtime": "t3-server",
116123
"t3code.server.managed_by": config.mode === "desktop" ? "desktop" : "standalone",
124+
...releaseResourceAttributes(telemetryChannel, config.otelEnvironment.resourceAttributes),
117125
"host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,
118126
},
119127
});

‎apps/server/src/telemetry/AnalyticsService.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ const RETRY_MAX_DELAY_MS = 300_000;
5050
// These statuses reject the batch itself; unlike 408/429, retrying cannot help.
5151
const REJECTED_BATCH_STATUSES = new Set([400, 401, 413]);
5252

53-
// Official release workflows set this telemetry channel; other builds report dev.
54-
// Unlike __T3CODE_BUILD_CHANNEL__, this is not the sign-in page's latest/nightly channel.
55-
declare const __T3CODE_TELEMETRY_CHANNEL__: "stable" | "nightly" | "preview" | "dev" | undefined;
56-
5753
/** `random` is in [0, 1). */
5854
function retryDelayMs(failures: number, random: number): number {
5955
const ceiling = Math.min(RETRY_MAX_DELAY_MS, RETRY_BASE_DELAY_MS * 2 ** (failures - 1));
@@ -142,8 +138,6 @@ export const make = Effect.gen(function* () {
142138
// The background flush and the shutdown flush must not send the same batch at once.
143139
const flushLock = yield* Semaphore.make(1);
144140
const clientType = serverConfig.mode === "desktop" ? "desktop-app" : "cli-web-client";
145-
const telemetryChannel =
146-
typeof __T3CODE_TELEMETRY_CHANNEL__ === "undefined" ? "dev" : __T3CODE_TELEMETRY_CHANNEL__;
147141
const hostPlatform = yield* HostProcess.Platform;
148142
const hostArchitecture = yield* HostProcess.Architecture;
149143

@@ -198,7 +192,7 @@ export const make = Effect.gen(function* () {
198192
wsl: Option.getOrUndefined(telemetryConfig.wslDistroName),
199193
arch: hostArchitecture,
200194
t3CodeVersion: packageJson.version,
201-
telemetryChannel,
195+
telemetryChannel: ServerConfig.telemetryChannel,
202196
clientType,
203197
serverOs: serverOsFromNodePlatform(hostPlatform),
204198
serverArch: hostArchitecture,

‎apps/web/src/observability/clientTracing.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@ import { OtlpExporter, OtlpSerialization, OtlpTracer } from "effect/observabilit
88
import { settleAsyncResult, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
99
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
1010
import { resolvePrimaryEnvironmentHttpUrl } from "../environments/primary";
11+
import { releaseChannelOf, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
1112
import * as ClientTracer from "./clientTracer";
1213
import * as PrimaryEnvironmentHttpLayer from "../environments/primary/httpLayer";
1314
import { isElectron } from "../env";
1415
import { APP_VERSION } from "~/branding";
1516

1617
const DEFAULT_EXPORT_INTERVAL_MS = 1_000;
18+
// PR previews can't mark themselves in APP_VERSION without breaking the version-skew check against the server.
19+
const RELEASE_CHANNEL =
20+
import.meta.env.DEV || import.meta.env.VITE_RELEASE_CHANNEL === "dev"
21+
? "dev"
22+
: import.meta.env.VITE_RELEASE_CHANNEL === "preview"
23+
? "preview"
24+
: releaseChannelOf(APP_VERSION);
1725
const CLIENT_TRACING_RESOURCE = {
1826
serviceName: "t3code-web",
1927
attributes: {
2028
"service.namespace": "t3code",
21-
"deployment.environment.name": import.meta.env.DEV ? "development" : "production",
29+
...releaseResourceAttributes(RELEASE_CHANNEL),
2230
"service.runtime": "t3-web",
2331
"t3code.client.surface": isElectron ? "desktop" : "web",
2432
"service.version": APP_VERSION,

‎apps/web/src/vite-env.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface ImportMetaEnv {
77
readonly VITE_WS_URL: string;
88
readonly VITE_HOSTED_APP_URL: string;
99
readonly VITE_HOSTED_APP_CHANNEL: string;
10+
readonly VITE_RELEASE_CHANNEL: string;
1011
readonly VITE_CLERK_PUBLISHABLE_KEY: string;
1112
readonly VITE_CLERK_JWT_TEMPLATE: string;
1213
readonly VITE_CLERK_CLI_OAUTH_CLIENT_ID: string;

‎apps/web/vite.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const configuredRelayTracingUrl = repoEnv.VITE_RELAY_OTLP_TRACES_URL?.trim() ||
3939
const configuredRelayTracingDataset = repoEnv.VITE_RELAY_OTLP_TRACES_DATASET?.trim() || "";
4040
const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim() || "";
4141
const configuredHostedAppChannel = process.env.VITE_HOSTED_APP_CHANNEL?.trim() || "";
42+
const configuredReleaseChannel =
43+
process.env.T3CODE_BUILT_DEV === "1" ? "dev" : process.env.VITE_RELEASE_CHANNEL?.trim() || "";
4244
const configuredAppVersion = process.env.APP_VERSION?.trim() || pkg.version;
4345
const configuredDesktopUpdateRepository =
4446
process.env.T3CODE_DESKTOP_UPDATE_REPOSITORY?.trim() ||
@@ -237,6 +239,7 @@ export default defineConfig(() => {
237239
"import.meta.env.T3CODE_DESKTOP_UPDATE_REPOSITORY": JSON.stringify(
238240
configuredDesktopUpdateRepository,
239241
),
242+
"import.meta.env.VITE_RELEASE_CHANNEL": JSON.stringify(configuredReleaseChannel),
240243
"import.meta.env.APP_VERSION": JSON.stringify(configuredAppVersion),
241244
},
242245
resolve: {

0 commit comments

Comments
 (0)