fix(observability): a malformed OTEL_RESOURCE_ATTRIBUTES no longer stops startup #13469
Mergedyordis(GitHub profile, opens in a new tab)merged 4 commits into
main fromrefs/pull/13469/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/shared/src/otelEnvironment.ts lines 97 to 97OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -66,0 +77,21 @@77+const PairComponent = Schema.String.pipe(78+ Schema.decodeTo(Schema.StringFromUriComponent, SchemaTransformation.trim()),79+);80+const decodePairComponent = Schema.decodeUnknownOption(PairComponent);81+82+interface ResourceAttributes {83+ readonly value: Readonly<Record<string, string>>;84+ readonly warnings: ReadonlyArray<string>;85+}86+87+/**88+ * Reads `OTEL_RESOURCE_ATTRIBUTES` the way the specification describes it: a89+ * comma-separated list of `key=value` pairs, percent-decoded. Effect's90+ * exporters read the same variable with `Config.Record`, which dies on the91+ * first pair it cannot decode; here, a pair that has no `=` or fails to92+ * percent-decode is dropped with a warning, and every other pair still93+ * applies, since one bad attribute should not be why the process would not94+ * start.95+ */96+const parseResourceAttributes = (raw: string): ResourceAttributes => {97+ const value: Record<string, string> = {};
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- packages/shared/src/otelEnvironment.ts lines 88 to 88OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -65,2 +68,21 @@6568 const T3CODE_FALSE = ["false", "no", "off", "0", "n"];666970+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+ Config.map((raw): ResourceAttributes => ({87+ value: {},88+ warning: `${RESOURCE_ATTRIBUTES}=${raw} is not a list of percent-encoded key=value pairs and was ignored`,
juliusmarminge reviewed
packages/shared/src/otelEnvironment.ts line 138OutdatedResolvedby yordis(GitHub profile, opens in a new tab)@@ -88,13 +119,20 @@119+ );88120 if (disabled) {89121 warnings.push(90122 t3.value91123 ? "T3CODE_OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it"92124 : "OTEL_SDK_DISABLED is set, so no telemetry is exported, whatever configured it; set T3CODE_OTEL_SDK_DISABLED=false to export anyway",93125 );94126 }95- return { disabled, warnings };127+ return { disabled, warnings, resourceAttributes: resource.value };96128 }),97129 // Every read above falls back instead of failing, so this cannot happen.98130 Effect.orDie,99131 );100132133+/**134+ * Provide this around Effect's OTLP exporters, which read135+ * `OTEL_RESOURCE_ATTRIBUTES` for themselves and die when it does not decode,136+ * so they see what `load` accepted instead.137+ */138+export const resourceAttributesLayer = (attributes: Readonly<Record<string, string>>) =>juliusmarminge reviewed
@@ -64,3 +67,20 @@6467 const T3CODE_TRUE = ["true", "yes", "on", "1", "y"];6568 const T3CODE_FALSE = ["false", "no", "off", "0", "n"];666970+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.macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this87d8428
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed