🪢 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
feat: extract top-level metadata from langfuse.metadata (#6173)
Steffen Schmitz2744028f4c5b
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 77ec6f4956..e08fc55a00 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -826,6 +826,54 @@ describe("OTel Resource Span Mapping", () => {entityAttributeValue: '{"foo": "bar"}',},],+ [+ "should map langfuse.metadata string to top-level metadata for trace",+ {+ entity: "trace",+ otelAttributeKey: "langfuse.metadata",+ otelAttributeValue: {+ stringValue: '{"customer_id": "123", "experiment": "test-run-1"}',+ },+ entityAttributeKey: "metadata.customer_id",+ entityAttributeValue: "123",+ },+ ],+ [+ "should map langfuse.metadata string to top-level metadata for observation",+ {+ entity: "observation",+ otelAttributeKey: "langfuse.metadata",+ otelAttributeValue: {+ stringValue: '{"customer_id": "123", "experiment": "test-run-1"}',+ },+ entityAttributeKey: "metadata.customer_id",+ entityAttributeValue: "123",+ },+ ],+ [+ "should extract metadata from langfuse.metadata.* keys for trace",+ {+ entity: "trace",+ otelAttributeKey: "langfuse.metadata.user_type",+ otelAttributeValue: {+ stringValue: "premium",+ },+ entityAttributeKey: "metadata.user_type",+ entityAttributeValue: "premium",+ },+ ],+ [+ "should extract metadata from langfuse.metadata.* keys for observation",+ {+ entity: "observation",+ otelAttributeKey: "langfuse.metadata.user_type",+ otelAttributeValue: {+ stringValue: "premium",+ },+ entityAttributeKey: "metadata.user_type",+ entityAttributeValue: "premium",+ },+ ],])("Attributes: %s",(@@ -912,6 +960,30 @@ describe("OTel Resource Span Mapping", () => {entityAttributeValue: "default",},],+ [+ "should extract metadata from resource attributes",+ {+ entity: "observation",+ otelResourceAttributeKey: "langfuse.metadata",+ otelResourceAttributeValue: {+ stringValue: '{"resource_id": "xyz", "region": "us-west-2"}',+ },+ entityAttributeKey: "metadata.resource_id",+ entityAttributeValue: "xyz",+ },+ ],+ [+ "should extract metadata from langfuse.metadata.* resource attributes",+ {+ entity: "observation",+ otelResourceAttributeKey: "langfuse.metadata.server_name",+ otelResourceAttributeValue: {+ stringValue: "web-server-01",+ },+ entityAttributeKey: "metadata.server_name",+ entityAttributeValue: "web-server-01",+ },+ ],])("ResourceAttributes: %s",(@@ -947,9 +1019,11 @@ describe("OTel Resource Span Mapping", () => {// Thenconst entity: { body: Record<string, any> } =spec.entity === "trace" ? langfuseEvents[0] : langfuseEvents[1];- expect(entity.body[spec.entityAttributeKey]).toEqual(- spec.entityAttributeValue,- );+ expect(+ spec.entityAttributeKey // This logic allows to follow a path in the object, e.g. foo.bar.baz.+ .split(".")+ .reduce((acc: any, key: string) => acc && acc[key], entity.body),+ ).toEqual(spec.entityAttributeValue);},);diff --git a/web/src/features/otel/server/index.ts b/web/src/features/otel/server/index.tsindex d1063da68a..2623601fde 100644--- a/web/src/features/otel/server/index.ts+++ b/web/src/features/otel/server/index.ts@@ -270,6 +270,41 @@ const extractName = (return spanName;};+const extractMetadata = (+ attributes: Record<string, unknown>,+): Record<string, unknown> => {+ // Extract top-level metadata object if available+ let metadata: Record<string, unknown> = {};+ if (attributes["langfuse.metadata"]) {+ try {+ // If it's a string (JSON), parse it+ if (typeof attributes["langfuse.metadata"] === "string") {+ metadata = JSON.parse(attributes["langfuse.metadata"] as string);+ }+ // If it's already an object, use it+ else if (typeof attributes["langfuse.metadata"] === "object") {+ metadata = attributes["langfuse.metadata"] as Record<string, unknown>;+ }+ } catch (e) {+ // If parsing fails, continue with nested metadata extraction+ }+ }++ // Extract metadata from langfuse.metadata.* keys+ const metadataAttributes = Object.keys(attributes).filter((key) =>+ key.startsWith("langfuse.metadata."),+ );++ return {+ ...metadata,+ ...metadataAttributes.reduce((acc: Record<string, unknown>, key) => {+ const metadataKey = key.replace("langfuse.metadata.", "");+ acc[metadataKey] = attributes[key];+ return acc;+ }, {}),+ };+};+const extractUserId = (attributes: Record<string, unknown>,): string | undefined => {@@ -416,6 +451,8 @@ export const convertOtelSpanToIngestionEvent = (): null;+ const spanAttributeMetadata = extractMetadata(attributes);+ const resourceAttributeMetadata = extractMetadata(resourceAttributes);if (!parentObservationId) {// Create a trace for any root spanconst trace = {@@ -423,6 +460,8 @@ export const convertOtelSpanToIngestionEvent = (timestamp: convertNanoTimestampToISO(span.startTimeUnixNano),name: extractName(span.name, attributes),metadata: {+ ...resourceAttributeMetadata,+ ...spanAttributeMetadata,attributes,resourceAttributes,scope: scopeSpan?.scope,@@ -467,6 +506,8 @@ export const convertOtelSpanToIngestionEvent = (// Additional fieldsmetadata: {+ ...resourceAttributeMetadata,+ ...spanAttributeMetadata,attributes,resourceAttributes,scope: scopeSpan?.scope,