Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: map otel environment config to langfuse environment (#5757)

Steffen Schmitzc5fc30a9d09e
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index a25bd12675..2b6fd2f17c 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -361,6 +361,36 @@ describe("OTel Resource Span Mapping", () => {
});
it.each([
+ [
+ "should extract environment on trace for langfuse.environment",
+ {
+ entity: "trace",
+ otelAttributeKey: "langfuse.environment",
+ otelAttributeValue: { stringValue: "test" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "test",
+ },
+ ],
+ [
+ "should extract environment on observation for deployment.environment.name",
+ {
+ entity: "observation",
+ otelAttributeKey: "deployment.environment.name",
+ otelAttributeValue: { stringValue: "test" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "test",
+ },
+ ],
+ [
+ "should fallback to default on observation if no environment present",
+ {
+ entity: "observation",
+ otelAttributeKey: "unused.key",
+ otelAttributeValue: { stringValue: "" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "default",
+ },
+ ],
[
"should extract promptName on observation from langfuse.prompt.name",
{
@@ -733,6 +763,36 @@ describe("OTel Resource Span Mapping", () => {
entityAttributeValue: "1.0.5",
},
],
+ [
+ "should extract environment on trace for langfuse.environment",
+ {
+ entity: "trace",
+ otelResourceAttributeKey: "langfuse.environment",
+ otelResourceAttributeValue: { stringValue: "test" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "test",
+ },
+ ],
+ [
+ "should extract environment on observation for deployment.environment.name",
+ {
+ entity: "observation",
+ otelResourceAttributeKey: "deployment.environment.name",
+ otelResourceAttributeValue: { stringValue: "test" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "test",
+ },
+ ],
+ [
+ "should fallback to default on observation if no environment present",
+ {
+ entity: "observation",
+ otelResourceAttributeKey: "unused.key",
+ otelResourceAttributeValue: { stringValue: "" },
+ entityAttributeKey: "environment",
+ entityAttributeValue: "default",
+ },
+ ],
])(
"ResourceAttributes: %s",
(
diff --git a/web/src/features/otel/server/index.ts b/web/src/features/otel/server/index.ts
index 823eb23b0f..60ed6b4ed0 100644
--- a/web/src/features/otel/server/index.ts
+++ b/web/src/features/otel/server/index.ts
@@ -177,6 +177,26 @@ const extractInputAndOutput = (
return { input: null, output: null };
};
+const extractEnvironment = (
+ attributes: Record<string, unknown>,
+ resourceAttributes: Record<string, unknown>,
+): string => {
+ const environmentAttributeKeys = [
+ "langfuse.environment",
+ "deployment.environment.name",
+ "deployment.environment",
+ ];
+ for (const key of environmentAttributeKeys) {
+ if (resourceAttributes[key]) {
+ return resourceAttributes[key] as string;
+ }
+ if (attributes[key]) {
+ return attributes[key] as string;
+ }
+ }
+ return "default";
+};
+
const extractUserId = (
attributes: Record<string, unknown>,
): string | undefined => {
@@ -334,8 +354,7 @@ export const convertOtelSpanToIngestionEvent = (
attributes?.["langfuse.public"] === "true",
tags: attributes?.["langfuse.tags"] ?? [],
- // TODO: Overwrite to OTel based mapping via LFE-4081
- environment: "default",
+ environment: extractEnvironment(attributes, resourceAttributes),
// Input and Output
...extractInputAndOutput(span?.events ?? [], attributes),
@@ -359,8 +378,7 @@ export const convertOtelSpanToIngestionEvent = (
startTime: convertNanoTimestampToISO(span.startTimeUnixNano),
endTime: convertNanoTimestampToISO(span.endTimeUnixNano),
- // TODO: Overwrite to OTel based mapping via LFE-4081
- environment: "default",
+ environment: extractEnvironment(attributes, resourceAttributes),
// Additional fields
metadata: {