Skip to content

Commit

Permalink
128 bit trace IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Jun 25, 2024
1 parent 79c58eb commit 6747dd4
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 85 deletions.
1 change: 1 addition & 0 deletions packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class DdSdkReactNative {
private static readonly DD_SDK_VERSION = '_dd.sdk_version';
private static readonly DD_VERSION = '_dd.version';
private static readonly DD_VERSION_SUFFIX = '_dd.version_suffix';

private static wasAutoInstrumented = false;
private static features?: AutoInstrumentationConfiguration;
private static _isInitialized = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { TraceIdentifier } from '../distributedTracing';
import {
TracingIdentifier,
TracingIdRepresentation
} from '../distributedTracing';

describe('TraceIdentifier', () => {
describe('TracingIdentifier', () => {
it('M return an unique identifier W toString', async () => {
// GIVEN
const generatedIds = new Set<string>();
Expand All @@ -9,63 +12,98 @@ describe('TraceIdentifier', () => {

// WHEN
while (counter-- > 0) {
generatedIds.add(new TraceIdentifier().toString(10));
generatedIds.add(
new TracingIdentifier().toString(
TracingIdRepresentation.decimal
)
);
}

// THEN
expect(generatedIds.size).toBe(iterations);
});

it('M return an 64 bits positive integer W toString', async () => {
it('M return a valid 128 bits positive integer W toString', async () => {
let iterations = 100;
while (iterations-- > 0) {
// GIVEN
const id = new TraceIdentifier().toString(10);
const id = new TracingIdentifier();
const idStr128 = id.toString(TracingIdRepresentation.decimal);

// THEN
expect(id).toMatch(/[1-9]{1,19}/);
// should be less than the max 64 bits integer
if (id.length === 19) {
expect(id < '9223372036854775807').toBeTruthy();
}
expect(idStr128).toMatch(/[1-9]{1,19}/);
expect(TracingIdentifier.is64Bits(idStr128)).toBe(false);
expect(TracingIdentifier.is128Bits(idStr128)).toBe(true);
}
});

it('M return an 64 bits positive hex W toString(16)', async () => {
it('M return a valid 64 bits low and high part integer W toString', async () => {
let iterations = 100;
while (iterations-- > 0) {
// GIVEN
const trace = new TraceIdentifier();
const id = trace.toString(16);
const paddedId = trace.toPaddedString(16, 16);
const id = new TracingIdentifier();
const idStrLow64 = id.toString(TracingIdRepresentation.lowDecimal);
const idStrHigh64 = id.highToString(10);

// THEN
expect(id).toMatch(/[1-9a-f]{1,16}/);
expect(paddedId).toMatch(/[0-9a-f]{16}/);
expect(TracingIdentifier.is64Bits(idStrLow64)).toBe(true);
expect(TracingIdentifier.is64Bits(idStrHigh64)).toBe(true);
}
});

it('M return an 64 bits positive padded hex W toPaddedString(16, 32)', async () => {
it('M return a valid timestamp in the high part of the ID w toString', () => {
const tracingId = new TracingIdentifier();
const idHex = tracingId.toString(TracingIdRepresentation.hex);
const timestamp = TracingIdentifier.extractTimestamp(idHex);

const currentUnixTime = Math.floor(Date.now() / 1000);
const fiveMinutesInSeconds = 5 * 60;

expect(timestamp).toBeGreaterThan(
currentUnixTime - fiveMinutesInSeconds
);
expect(timestamp).toBeLessThan(currentUnixTime + fiveMinutesInSeconds);
});

it('M return a 128 bits positive hex W toString(16)', async () => {
let iterations = 100;
while (iterations-- > 0) {
// GIVEN
const trace = new TracingIdentifier();
const id = trace.toString(TracingIdRepresentation.hex);
const paddedId = trace.toString(TracingIdRepresentation.hex16Chars);

// THEN
expect(id).toMatch(/^[0-9a-f]{1,32}$/);
expect(paddedId).toMatch(/^[0-9a-f]{16}$/);

expect(TracingIdentifier.is128Bits(id, 16)).toBe(true);
}
});

it('M return a 128 bits positive padded hex W toPaddedString(16, 64)', async () => {
let iterations = 100;
while (iterations-- > 0) {
// GIVEN
const id = new TraceIdentifier().toPaddedString(16, 32);
const id = new TracingIdentifier().toPaddedString(16, 64);

// THEN
expect(id).not.toMatch(/[0]{32}/);
expect(id).toMatch(/[0]{16}[0-9a-f]{16}/);
expect(id).not.toMatch(/^[0]{32}$/);
expect(id).toMatch(/^[0]{32}[0-9a-f]{32}$/);

expect(TracingIdentifier.is128Bits(id, 16)).toBe(true);
}
});

it('M return original string hex W toPaddedString(16, 10)', async () => {
let iterations = 100;
while (iterations-- > 0) {
// GIVEN
const id = new TraceIdentifier().toPaddedString(16, 10);
const id = new TracingIdentifier().toPaddedString(16, 10);

// THEN
expect(id).not.toMatch(/[0]{10}/);
expect(id).toMatch(/[0-9a-f]{10}/);
expect(id).not.toMatch(/^[0]{10}$/);
expect(id).toMatch(/^[0-9a-f]{32}$/);
}
});
});
Loading

0 comments on commit 6747dd4

Please sign in to comment.