TypeScript
fix(observability): trace attributes keep objects that appear twice (#23677)
Victor Vogeld18690dadc1a
diff --git a/packages/shared/src/observability.test.ts b/packages/shared/src/observability.test.tsindex f858f5e3a45..c612227993a 100644--- a/packages/shared/src/observability.test.ts+++ b/packages/shared/src/observability.test.ts@@ -214,6 +214,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<string, unknown> = { 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.tsindex 4109442553e..e3582779009 100644--- a/packages/shared/src/observability.ts+++ b/packages/shared/src/observability.ts@@ -205,15 +205,28 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {return typeof value === "object" && value !== null && !Array.isArray(value);}-function markSeen(value: object, seen: WeakSet<object>): 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<object>;+ 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<object> = new WeakSet()): unknown {+function normalizeJsonValue(value: unknown, state: NormalizeState): unknown {if (value === null ||value === undefined ||@@ -237,46 +250,59 @@ function normalizeJsonValue(value: unknown, seen: WeakSet<object> = 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<Record<string, unknown>>,): 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);