🪢 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: simplify otel input/output and accept string token values (#5777)
Steffen Schmitz591de20a7265
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 2b6fd2f17c..ed8176a765 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -361,6 +361,16 @@ describe("OTel Resource Span Mapping", () => {});it.each([+ [+ "should cast input_tokens from string to number",+ {+ entity: "observation",+ otelAttributeKey: "gen_ai.usage.input_tokens",+ otelAttributeValue: { stringValue: "15" },+ entityAttributeKey: "usageDetails.input",+ entityAttributeValue: 15,+ },+ ],["should extract environment on trace for langfuse.environment",{@@ -833,5 +843,125 @@ describe("OTel Resource Span Mapping", () => {);},);++ it.each([+ [+ "should extract input on trace from event attributes",+ {+ entity: "trace",+ otelEventName: "gen_ai.content.prompt",+ otelEventAttributeKey: "gen_ai.prompt",+ otelEventAttributeValue: {+ stringValue: "user: What is LLM Observability?",+ },+ entityAttributeKey: "input",+ entityAttributeValue: "user: What is LLM Observability?",+ },+ ],+ [+ "should extract array input on trace event attributes",+ {+ entity: "trace",+ otelEventName: "gen_ai.content.prompt",+ otelEventAttributeKey: "gen_ai.prompt",+ otelEventAttributeValue: {+ arrayValue: {+ values: [+ {+ stringValue: "Reply with the word 'java'",+ },+ ],+ },+ },+ entityAttributeKey: "input",+ entityAttributeValue: ["Reply with the word 'java'"],+ },+ ],+ [+ "should extract output on observation from event attributes",+ {+ entity: "observation",+ otelEventName: "gen_ai.content.completion",+ otelEventAttributeKey: "gen_ai.completion",+ otelEventAttributeValue: {+ stringValue:+ "assistant: LLM Observability stands for logs, metrics, and traces observability.",+ },+ entityAttributeKey: "output",+ entityAttributeValue:+ "assistant: LLM Observability stands for logs, metrics, and traces observability.",+ },+ ],+ [+ "should extract output on observation from event attributes even if no gen_ai.completion attribute is available",+ {+ entity: "observation",+ otelEventName: "gen_ai.content.completion",+ otelEventAttributeKey: "gen_ai.something_else",+ otelEventAttributeValue: {+ stringValue:+ "assistant: LLM Observability stands for logs, metrics, and traces observability.",+ },+ entityAttributeKey: "output",+ entityAttributeValue: {+ "gen_ai.something_else":+ "assistant: LLM Observability stands for logs, metrics, and traces observability.",+ },+ },+ ],+ ])(+ "Events: %s",+ (+ _name: string,+ spec: {+ entity: string;+ otelEventName: string;+ otelEventAttributeKey: string;+ otelEventAttributeValue: any;+ entityAttributeKey: string;+ entityAttributeValue: any;+ },+ ) => {+ // Setup+ const resourceSpan = {+ resource: {},+ scopeSpans: [+ {+ spans: [+ {+ ...defaultSpanProps,+ events: [+ {+ timeUnixNano: {+ low: 1327691067,+ high: 404677085,+ unsigned: true,+ },+ name: spec.otelEventName,+ attributes: [+ {+ key: spec.otelEventAttributeKey,+ value: spec.otelEventAttributeValue,+ },+ ],+ },+ ],+ },+ ],+ },+ ],+ };++ // When+ const langfuseEvents = convertOtelSpanToIngestionEvent(resourceSpan);++ // Then+ const entity: { body: Record<string, any> } =+ spec.entity === "trace" ? langfuseEvents[0] : langfuseEvents[1];+ expect(entity.body[spec.entityAttributeKey]).toEqual(+ spec.entityAttributeValue,+ );+ },+ );});});diff --git a/web/src/features/otel/server/index.ts b/web/src/features/otel/server/index.tsindex 60ed6b4ed0..8559adf7eb 100644--- a/web/src/features/otel/server/index.ts+++ b/web/src/features/otel/server/index.ts@@ -128,7 +128,21 @@ const extractInputAndOutput = (event.name === "gen_ai.content.completion",)?.attributes;if (input || output) {- return { input, output };+ input =+ input?.reduce((acc: any, attr: any) => {+ acc[attr.key] = convertValueToPlainJavascript(attr.value);+ return acc;+ }, {}) ?? {};+ output =+ output?.reduce((acc: any, attr: any) => {+ acc[attr.key] = convertValueToPlainJavascript(attr.value);+ return acc;+ }, {}) ?? {};+ // Here, we are interested in the attributes of the event. Usually gen_ai.prompt and gen_ai.completion.+ // We can use the current function again to extract them from the event attributes.+ const { input: eventInput } = extractInputAndOutput([], input);+ const { output: eventOutput } = extractInputAndOutput([], output);+ return { input: eventInput || input, output: eventOutput || output };}// MLFlow sets mlflow.spanInputs and mlflow.spanOutputs@@ -286,7 +300,11 @@ const extractUsageDetails = (.replace("llm.token_count.", "");const mappedUsageDetailKey =usageDetailKeyMapping[usageDetailKey] ?? usageDetailKey;- acc[mappedUsageDetailKey] = attributes[key];+ // Cast the respective key to a number+ const value = Number(attributes[key]);+ if (!Number.isNaN(value)) {+ acc[mappedUsageDetailKey] = value;+ }return acc;}, {});};