Skip to content

Commit d18690d

Browse files
only21milclaude
andauthored
fix(observability): trace attributes keep objects that appear twice (#23677)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 28c7d3e commit d18690d

2 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎packages/shared/src/observability.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,39 @@ describe("observability", () => {
214214
);
215215
});
216216

217+
it("keeps an object that appears twice without marking it circular", () => {
218+
const shared = { cwd: "/repo" };
219+
const sharedMap = new Map([["cwd", "/repo"]]);
220+
const sharedSet = new Set(["/repo"]);
221+
222+
assert.deepStrictEqual(
223+
compactTraceAttributes({
224+
ctx: { first: shared, second: shared },
225+
list: [shared, shared],
226+
maps: [sharedMap, sharedMap],
227+
sets: [sharedSet, sharedSet],
228+
}),
229+
{
230+
ctx: { first: { cwd: "/repo" }, second: { cwd: "/repo" } },
231+
list: [{ cwd: "/repo" }, { cwd: "/repo" }],
232+
maps: [{ cwd: "/repo" }, { cwd: "/repo" }],
233+
sets: [["/repo"], ["/repo"]],
234+
},
235+
);
236+
});
237+
238+
it("caps shared references that would expand exponentially", () => {
239+
let graph: Record<string, unknown> = { cwd: "/repo" };
240+
for (let depth = 0; depth < 20; depth++) {
241+
graph = { left: graph, right: graph };
242+
}
243+
244+
const serialized = JSON.stringify(compactTraceAttributes({ graph }));
245+
246+
assert.include(serialized, '"[Truncated]"');
247+
assert.isBelow(serialized.length, 1_000_000);
248+
});
249+
217250
nodeServicesIt("node services", (it) => {
218251
it.effect(
219252
"keeps trace producers responsive during a slow write, bounds the backlog, and drains on close",

‎packages/shared/src/observability.ts‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,28 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
205205
return typeof value === "object" && value !== null && !Array.isArray(value);
206206
}
207207

208-
function markSeen(value: object, seen: WeakSet<object>): boolean {
209-
if (seen.has(value)) {
210-
return true;
208+
// Repeated references expand once per path, so a small shared graph can grow exponentially.
209+
const TRACE_ATTRIBUTE_MAX_NODES = 10_000;
210+
211+
interface NormalizeState {
212+
// Only the current ancestors, so a value repeated under two keys is not a cycle.
213+
readonly seen: WeakSet<object>;
214+
remainingNodes: number;
215+
}
216+
217+
function enterNode(value: object, state: NormalizeState): string | undefined {
218+
if (state.seen.has(value)) {
219+
return "[Circular]";
220+
}
221+
if (state.remainingNodes <= 0) {
222+
return "[Truncated]";
211223
}
212-
seen.add(value);
213-
return false;
224+
state.remainingNodes--;
225+
state.seen.add(value);
226+
return undefined;
214227
}
215228

216-
function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet()): unknown {
229+
function normalizeJsonValue(value: unknown, state: NormalizeState): unknown {
217230
if (
218231
value === null ||
219232
value === undefined ||
@@ -237,46 +250,59 @@ function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet(
237250
};
238251
}
239252
if (Array.isArray(value)) {
240-
if (markSeen(value, seen)) {
241-
return "[Circular]";
253+
const marker = enterNode(value, state);
254+
if (marker) {
255+
return marker;
242256
}
243-
return value.map((entry) => normalizeJsonValue(entry, seen));
257+
const normalized = value.map((entry) => normalizeJsonValue(entry, state));
258+
state.seen.delete(value);
259+
return normalized;
244260
}
245261
if (value instanceof Map) {
246-
if (markSeen(value, seen)) {
247-
return "[Circular]";
262+
const marker = enterNode(value, state);
263+
if (marker) {
264+
return marker;
248265
}
249-
return Object.fromEntries(
266+
const normalized = Object.fromEntries(
250267
Array.from(value.entries(), ([key, entryValue]) => [
251268
String(key),
252-
normalizeJsonValue(entryValue, seen),
269+
normalizeJsonValue(entryValue, state),
253270
]),
254271
);
272+
state.seen.delete(value);
273+
return normalized;
255274
}
256275
if (value instanceof Set) {
257-
if (markSeen(value, seen)) {
258-
return "[Circular]";
276+
const marker = enterNode(value, state);
277+
if (marker) {
278+
return marker;
259279
}
260-
return Array.from(value.values(), (entry) => normalizeJsonValue(entry, seen));
280+
const normalized = Array.from(value.values(), (entry) => normalizeJsonValue(entry, state));
281+
state.seen.delete(value);
282+
return normalized;
261283
}
262284
if (!isPlainObject(value)) {
263285
return String(value);
264286
}
265-
if (markSeen(value, seen)) {
266-
return "[Circular]";
287+
const marker = enterNode(value, state);
288+
if (marker) {
289+
return marker;
267290
}
268-
return Object.fromEntries(
269-
Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, seen)]),
291+
const normalized = Object.fromEntries(
292+
Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, state)]),
270293
);
294+
state.seen.delete(value);
295+
return normalized;
271296
}
272297

273298
export function compactTraceAttributes(
274299
attributes: Readonly<Record<string, unknown>>,
275300
): TraceAttributes {
301+
const state: NormalizeState = { seen: new WeakSet(), remainingNodes: TRACE_ATTRIBUTE_MAX_NODES };
276302
const entries: Array<[string, unknown]> = [];
277303
for (const [key, value] of Object.entries(attributes)) {
278304
if (value !== undefined) {
279-
entries.push([key, normalizeJsonValue(value)]);
305+
entries.push([key, normalizeJsonValue(value, state)]);
280306
}
281307
}
282308
return Object.fromEntries(entries);

0 commit comments

Comments
 (0)