Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/shared/src/observability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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",
Expand Down
68 changes: 47 additions & 21 deletions packages/shared/src/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,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 ||
Expand All @@ -210,46 +223,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);
Expand Down
Loading