fix(observability): a malformed OTEL_RESOURCE_ATTRIBUTES no longer stops startup (#13469)

Signed-off-by: Yordis Prieto <[email protected]>
This commit is contained in:
Yordis Prieto authored and GitHub committed 2026-09-24 12:14:28 -07:00
1 parent 3412097bff
commit 87d8428019
4 files changed
+120 -5

No files matched your search

+12 -2
View File
@@ -356,7 +356,13 @@ const readPersistedObservabilitySettings: Effect.Effect<
const resolveOtlpEndpoints = Effect.gen(function* () {
const otel = yield* OtelEnvironment.load;
if (otel.disabled) {
return { traces: undefined, metrics: undefined, logs: undefined, warnings: otel.warnings };
return {
traces: undefined,
metrics: undefined,
logs: undefined,
warnings: otel.warnings,
resourceAttributes: otel.resourceAttributes,
};
}
const environment = yield* DesktopEnvironment.DesktopEnvironment;
@@ -366,6 +372,7 @@ const resolveOtlpEndpoints = Effect.gen(function* () {
metrics: Option.getOrUndefined(environment.otlpMetricsUrl) ?? persisted.otlpMetricsUrl,
logs: Option.getOrUndefined(environment.otlpLogsUrl) ?? persisted.otlpLogsUrl,
warnings: otel.warnings,
resourceAttributes: otel.resourceAttributes,
};
});
@@ -683,7 +690,10 @@ const telemetryLayer = Layer.unwrap(
Effect.forEach(endpoints.warnings, (warning) => Effect.logWarning(warning)),
);
return otelWarningsLayer.pipe(Layer.provideMerge(Layer.mergeAll(loggerLayer, tracerLayer)));
return otelWarningsLayer.pipe(
Layer.provideMerge(Layer.mergeAll(loggerLayer, tracerLayer)),
Layer.provide(OtelEnvironment.layerResourceAttributes(endpoints.resourceAttributes)),
);
}),
);
@@ -4,6 +4,7 @@ import {
makeTraceSink,
otlpSerializationLayer,
} from "@t3tools/shared/observability";
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as References from "effect/References";
@@ -96,6 +97,9 @@ export const ObservabilityLive = Layer.unwrap(
Layer.provideMerge(
Layer.mergeAll(ServerLoggerLive, traceReferencesLayer, tracerLayer, metricsLayer),
),
Layer.provide(
OtelEnvironment.layerResourceAttributes(config.otelEnvironment.resourceAttributes),
),
);
}),
);
@@ -1,6 +1,8 @@
import { assert, describe, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as OtlpResource from "effect/unstable/observability/OtlpResource";
import * as OtelEnvironment from "./otelEnvironment.ts";
@@ -79,4 +81,51 @@ describe("OtelEnvironment", () => {
assert.deepStrictEqual(resolved.warnings, warnings);
}),
);
it.effect.each([
{ name: "unset", env: {}, resourceAttributes: {}, warnings: [] },
{
name: "a percent-encoded list",
env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,message=hello%20world" },
resourceAttributes: { team: "core", message: "hello world" },
warnings: [],
},
{
name: "a list that does not decode",
env: { OTEL_RESOURCE_ATTRIBUTES: "team=core,broken=%zz" },
resourceAttributes: {},
warnings: [
"OTEL_RESOURCE_ATTRIBUTES is not a list of percent-encoded key=value pairs and was ignored",
],
},
])("resource attributes: $name", ({ env, resourceAttributes, warnings }) =>
Effect.gen(function* () {
const resolved = yield* load(env);
assert.deepStrictEqual(resolved.resourceAttributes, resourceAttributes);
assert.deepStrictEqual(resolved.warnings, warnings);
}),
);
describe("layerResourceAttributes", () => {
it.effect.each([
{ name: "a list that does not decode", raw: "team=%zz", attributes: [] },
{ name: "encoded separators", raw: "a%2Cb=x%3Dy", attributes: ["a,b"] },
])("lets the exporters' own read succeed with $name", ({ raw, attributes }) =>
Effect.gen(function* () {
const env = ConfigProvider.layer(
ConfigProvider.fromEnv({ env: { OTEL_RESOURCE_ATTRIBUTES: raw } }),
);
const otel = yield* OtelEnvironment.load.pipe(Effect.provide(env));
const resource = yield* OtlpResource.fromConfig({ serviceName: "t3" }).pipe(
Effect.provide(
Layer.provide(OtelEnvironment.layerResourceAttributes(otel.resourceAttributes), env),
),
);
assert.deepStrictEqual(
resource.attributes.map((attribute) => attribute.key),
[...attributes, "service.name"],
);
}),
);
});
});
+55 -3
View File
@@ -8,6 +8,7 @@
* @module otelEnvironment
*/
import * as Config from "effect/Config";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as SchemaTransformation from "effect/SchemaTransformation";
@@ -17,6 +18,8 @@ export interface OtelEnvironment {
readonly disabled: boolean;
/** Messages for the caller to log once at startup. */
readonly warnings: ReadonlyArray<string>;
/** `OTEL_RESOURCE_ATTRIBUTES`, or nothing when it could not be read. */
readonly resourceAttributes: Readonly<Record<string, string>>;
}
interface Flag {
@@ -64,6 +67,32 @@ const flag = (
const T3CODE_TRUE = ["true", "yes", "on", "1", "y"];
const T3CODE_FALSE = ["false", "no", "off", "0", "n"];
const RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
interface ResourceAttributes {
readonly value: Readonly<Record<string, string>>;
readonly warning?: string;
}
// The schema Effect's OTLP exporters read this variable with.
const resourceAttributes = Config.Record(
Schema.StringFromUriComponent,
Schema.StringFromUriComponent,
RESOURCE_ATTRIBUTES,
).pipe(
Config.map((value): ResourceAttributes => ({ value })),
Config.orElse(() =>
Config.String(RESOURCE_ATTRIBUTES).pipe(
// The value is left out because attributes can carry credentials.
Config.map((): ResourceAttributes => ({
value: {},
warning: `${RESOURCE_ATTRIBUTES} is not a list of percent-encoded key=value pairs and was ignored`,
})),
),
),
Config.withDefault<ResourceAttributes>({ value: {} }),
);
export const load: Effect.Effect<OtelEnvironment> = Config.all({
t3: flag(
"T3CODE_OTEL_SDK_DISABLED",
@@ -81,10 +110,13 @@ export const load: Effect.Effect<OtelEnvironment> = Config.all({
(value) =>
`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`,
),
resource: resourceAttributes,
}).pipe(
Effect.map(({ t3, spec }) => {
Effect.map(({ t3, spec, resource }) => {
const disabled = t3.value ?? spec.value ?? false;
const warnings = [t3.warning, spec.warning].filter((warning) => warning !== undefined);
const warnings = [t3.warning, spec.warning, resource.warning].filter(
(warning) => warning !== undefined,
);
if (disabled) {
warnings.push(
t3.value
@@ -92,14 +124,34 @@ export const load: Effect.Effect<OtelEnvironment> = Config.all({
: "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway",
);
}
return { disabled, warnings };
return { disabled, warnings, resourceAttributes: resource.value };
}),
// Every read above falls back instead of failing, so this cannot happen.
Effect.orDie,
);
/**
* Provide this around Effect's OTLP exporters, which read
* `OTEL_RESOURCE_ATTRIBUTES` for themselves and die when it does not decode,
* so they see what `load` accepted instead.
*/
export const layerResourceAttributes = (attributes: Readonly<Record<string, string>>) =>
ConfigProvider.layerAdd(
ConfigProvider.fromEnv({
env: {
[RESOURCE_ATTRIBUTES]: Object.entries(attributes)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join(","),
},
// Keeps an emptied list from falling through to the raw value.
preserveEmptyStrings: true,
}),
{ asPrimary: true },
);
/** An environment that asked for nothing, for tests and for the pairing CLI. */
export const none: OtelEnvironment = {
disabled: false,
warnings: [],
resourceAttributes: {},
};