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
5 changes: 5 additions & 0 deletions .changeset/trace-ids-math-random.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/web": patch
---

Server trace and span ids (the W3C `traceparent` a render mints when the request carries none) are drawn from `Math.random` instead of `crypto.getRandomValues`. The ids need to be unique and well spread, not unguessable, which is also OpenTelemetry's default; the two `getRandomValues` calls cost about 10 µs per streamed render on Node, around 14% of the CPU between a request and its first chunk.
14 changes: 9 additions & 5 deletions packages/web/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,17 @@ function parseTraceparent(
return { traceId, parentId, sampled: (parseInt(flags, 16) & 1) === 1 };
}

// Trace and span ids have to be unique and well spread, not unguessable —
// W3C Trace Context asks for randomness, and OpenTelemetry's default
// IdGenerator draws them from Math.random. `crypto.getRandomValues` went
// through Node's RandomBytesJob on every render and cost ~10 µs of a
// streamed shell's ~73 µs of CPU (2026-10-16 profile); four Math.random
// words cost under a microsecond.
function randomHex(bytes: number): string {
const buffer = new Uint8Array(bytes);
crypto.getRandomValues(buffer);
let out = "";
for (let i = 0; i < buffer.length; i++)
out += (buffer[i] < 16 ? "0" : "") + buffer[i].toString(16);
return out;
for (let i = 0; i < bytes; i += 4)
out += ((Math.random() * 0x100000000) >>> 0).toString(16).padStart(8, "0");
return out.length === bytes * 2 ? out : out.slice(0, bytes * 2);
}

function randomTraceId(): string {
Expand Down
Loading