🪢 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(ingestion): store each flattened metadata path once (#18440)
Niklas Semmler679d075694b2
diff --git a/packages/shared/src/server/otel/utils.test.ts b/packages/shared/src/server/otel/utils.test.tsindex 0579c49226..df65717e63 100644--- a/packages/shared/src/server/otel/utils.test.ts+++ b/packages/shared/src/server/otel/utils.test.ts@@ -1,6 +1,10 @@import { describe, expect, it } from "vitest";-import { getOtelIdRejectionReason, validateOtelSpanIds } from "./utils";+import {+ flattenJsonToPathArrays,+ getOtelIdRejectionReason,+ validateOtelSpanIds,+} from "./utils";const TRACE_ID = "bb14c33c23138873afcc5e6f3c2b5f61"; // 16 bytesconst SPAN_ID = "cb00000daff4e5ae"; // 8 bytes@@ -350,3 +354,34 @@ describe("validateOtelSpanIds", () => {});});});++describe("flattenJsonToPathArrays", () => {+ it.each([+ {+ order: "literal dotted key first",+ metadata: {+ "db.host": "1.2.3.4",+ env: "prod",+ db: { host: "localhost" },+ },+ expected: "1.2.3.4",+ },+ {+ order: "nested object first",+ metadata: {+ db: { host: "localhost" },+ env: "prod",+ "db.host": "1.2.3.4",+ },+ expected: "localhost",+ },+ ])(+ "stores a path that a dotted key and a nested object share once, first write wins ($order)",+ ({ metadata, expected }) => {+ expect(flattenJsonToPathArrays(metadata)).toEqual({+ names: ["db.host", "env"],+ values: [expected, "prod"],+ });+ },+ );+});diff --git a/packages/shared/src/server/otel/utils.ts b/packages/shared/src/server/otel/utils.tsindex 4b31011859..8e1e2ffba5 100644--- a/packages/shared/src/server/otel/utils.ts+++ b/packages/shared/src/server/otel/utils.ts@@ -235,14 +235,28 @@ export function validateOtelSpanIds(* - values: ["baz", "42"]** All values are converted to strings for consistent storage.+ *+ * Each path is emitted once. A literal dotted key and a nested object can+ * produce the same path ({"a.b": 1, a: {b: 2}}); the first value wins, which+ * is the value metadata reads return for a duplicated path.*/export function flattenJsonToPathArrays(obj: Record<string, unknown>,prefix = "",): { names: string[]; values: Array<string | null | undefined> } {- const names: string[] = [];- const values: Array<string | null | undefined> = [];+ const flattened = new Map<string, string | null | undefined>();+ collectJsonPaths(obj, prefix, flattened);+ return {+ names: [...flattened.keys()],+ values: [...flattened.values()],+ };+}+function collectJsonPaths(+ obj: Record<string, unknown>,+ prefix: string,+ flattened: Map<string, string | null | undefined>,+): void {for (const [key, value] of Object.entries(obj)) {const path = prefix ? `${prefix}.${key}` : key;@@ -252,23 +266,17 @@ export function flattenJsonToPathArrays(typeof value === "object" &&!Array.isArray(value)) {- // Recursively flatten nested objects- const nested = flattenJsonToPathArrays(- value as Record<string, unknown>,- path,- );- names.push(...nested.names);- values.push(...nested.values);+ collectJsonPaths(value as Record<string, unknown>, path, flattened);+ } else if (flattened.has(path)) {+ continue;+ } else if (+ value === null ||+ value === undefined ||+ typeof value === "string"+ ) {+ flattened.set(path, value);} else {- // Leaf value - convert to string- names.push(path);- if (value === null || value === undefined || typeof value === "string") {- values.push(value);- } else {- values.push(JSON.stringify(value));- }+ flattened.set(path, JSON.stringify(value));}}-- return { names, values };}diff --git a/worker/src/__tests__/otelReplay.properties.test.ts b/worker/src/__tests__/otelReplay.properties.test.tsindex 6688ea0076..7dfd802695 100644--- a/worker/src/__tests__/otelReplay.properties.test.ts+++ b/worker/src/__tests__/otelReplay.properties.test.ts@@ -146,15 +146,26 @@ function expectedMetadata(seed: SpanSeed): Record<string, unknown> {}// Test-side contract oracle for the persisted flattened metadata. Arrays stay-// leaves; returning pairs preserves duplicate flattened paths.+// leaves; a path produced twice (literal-dot/nested collision) is persisted+// once with its first value.function expectedMetadataPairs(metadata: Record<string, unknown>,+): Array<[string, string]> {+ const firstValueByPath = new Map<string, string>();+ for (const [path, value] of flattenedMetadataPairs(metadata)) {+ if (!firstValueByPath.has(path)) firstValueByPath.set(path, value);+ }+ return [...firstValueByPath];+}++function flattenedMetadataPairs(+ metadata: Record<string, unknown>,prefix = "",): Array<[string, string]> {return Object.entries(metadata).flatMap(([key, value]) => {const path = prefix ? `${prefix}.${key}` : key;if (value !== null && typeof value === "object" && !Array.isArray(value)) {- return expectedMetadataPairs(value as Record<string, unknown>, path);+ return flattenedMetadataPairs(value as Record<string, unknown>, path);}const persistedValue =value === null || value === undefined