Skip to content
Clone
69f12aa
12 lines 402 B
const TRACE_SAMPLE_RATE = 0.1
function randomHex(bytes: number): string {
const buffer = new Uint8Array(bytes)
crypto.getRandomValues(buffer)
return Array.from(buffer, value => value.toString(16).padStart(2, '0')).join('')
}
export function newTraceparent(): string {
const sampled = Math.random() < TRACE_SAMPLE_RATE ? '01' : '00'
return `00-${randomHex(16)}-${randomHex(8)}-${sampled}`
}