diff --git a/packages/shared/src/observability.test.ts b/packages/shared/src/observability.test.ts index cab62b1bc2a35..0dc3c6203c1c3 100644 --- a/packages/shared/src/observability.test.ts +++ b/packages/shared/src/observability.test.ts @@ -206,6 +206,39 @@ describe("observability", () => { ); }); + it("keeps an object that appears twice without marking it circular", () => { + const shared = { cwd: "/repo" }; + const sharedMap = new Map([["cwd", "/repo"]]); + const sharedSet = new Set(["/repo"]); + + assert.deepStrictEqual( + compactTraceAttributes({ + ctx: { first: shared, second: shared }, + list: [shared, shared], + maps: [sharedMap, sharedMap], + sets: [sharedSet, sharedSet], + }), + { + ctx: { first: { cwd: "/repo" }, second: { cwd: "/repo" } }, + list: [{ cwd: "/repo" }, { cwd: "/repo" }], + maps: [{ cwd: "/repo" }, { cwd: "/repo" }], + sets: [["/repo"], ["/repo"]], + }, + ); + }); + + it("caps shared references that would expand exponentially", () => { + let graph: Record = { cwd: "/repo" }; + for (let depth = 0; depth < 20; depth++) { + graph = { left: graph, right: graph }; + } + + const serialized = JSON.stringify(compactTraceAttributes({ graph })); + + assert.include(serialized, '"[Truncated]"'); + assert.isBelow(serialized.length, 1_000_000); + }); + nodeServicesIt("node services", (it) => { it.effect( "keeps trace producers responsive during a slow write, bounds the backlog, and drains on close", diff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.ts index 25b9b3a746c31..c0509ea20f520 100644 --- a/packages/shared/src/observability.ts +++ b/packages/shared/src/observability.ts @@ -178,15 +178,28 @@ function isPlainObject(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } -function markSeen(value: object, seen: WeakSet): boolean { - if (seen.has(value)) { - return true; +// Repeated references expand once per path, so a small shared graph can grow exponentially. +const TRACE_ATTRIBUTE_MAX_NODES = 10_000; + +interface NormalizeState { + // Only the current ancestors, so a value repeated under two keys is not a cycle. + readonly seen: WeakSet; + remainingNodes: number; +} + +function enterNode(value: object, state: NormalizeState): string | undefined { + if (state.seen.has(value)) { + return "[Circular]"; + } + if (state.remainingNodes <= 0) { + return "[Truncated]"; } - seen.add(value); - return false; + state.remainingNodes--; + state.seen.add(value); + return undefined; } -function normalizeJsonValue(value: unknown, seen: WeakSet = new WeakSet()): unknown { +function normalizeJsonValue(value: unknown, state: NormalizeState): unknown { if ( value === null || value === undefined || @@ -210,46 +223,59 @@ function normalizeJsonValue(value: unknown, seen: WeakSet = new WeakSet( }; } if (Array.isArray(value)) { - if (markSeen(value, seen)) { - return "[Circular]"; + const marker = enterNode(value, state); + if (marker) { + return marker; } - return value.map((entry) => normalizeJsonValue(entry, seen)); + const normalized = value.map((entry) => normalizeJsonValue(entry, state)); + state.seen.delete(value); + return normalized; } if (value instanceof Map) { - if (markSeen(value, seen)) { - return "[Circular]"; + const marker = enterNode(value, state); + if (marker) { + return marker; } - return Object.fromEntries( + const normalized = Object.fromEntries( Array.from(value.entries(), ([key, entryValue]) => [ String(key), - normalizeJsonValue(entryValue, seen), + normalizeJsonValue(entryValue, state), ]), ); + state.seen.delete(value); + return normalized; } if (value instanceof Set) { - if (markSeen(value, seen)) { - return "[Circular]"; + const marker = enterNode(value, state); + if (marker) { + return marker; } - return Array.from(value.values(), (entry) => normalizeJsonValue(entry, seen)); + const normalized = Array.from(value.values(), (entry) => normalizeJsonValue(entry, state)); + state.seen.delete(value); + return normalized; } if (!isPlainObject(value)) { return String(value); } - if (markSeen(value, seen)) { - return "[Circular]"; + const marker = enterNode(value, state); + if (marker) { + return marker; } - return Object.fromEntries( - Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, seen)]), + const normalized = Object.fromEntries( + Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, state)]), ); + state.seen.delete(value); + return normalized; } export function compactTraceAttributes( attributes: Readonly>, ): TraceAttributes { + const state: NormalizeState = { seen: new WeakSet(), remainingNodes: TRACE_ATTRIBUTE_MAX_NODES }; const entries: Array<[string, unknown]> = []; for (const [key, value] of Object.entries(attributes)) { if (value !== undefined) { - entries.push([key, normalizeJsonValue(value)]); + entries.push([key, normalizeJsonValue(value, state)]); } } return Object.fromEntries(entries);