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 23, 23:00 UTC
fix(observability): a malformed OTEL_RESOURCE_ATTRIBUTES no longer stops startup (#13469)
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Yordis Prieto committed
Sep 24, 2026 at 15:14 UTC
87d84280192195b338e27acf2333771a13d04e61
4 files changed
+120
-5
apps/desktop/src/app/DesktopObservability.ts
+12
-2
@@ -356,7 +356,13 @@ const readPersistedObservabilitySettings: Effect.Effect<
356
const resolveOtlpEndpoints = Effect.gen(function* () {
357
const otel = yield* OtelEnvironment.load;
358
if (otel.disabled) {
359
- return { traces: undefined, metrics: undefined, logs: undefined, warnings: otel.warnings };
359
+ return {
360
+ traces: undefined,
361
+ metrics: undefined,
362
+ logs: undefined,
363
+ warnings: otel.warnings,
364
+ resourceAttributes: otel.resourceAttributes,
365
+ };
366
}
367
368
const environment = yield* DesktopEnvironment.DesktopEnvironment;
@@ -366,6 +372,7 @@ const resolveOtlpEndpoints = Effect.gen(function* () {
372
metrics: Option.getOrUndefined(environment.otlpMetricsUrl) ?? persisted.otlpMetricsUrl,
373
logs: Option.getOrUndefined(environment.otlpLogsUrl) ?? persisted.otlpLogsUrl,
374
warnings: otel.warnings,
375
+ resourceAttributes: otel.resourceAttributes,
376
};
377
});
378
@@ -683,7 +690,10 @@ const telemetryLayer = Layer.unwrap(
690
Effect.forEach(endpoints.warnings, (warning) => Effect.logWarning(warning)),
691
);
692
686
- return otelWarningsLayer.pipe(Layer.provideMerge(Layer.mergeAll(loggerLayer, tracerLayer)));
693
+ return otelWarningsLayer.pipe(
694
+ Layer.provideMerge(Layer.mergeAll(loggerLayer, tracerLayer)),
695
+ Layer.provide(OtelEnvironment.layerResourceAttributes(endpoints.resourceAttributes)),
696
+ );
697
}),
698
);
699
apps/server/src/observability/Layers/Observability.ts
+4
@@ -4,6 +4,7 @@ import {
4
makeTraceSink,
5
otlpSerializationLayer,
6
} from "@t3tools/shared/observability";
7
+import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
8
import * as Effect from "effect/Effect";
9
import * as Layer from "effect/Layer";
10
import * as References from "effect/References";
@@ -96,6 +97,9 @@ export const ObservabilityLive = Layer.unwrap(
97
Layer.provideMerge(
98
Layer.mergeAll(ServerLoggerLive, traceReferencesLayer, tracerLayer, metricsLayer),
99
),
100
+ Layer.provide(
101
+ OtelEnvironment.layerResourceAttributes(config.otelEnvironment.resourceAttributes),
102
+ ),
103
);
104
}),
105
);
packages/shared/src/otelEnvironment.test.ts
+49
@@ -1,6 +1,8 @@
1
import { assert, describe, it } from "@effect/vitest";
2
import * as ConfigProvider from "effect/ConfigProvider";
3
import * as Effect from "effect/Effect";
4
+import * as Layer from "effect/Layer";
5
+import * as OtlpResource from "effect/unstable/observability/OtlpResource";
6
7
import * as OtelEnvironment from "./otelEnvironment.ts";
8
@@ -79,4 +81,51 @@ describe("OtelEnvironment", () => {
81
assert.deepStrictEqual(resolved.warnings, warnings);
82
}),
83
);
84
+
85
+ it.effect.each([
86
+ { name: "unset", env: {}, resourceAttributes: {}, warnings: [] },
87
+ {
88
+ name: "a percent-encoded list",
89
+ env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,message=hello%20world" },
90
+ resourceAttributes: { team: "core", message: "hello world" },
91
+ warnings: [],
92
+ },
93
+ {
94
+ name: "a list that does not decode",
95
+ env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,broken=%zz" },
96
+ resourceAttributes: {},
97
+ warnings: [
98
+ "OTEL_RESOURCE_ATTRIBUTES is not a list of percent-encoded key=value pairs and was ignored",
99
+ ],
100
+ },
101
+ ])("resource attributes: $name", ({ env, resourceAttributes, warnings }) =>
102
+ Effect.gen(function* () {
103
+ const resolved = yield* load(env);
104
+ assert.deepStrictEqual(resolved.resourceAttributes, resourceAttributes);
105
+ assert.deepStrictEqual(resolved.warnings, warnings);
106
+ }),
107
+ );
108
+
109
+ describe("layerResourceAttributes", () => {
110
+ it.effect.each([
111
+ { name: "a list that does not decode", raw: "team=%zz", attributes: [] },
112
+ { name: "encoded separators", raw: "a%2Cb=x%3Dy", attributes: ["a,b"] },
113
+ ])("lets the exporters' own read succeed with $name", ({ raw, attributes }) =>
114
+ Effect.gen(function* () {
115
+ const env = ConfigProvider.layer(
116
+ ConfigProvider.fromEnv({ env: { OTEL_RESOURCE_ATTRIBUTES: raw } }),
117
+ );
118
+ const otel = yield* OtelEnvironment.load.pipe(Effect.provide(env));
119
+ const resource = yield* OtlpResource.fromConfig({ serviceName: "t3" }).pipe(
120
+ Effect.provide(
121
+ Layer.provide(OtelEnvironment.layerResourceAttributes(otel.resourceAttributes), env),
122
+ ),
123
+ );
124
+ assert.deepStrictEqual(
125
+ resource.attributes.map((attribute) => attribute.key),
126
+ [...attributes, "service.name"],
127
+ );
128
+ }),
129
+ );
130
+ });
131
});
packages/shared/src/otelEnvironment.ts
+55
-3
@@ -8,6 +8,7 @@
8
* @module otelEnvironment
9
*/
10
import * as Config from "effect/Config";
11
+import * as ConfigProvider from "effect/ConfigProvider";
12
import * as Effect from "effect/Effect";
13
import * as Schema from "effect/Schema";
14
import * as SchemaTransformation from "effect/SchemaTransformation";
@@ -17,6 +18,8 @@ export interface OtelEnvironment {
18
readonly disabled: boolean;
19
/** Messages for the caller to log once at startup. */
20
readonly warnings: ReadonlyArray<string>;
21
+ /** `OTEL_RESOURCE_ATTRIBUTES`, or nothing when it could not be read. */
22
+ readonly resourceAttributes: Readonly<Record<string, string>>;
23
}
24
25
interface Flag {
@@ -64,6 +67,32 @@ const flag = (
67
const T3CODE_TRUE = ["true", "yes", "on", "1", "y"];
68
const T3CODE_FALSE = ["false", "no", "off", "0", "n"];
69
70
+const RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
71
+
72
+interface ResourceAttributes {
73
+ readonly value: Readonly<Record<string, string>>;
74
+ readonly warning?: string;
75
+}
76
+
77
+// The schema Effect's OTLP exporters read this variable with.
78
+const resourceAttributes = Config.Record(
79
+ Schema.StringFromUriComponent,
80
+ Schema.StringFromUriComponent,
81
+ RESOURCE_ATTRIBUTES,
82
+).pipe(
83
+ Config.map((value): ResourceAttributes => ({ value })),
84
+ Config.orElse(() =>
85
+ Config.String(RESOURCE_ATTRIBUTES).pipe(
86
+ // The value is left out because attributes can carry credentials.
87
+ Config.map((): ResourceAttributes => ({
88
+ value: {},
89
+ warning: `${RESOURCE_ATTRIBUTES} is not a list of percent-encoded key=value pairs and was ignored`,
90
+ })),
91
+ ),
92
+ ),
93
+ Config.withDefault<ResourceAttributes>({ value: {} }),
94
+);
95
+
96
export const load: Effect.Effect<OtelEnvironment> = Config.all({
97
t3: flag(
98
"T3CODE_OTEL_SDK_DISABLED",
@@ -81,10 +110,13 @@ export const load: Effect.Effect<OtelEnvironment> = Config.all({
110
(value) =>
111
`OTEL_SDK_DISABLED=${value} was read as false; the OpenTelemetry specification recognizes only the string true, so use OTEL_SDK_DISABLED=true or T3CODE_OTEL_SDK_DISABLED to say it any other way`,
112
),
113
+ resource: resourceAttributes,
114
}).pipe(
85
- Effect.map(({ t3, spec }) => {
115
+ Effect.map(({ t3, spec, resource }) => {
116
const disabled = t3.value ?? spec.value ?? false;
87
- const warnings = [t3.warning, spec.warning].filter((warning) => warning !== undefined);
117
+ const warnings = [t3.warning, spec.warning, resource.warning].filter(
118
+ (warning) => warning !== undefined,
119
+ );
120
if (disabled) {
121
warnings.push(
122
t3.value
@@ -92,14 +124,34 @@ export const load: Effect.Effect<OtelEnvironment> = Config.all({
124
: "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway",
125
);
126
}
95
- return { disabled, warnings };
127
+ return { disabled, warnings, resourceAttributes: resource.value };
128
}),
129
// Every read above falls back instead of failing, so this cannot happen.
130
Effect.orDie,
131
);
132
133
+/**
134
+ * Provide this around Effect's OTLP exporters, which read
135
+ * `OTEL_RESOURCE_ATTRIBUTES` for themselves and die when it does not decode,
136
+ * so they see what `load` accepted instead.
137
+ */
138
+export const layerResourceAttributes = (attributes: Readonly<Record<string, string>>) =>
139
+ ConfigProvider.layerAdd(
140
+ ConfigProvider.fromEnv({
141
+ env: {
142
+ [RESOURCE_ATTRIBUTES]: Object.entries(attributes)
143
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
144
+ .join(","),
145
+ },
146
+ // Keeps an emptied list from falling through to the raw value.
147
+ preserveEmptyStrings: true,
148
+ }),
149
+ { asPrimary: true },
150
+ );
151
+
152
/** An environment that asked for nothing, for tests and for the pairing CLI. */
153
export const none: OtelEnvironment = {
154
disabled: false,
155
warnings: [],
156
+ resourceAttributes: {},
157
};