Skip to content

Commit 06bc9e4

Browse files
committed
fix(observability): every app reports its own environment and release channel
Signed-off-by: Yordis Prieto <[email protected]>
1 parent d14ace8 commit 06bc9e4

19 files changed

Lines changed: 400 additions & 20 deletions

‎.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.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,43 @@ describe("DesktopObservability", () => {
281281
),
282282
);
283283

284+
it.effect("reports its own release channel and keeps an operator environment", () => {
285+
const requests: Array<ExportedRequest> = [];
286+
return Effect.gen(function* () {
287+
const fileSystem = yield* FileSystem.FileSystem;
288+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
289+
prefix: "t3-desktop-observability-test-",
290+
});
291+
const layerEnvironment = layerEnvironmentFor(baseDir, true, {
292+
T3CODE_OTLP_LOGS_URL: "https://collector.example.com/v1/logs",
293+
});
294+
295+
yield* Effect.scoped(
296+
Effect.logInfo("desktop operator environment").pipe(
297+
Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
298+
),
299+
);
300+
301+
assert.lengthOf(requests, 1);
302+
const body = requests[0]?.body ?? "";
303+
assert.include(body, '"key":"deployment.environment.name","value":{"stringValue":"staging"}');
304+
assert.include(body, '"key":"t3code.release.channel","value":{"stringValue":"dev"}');
305+
}).pipe(
306+
Effect.scoped,
307+
Effect.provide(
308+
Layer.mergeAll(
309+
NodeServices.layer,
310+
layerCollector(requests),
311+
ConfigProvider.layer(
312+
ConfigProvider.fromEnv({
313+
env: { OTEL_RESOURCE_ATTRIBUTES: "deployment.environment.name=staging" },
314+
}),
315+
),
316+
),
317+
),
318+
);
319+
});
320+
284321
it.effect("stops every export when the OpenTelemetry SDK is disabled", () => {
285322
const requests: Array<ExportedRequest> = [];
286323
return Effect.gen(function* () {
@@ -314,6 +351,36 @@ describe("DesktopObservability", () => {
314351
);
315352
});
316353

354+
it.effect("reports the app's own release identity for a non-development run", () => {
355+
const requests: Array<ExportedRequest> = [];
356+
return Effect.gen(function* () {
357+
const fileSystem = yield* FileSystem.FileSystem;
358+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
359+
prefix: "t3-desktop-observability-test-",
360+
});
361+
const layerEnvironment = layerEnvironmentFor(baseDir, false);
362+
yield* writeObservabilitySettings(layerEnvironment, {
363+
otlpLogsUrl: "https://settings.example.com/v1/logs",
364+
});
365+
366+
yield* Effect.scoped(
367+
Effect.logInfo("desktop identity under test").pipe(
368+
Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
369+
),
370+
);
371+
372+
const body = requests.map((request) => request.body).join("\n");
373+
assert.include(
374+
body,
375+
'"key":"deployment.environment.name","value":{"stringValue":"production"}',
376+
);
377+
assert.include(body, '"key":"t3code.release.channel","value":{"stringValue":"stable"}');
378+
}).pipe(
379+
Effect.scoped,
380+
Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),
381+
);
382+
});
383+
317384
it.effect("reports the app's own version for an unpackaged desktop run", () => {
318385
const requests: Array<ExportedRequest> = [];
319386
return Effect.gen(function* () {

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

Lines changed: 11 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 { releaseResourceAttributes } from "@t3tools/shared/releaseResource";
1011
import {
1112
parsePersistedServerObservabilitySettings,
1213
type PersistedServerObservabilitySettings,
@@ -597,14 +598,22 @@ const layerTelemetry = Layer.unwrap(
597598
Effect.gen(function* () {
598599
const environment = yield* DesktopEnvironment.DesktopEnvironment;
599600
const endpoints = yield* resolveOtlpEndpoints;
601+
// Unpackaged runs launch a script, so Electron reports its own version.
602+
const serviceVersion = environment.isPackaged
603+
? environment.appVersion
604+
: desktopPackageJson.version;
600605
const resource = {
601606
serviceName: "t3code-desktop",
602-
// Unpackaged runs launch a script, so Electron reports its own version.
603-
serviceVersion: environment.isPackaged ? environment.appVersion : desktopPackageJson.version,
607+
serviceVersion,
604608
attributes: {
605609
"service.namespace": "t3code",
606610
"service.runtime": "desktop",
607611
"service.mode": environment.isDevelopment ? "development" : "packaged",
612+
...releaseResourceAttributes({
613+
version: serviceVersion,
614+
operatorAttributes: endpoints.resourceAttributes,
615+
isDevelopment: environment.isDevelopment,
616+
}),
608617
"host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname),
609618
},
610619
};

‎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
@@ -113,6 +113,7 @@ const WSL_FORWARDED_ENV_NAMES = [
113113
"OTEL_TRACES_EXPORTER",
114114
"OTEL_METRICS_EXPORTER",
115115
"OTEL_LOGS_EXPORTER",
116+
"OTEL_RESOURCE_ATTRIBUTES",
116117
] as const;
117118

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

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,63 @@ vi.mock("expo-constants", () => ({
1717
},
1818
}));
1919

20+
const exportedResourceBody = (appVariant: string) => {
21+
const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
22+
const layerTracing = Tracing.layerFromConfig(
23+
{
24+
tracesUrl: "https://api.axiom.test/v1/traces",
25+
tracesDataset: "mobile-traces",
26+
tracesToken: "public-ingest-token",
27+
},
28+
{
29+
appVariant,
30+
serviceVersion: "1.2.3",
31+
},
32+
).pipe(Layer.provide(layerRemoteHttpClient(fetchFn)));
33+
const layerTracedApplication = Layer.effectDiscard(
34+
Effect.void.pipe(Effect.withSpan("mobile.test.span"), withRelayClientTracing),
35+
).pipe(Layer.provide(layerTracing));
36+
37+
return Layer.build(layerTracedApplication).pipe(
38+
Effect.scoped,
39+
Effect.andThen(
40+
Effect.sync(() => {
41+
expect(fetchFn).toHaveBeenCalledOnce();
42+
return new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
43+
}),
44+
),
45+
);
46+
};
47+
48+
const stringAttribute = (key: string, value: string) =>
49+
`"key":"${key}","value":{"stringValue":"${value}"}`;
50+
51+
it.effect.each([
52+
["development", "development", "dev"],
53+
["preview", "staging", "preview"],
54+
["production", "production", "stable"],
55+
] as const)("reports %s builds as %s on the %s channel", ([appVariant, environment, channel]) =>
56+
exportedResourceBody(appVariant).pipe(
57+
Effect.andThen((body) =>
58+
Effect.sync(() => {
59+
expect(body).toContain(stringAttribute("deployment.environment.name", environment));
60+
expect(body).toContain(stringAttribute("t3code.release.channel", channel));
61+
}),
62+
),
63+
),
64+
);
65+
66+
it.effect("omits environment and channel for an unknown variant", () =>
67+
exportedResourceBody("unknown").pipe(
68+
Effect.andThen((body) =>
69+
Effect.sync(() => {
70+
expect(body).not.toContain("deployment.environment.name");
71+
expect(body).not.toContain("t3code.release.channel");
72+
}),
73+
),
74+
),
75+
);
76+
2077
it.effect("does not let OTLP serialization failures alter application effects", () => {
2178
const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
2279
const layerTracing = Tracing.layerFromConfig(

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

Lines changed: 16 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 } from "@t3tools/shared/releaseResource";
23
import * as RelayTracing from "@t3tools/shared/relayTracing";
34

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

18+
const RELEASE_IDENTITY_BY_VARIANT: Readonly<
19+
Record<string, { readonly environment: string; readonly channel: ReleaseChannel }>
20+
> = {
21+
development: { environment: "development", channel: "dev" },
22+
preview: { environment: "staging", channel: "preview" },
23+
production: { environment: "production", channel: "stable" },
24+
};
25+
1726
export function resolveTracingConfig(): TracingConfig | null {
1827
const config = resolveCloudPublicConfig();
1928
if (!hasTracingPublicConfig(config)) {
@@ -24,11 +33,18 @@ export function resolveTracingConfig(): TracingConfig | null {
2433
}
2534

2635
export function layerFromConfig(config: TracingConfig | null, resource: TracingResource) {
36+
const releaseIdentity = Object.hasOwn(RELEASE_IDENTITY_BY_VARIANT, resource.appVariant)
37+
? RELEASE_IDENTITY_BY_VARIANT[resource.appVariant]
38+
: undefined;
2739
return RelayTracing.layer(config, {
2840
serviceName: "t3code-mobile",
2941
serviceVersion: resource.serviceVersion,
3042
runtime: "react-native",
3143
client: `mobile-${resource.appVariant}`,
44+
attributes: releaseIdentity && {
45+
"deployment.environment.name": releaseIdentity.environment,
46+
"t3code.release.channel": releaseIdentity.channel,
47+
},
3248
});
3349
}
3450

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
2+
import { assert, describe, it } from "@effect/vitest";
3+
4+
import packageJson from "../package.json" with { type: "json" };
5+
import * as ServerConfig from "./config.ts";
6+
7+
describe("otlpResource", () => {
8+
const resource = (
9+
resourceAttributes: Record<string, string>,
10+
overrides: Partial<Pick<ServerConfig.ServerConfig["Service"], "mode" | "devUrl">> = {},
11+
) =>
12+
ServerConfig.otlpResource(
13+
{
14+
mode: "web",
15+
devUrl: undefined,
16+
otelEnvironment: { ...OtelEnvironment.none, resourceAttributes },
17+
...overrides,
18+
},
19+
"box-1",
20+
);
21+
22+
it("reports the server's own version, release channel and environment", () => {
23+
const attributes: Readonly<Record<string, unknown>> = resource({}).attributes;
24+
assert.equal(resource({}).serviceVersion, packageJson.version);
25+
assert.equal(attributes["t3code.release.channel"], "stable");
26+
assert.equal(attributes["deployment.environment.name"], "production");
27+
28+
const dev: Readonly<Record<string, unknown>> = resource(
29+
{},
30+
{ devUrl: new URL("http://localhost:5733") },
31+
).attributes;
32+
assert.equal(dev["t3code.release.channel"], "dev");
33+
assert.equal(dev["deployment.environment.name"], "development");
34+
35+
const operator: Readonly<Record<string, unknown>> = resource({
36+
"t3code.release.channel": "nightly",
37+
"deployment.environment.name": "staging",
38+
}).attributes;
39+
assert.equal(operator["t3code.release.channel"], "nightly");
40+
assert.equal(operator["deployment.environment.name"], "staging");
41+
});
42+
});

‎apps/server/src/config.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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 { releaseResourceAttributes } from "@t3tools/shared/releaseResource";
1516
import packageJson from "../package.json" with { type: "json" };
1617

1718
export const DEFAULT_PORT = 3773;
@@ -103,7 +104,7 @@ export class ServerConfig extends Context.Service<
103104
export const make = (config: ServerConfig["Service"]) => ServerConfig.of(config);
104105

105106
export const otlpResource = (
106-
config: Pick<ServerConfig["Service"], "mode" | "otelEnvironment">,
107+
config: Pick<ServerConfig["Service"], "mode" | "devUrl" | "otelEnvironment">,
107108
hostName: string,
108109
) => ({
109110
serviceName: "t3code-server",
@@ -112,6 +113,11 @@ export const otlpResource = (
112113
"service.namespace": "t3code",
113114
"service.runtime": "t3-server",
114115
"t3code.server.managed_by": config.mode === "desktop" ? "desktop" : "standalone",
116+
...releaseResourceAttributes({
117+
version: packageJson.version,
118+
operatorAttributes: config.otelEnvironment.resourceAttributes,
119+
isDevelopment: config.devUrl !== undefined,
120+
}),
115121
"host.name": config.otelEnvironment.resourceAttributes["host.name"] || hostName,
116122
},
117123
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { clientTracingResource } from "./clientResource";
4+
5+
const resource = (overrides: Partial<Parameters<typeof clientTracingResource>[0]> = {}) =>
6+
clientTracingResource({
7+
version: "1.2.3",
8+
isDevelopment: false,
9+
releaseChannel: undefined,
10+
surface: "web",
11+
...overrides,
12+
});
13+
14+
describe("clientTracingResource", () => {
15+
it.each([
16+
["a dev build", { isDevelopment: true }, "development", "dev"],
17+
["a stable build", {}, "production", "stable"],
18+
["an unrecognized channel", { releaseChannel: "unrecognized" }, "production", "stable"],
19+
["a nightly build", { version: "1.2.3-nightly.20261023.4" }, "production", "nightly"],
20+
["a PR preview", { releaseChannel: "preview" }, "staging", "preview"],
21+
["a preview version", { version: "1.2.3-preview.20261023.1" }, "staging", "preview"],
22+
] as const)("reports %s as %s on the %s channel", (_name, overrides, environment, channel) => {
23+
const { attributes } = resource(overrides);
24+
expect(attributes["deployment.environment.name"]).toBe(environment);
25+
expect(attributes["t3code.release.channel"]).toBe(channel);
26+
});
27+
28+
it("keeps the version untouched so the server version comparison still works", () => {
29+
expect(resource({ releaseChannel: "preview" }).attributes["service.version"]).toBe("1.2.3");
30+
});
31+
});

0 commit comments

Comments
 (0)