Skip to content

Commit

Permalink
chore: faster trace id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Feb 28, 2020
1 parent a34d03a commit f7b2e93
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
23 changes: 14 additions & 9 deletions packages/opentelemetry-core/src/platform/browser/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@ declare type WindowWithMsCrypto = Window & {
const cryptoLib = window.crypto || (window as WindowWithMsCrypto).msCrypto;

const SPAN_ID_BYTES = 8;
const spanBytesArray = new Uint8Array(SPAN_ID_BYTES);
const TRACE_ID_BYTES = 16;

/** Returns a random 16-byte trace ID formatted as a 32-char hex string. */
export function randomTraceId(): string {
return randomSpanId() + randomSpanId();
return randomId(TRACE_ID_BYTES);
}

/** Returns a random 8-byte span ID formatted as a 16-char hex string. */
export function randomSpanId(): string {
let spanId = '';
cryptoLib.getRandomValues(spanBytesArray);
for (let i = 0; i < SPAN_ID_BYTES; i++) {
const hexStr = spanBytesArray[i].toString(16);
return randomId(SPAN_ID_BYTES);
}

const randomBytesArray = new Uint8Array(TRACE_ID_BYTES);
function randomId(byteLength: number): string {
let id = '';
cryptoLib.getRandomValues(randomBytesArray);
for (let i = 0; i < byteLength; i++) {
const hexStr = randomBytesArray[i].toString(16);

// Zero pad bytes whose hex values are single digit.
if (hexStr.length === 1) spanId += '0';
if (hexStr.length === 1) id += '0';

spanId += hexStr;
id += hexStr;
}
return spanId;
return id;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/src/platform/node/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import * as crypto from 'crypto';

const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
*/
export function randomTraceId(): string {
return randomSpanId() + randomSpanId();
return crypto.randomBytes(TRACE_ID_BYTES).toString('hex');
}

/**
Expand Down

0 comments on commit f7b2e93

Please sign in to comment.