🪢 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: fallback on invalid environments instead of rejecting (#12664)
Steffen Schmitz892079d8a12c
diff --git a/packages/shared/src/server/ingestion/types.ts b/packages/shared/src/server/ingestion/types.tsindex 868583f298..81da77fbfd 100644--- a/packages/shared/src/server/ingestion/types.ts+++ b/packages/shared/src/server/ingestion/types.ts@@ -221,27 +221,35 @@ export const UsageDetails = z]).nullish();-const INTERNAL_ENVIRONMENT_NAME_REGEX_ERROR_MESSAGE =- "Only alphanumeric lower case characters, hyphens, and underscores are allowed";--const ENVIRONMENT_NAME_REGEX_ERROR_MESSAGE =- INTERNAL_ENVIRONMENT_NAME_REGEX_ERROR_MESSAGE +- " and it must not start with 'langfuse'";-/** Default environment name used when no environment is specified. */export const DEFAULT_TRACE_ENVIRONMENT = "default" as const;const PublicEnvironmentName = z.string().toLowerCase()- .max(40, "Maximum length is 40 characters")- .regex(/^(?!langfuse)[a-z0-9-_]+$/, ENVIRONMENT_NAME_REGEX_ERROR_MESSAGE)+ .transform((val) => {+ // Strip leading "langfuse" prefix (with optional separator)+ const stripped = val.replace(/^langfuse[-_]?/, "");+ // Truncate to 40 chars, validate allowed chars+ const truncated = stripped.slice(0, 40);+ if (!truncated || !/^[a-z0-9-_]+$/.test(truncated)) {+ return DEFAULT_TRACE_ENVIRONMENT;+ }+ return truncated;+ })+ .catch(DEFAULT_TRACE_ENVIRONMENT).default(DEFAULT_TRACE_ENVIRONMENT);const InternalEnvironmentName = z.string()- .max(40, "Maximum length is 40 characters")- .regex(/^[a-z0-9-_]+$/, INTERNAL_ENVIRONMENT_NAME_REGEX_ERROR_MESSAGE)+ .transform((val) => {+ const truncated = val.slice(0, 40);+ if (!truncated || !/^[a-z0-9-_]+$/.test(truncated)) {+ return DEFAULT_TRACE_ENVIRONMENT;+ }+ return truncated;+ })+ .catch(DEFAULT_TRACE_ENVIRONMENT).default(DEFAULT_TRACE_ENVIRONMENT);/** @deprecated Use PublicEnvironmentName or InternalEnvironmentName instead */diff --git a/web/src/__tests__/server/ingestion-api.servertest.ts b/web/src/__tests__/server/ingestion-api.servertest.tsindex 1238f349c7..015a7ef19f 100644--- a/web/src/__tests__/server/ingestion-api.servertest.ts+++ b/web/src/__tests__/server/ingestion-api.servertest.ts@@ -616,7 +616,7 @@ describe("/api/public/ingestion API Endpoint", () => {".invalidcharacter!","incrediblylongstringwithmorethan40characters",])(- "should fail for invalid environments (%s)",+ "should fallback to default for invalid environments (%s)",async (environment: string) => {const entity = {id: randomUUID(),@@ -637,7 +637,8 @@ describe("/api/public/ingestion API Endpoint", () => {});expect(response.status).toBe(207);- expect(response.body.errors[0].status).toBe(400);+ expect(response.body.successes.length).toBe(1);+ expect(response.body.errors.length).toBe(0);},);diff --git a/web/src/__tests__/server/otel-api.servertest.ts b/web/src/__tests__/server/otel-api.servertest.tsindex fdd1ba76af..0194f9ee6e 100644--- a/web/src/__tests__/server/otel-api.servertest.ts+++ b/web/src/__tests__/server/otel-api.servertest.ts@@ -479,4 +479,89 @@ describe("/api/public/otel/v1/traces API Endpoint", () => {expect(trace!.environment).toBe("production");}, 25_000);}, 30_000);++ it.each([+ { input: "langfuse-test", expected: "test" },+ { input: "langfuse_experiment", expected: "experiment" },+ { input: "langfuse", expected: "default" },+ { input: ".invalid!", expected: "default" },+ {+ input: "incrediblylongstringwithmorethan40characters",+ expected: "incrediblylongstringwithmorethan40charac",+ },+ ])(+ "should sanitize invalid environment '$input' to '$expected'",+ async ({ input, expected }) => {+ const traceId = randomBytes(16);+ const spanId = randomBytes(8);++ const payload = {+ resourceSpans: [+ {+ resource: {+ attributes: [+ {+ key: "deployment.environment",+ value: { stringValue: input },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "langfuse-sdk",+ version: "2.60.3",+ attributes: [+ {+ key: "public_key",+ value: { stringValue: "pk-lf-1234567890" },+ },+ ],+ },+ spans: [+ {+ traceId,+ spanId,+ name: "test-span",+ kind: 1,+ startTimeUnixNano: {+ low: 466848096,+ high: 406528574,+ unsigned: true,+ },+ endTimeUnixNano: {+ low: 467248096,+ high: 406528574,+ unsigned: true,+ },+ attributes: [],+ status: {},+ },+ ],+ },+ ],+ },+ ],+ };++ const response = await makeAPICall(+ "POST",+ "/api/public/otel/v1/traces",+ payload,+ );++ expect(response.status).toBe(200);++ await waitForExpect(async () => {+ const trace = await getTraceById({+ projectId,+ traceId: traceId.toString("hex"),+ });+ expect(trace).toBeDefined();+ expect(trace!.id).toBe(traceId.toString("hex"));+ expect(trace!.environment).toBe(expected);+ }, 25_000);+ },+ 30_000,+ );});