🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(otel): normalize environment on the direct events_full write path (#15499)
Steffen Schmitzd86d604dad45
diff --git a/packages/shared/src/server/ingestion/types.ts b/packages/shared/src/server/ingestion/types.tsindex c8aaf77506..e68db1d95c 100644--- a/packages/shared/src/server/ingestion/types.ts+++ b/packages/shared/src/server/ingestion/types.ts@@ -229,8 +229,12 @@ const PublicEnvironmentName = z.string().toLowerCase().transform((val) => {- // Strip leading "langfuse" prefix (with optional separator)- const stripped = val.replace(/^langfuse[-_]?/, "");+ // Strip all leading "langfuse" prefixes (with optional separator). The+ // repeated strip keeps normalization idempotent — values now pass through+ // this schema twice on the OTel path (extractEnvironment, then the+ // ingestion event parse) — and closes the reserved-namespace bypass where+ // "langfuselangfuse-x" survived a single-pass strip as "langfuse-x".+ const stripped = val.replace(/^(?:langfuse[-_]?)+/, "");// Truncate to 40 chars, validate allowed charsconst truncated = stripped.slice(0, 40);if (!truncated || !/^[a-z0-9-_]+$/.test(truncated)) {@@ -256,6 +260,22 @@ const InternalEnvironmentName = z/** @deprecated Use PublicEnvironmentName or InternalEnvironmentName instead */export const EnvironmentName = PublicEnvironmentName;+/**+ * Normalizes an environment value outside of a full ingestion event parse,+ * e.g. for the direct OTel events_full write path. Applies the same rules as+ * the ingestion event schemas: public values are lowercased and lose the+ * reserved "langfuse" prefix; internal values keep it so internal traces stay+ * in the "langfuse-*" namespace (see createIngestionEventSchema).+ */+export const normalizeEnvironment = (+ value: unknown,+ opts?: { isLangfuseInternal?: boolean },+): string =>+ (opts?.isLangfuseInternal+ ? InternalEnvironmentName+ : PublicEnvironmentName+ ).parse(value);+export const eventTypes = {TRACE_CREATE: "trace-create",SCORE_CREATE: "score-create",diff --git a/packages/shared/src/server/llm/ai-sdk/telemetryIntegration.test.ts b/packages/shared/src/server/llm/ai-sdk/telemetryIntegration.test.tsindex c45029535d..baa5bed97a 100644--- a/packages/shared/src/server/llm/ai-sdk/telemetryIntegration.test.ts+++ b/packages/shared/src/server/llm/ai-sdk/telemetryIntegration.test.ts@@ -170,6 +170,10 @@ describe("AI SDK telemetry integration", () => {publicKey: "",sdkName: "langfuse-internal-ai-sdk",sdkVersion: "unknown",+ // Mirrors production: publishInternalOtelSpans flags internal batches,+ // and the queue passes the flag through to the processor. Without it,+ // extractEnvironment applies the public schema and strips "langfuse-".+ isLangfuseInternal: true,});// The seen-traces dedup cache is Redis-backed; CI runs shared tests// without a Redis service, so the lookup would hang until test timeout.@@ -267,6 +271,9 @@ describe("AI SDK telemetry integration", () => {publicKey: "",sdkName: "langfuse-internal-ai-sdk",sdkVersion: "unknown",+ // Mirrors production: internal batches reach the queue processor with+ // this flag set, keeping the reserved "langfuse-*" environment intact.+ isLangfuseInternal: true,}).processToEvent(resourceSpans);const roots = eventInputs.filter((input: any) => !input.parentSpanId);diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex a1d1f0c43f..1027b4836b 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -21,6 +21,8 @@ import {UsageDetails,normalizeToolsForObservation,normalizeToolMetadataForObservation,+ normalizeEnvironment,+ DEFAULT_TRACE_ENVIRONMENT,} from "../";import { LangfuseOtelSpanAttributes } from "./attributes";@@ -2108,14 +2110,18 @@ export class OtelIngestionProcessor {for (const key of environmentAttributeKeys) {if (attributes[key]) {- return attributes[key] as string;+ return normalizeEnvironment(attributes[key], {+ isLangfuseInternal: this.isLangfuseInternal,+ });}if (resourceAttributes[key]) {- return resourceAttributes[key] as string;+ return normalizeEnvironment(resourceAttributes[key], {+ isLangfuseInternal: this.isLangfuseInternal,+ });}}- return "default";+ return DEFAULT_TRACE_ENVIRONMENT;}private extractName(diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex b7c38a1095..a5266c2688 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -3928,6 +3928,51 @@ describe("OTel Resource Span Mapping", () => {},);+ it("normalizes environment on the direct events write path (LFE-14403)", () => {+ const buildSpan = (environment: string) =>+ ({+ scopeSpans: [+ {+ spans: [+ {+ ...defaultSpanProps,+ attributes: [+ {+ key: "langfuse.environment",+ value: { stringValue: environment },+ },+ ],+ },+ ],+ },+ ],+ }) as unknown as ResourceSpan;++ // Public processors apply the public ingestion-schema rules; previously+ // the raw attribute was written to events_full ("PROD" in events_core+ // vs "prod" in the legacy tables).+ expect(+ createTestOtelProcessor().processToEvent([buildSpan("PROD")])[0]+ .environment,+ ).toBe("prod");++ // The reserved namespace cannot be entered via repeated "langfuse"+ // prefixes surviving a single-pass strip.+ expect(+ createTestOtelProcessor().processToEvent([+ buildSpan("langfuselangfuse-prompt-experiment"),+ ])[0].environment,+ ).toBe("prompt-experiment");++ // Internal processors keep the "langfuse-*" namespace so internal+ // traces stay excluded from user views and the eval-recursion guard.+ expect(+ createTestOtelProcessor({ isLangfuseInternal: true }).processToEvent([+ buildSpan("langfuse-prompt-experiment"),+ ])[0].environment,+ ).toBe("langfuse-prompt-experiment");+ });+describe("prompt linking gated to GENERATION observations", () => {const buildResourceSpan = (attributes: Record<string, any>[]) =>({diff --git a/worker/src/queues/otelIngestionQueue.ts b/worker/src/queues/otelIngestionQueue.tsindex e60bb33e30..ec0296ed18 100644--- a/worker/src/queues/otelIngestionQueue.ts+++ b/worker/src/queues/otelIngestionQueue.ts@@ -331,6 +331,7 @@ export const otelIngestionQueueProcessorBuilder = (sdkName: attribution.ingestionSdkName,sdkVersion: attribution.ingestionSdkVersion,fileKey,+ isLangfuseInternal,});const events: IngestionEventType[] =await processor.processToIngestionEvents(parsedSpans);