Skip to content

Commit 0ff0649

Browse files
Merge pull request #770 from DataDog/marcosaia/dev/improve-generate-uuid
[IMPROVEMENT] Add paddedHex and decimal format to generateUUID
2 parents f598149 + 8acbb5f commit 0ff0649

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

packages/core/src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { DdTrace } from './trace/DdTrace';
3232
import { DefaultTimeProvider } from './utils/time-provider/DefaultTimeProvider';
3333
import { TimeProvider } from './utils/time-provider/TimeProvider';
3434
import type { Timestamp } from './utils/time-provider/TimeProvider';
35+
import { TracingIdType } from './rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
3536

3637
/* eslint-enable arca/import-ordering */
3738

@@ -60,7 +61,8 @@ export {
6061
DefaultTimeProvider,
6162
DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
6263
DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
63-
DATADOG_GRAPH_QL_VARIABLES_HEADER
64+
DATADOG_GRAPH_QL_VARIABLES_HEADER,
65+
TracingIdType
6466
};
6567

6668
export type { Timestamp };

packages/core/src/rum/DdRum.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { generateErrorEventMapper } from './eventMappers/errorEventMapper';
2424
import type { ResourceEventMapper } from './eventMappers/resourceEventMapper';
2525
import { generateResourceEventMapper } from './eventMappers/resourceEventMapper';
2626
import {
27+
TracingIdFormat,
2728
TracingIdType,
2829
TracingIdentifier
2930
} from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
@@ -230,11 +231,23 @@ class DdRumWrapper implements DdRumType {
230231
};
231232

232233
generateUUID = (type: TracingIdType): string => {
233-
if (type === TracingIdType.trace) {
234-
return TracingIdentifier.createTraceId().id.toString();
234+
switch (type) {
235+
case TracingIdType.trace:
236+
return TracingIdentifier.createTraceId().toString(
237+
TracingIdFormat.paddedHex
238+
);
239+
case TracingIdType.span:
240+
return TracingIdentifier.createSpanId().toString(
241+
TracingIdFormat.decimal
242+
);
243+
default:
244+
console.warn(
245+
`Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID.`
246+
);
247+
return TracingIdentifier.createSpanId().toString(
248+
TracingIdFormat.decimal
249+
);
235250
}
236-
237-
return TracingIdentifier.createSpanId().id.toString();
238251
};
239252

240253
addError = (

packages/core/src/rum/__tests__/DdRum.test.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { ActionEventMapper } from '../eventMappers/actionEventMapper';
1818
import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
1919
import type { ResourceEventMapper } from '../eventMappers/resourceEventMapper';
2020
import { TracingIdType } from '../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
21+
import { TracingIdentifierUtils } from '../instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils';
2122
import { ErrorSource, PropagatorType, RumActionType } from '../types';
2223

2324
jest.mock('../../utils/time-provider/DefaultTimeProvider', () => {
@@ -451,17 +452,33 @@ describe('DdRum', () => {
451452
});
452453

453454
describe('DdRum.generateUUID', () => {
454-
it('generates a valid trace id in decimal format', () => {
455-
const traceUUID = DdRum.generateUUID(TracingIdType.trace);
455+
it('generates a valid trace id in paddedHex format', () => {
456+
const uuid = DdRum.generateUUID(TracingIdType.trace);
456457

457-
expect(traceUUID).toBeDefined(); // Ensure the value is defined
458-
expect(BigInt(traceUUID).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number
458+
expect(uuid).toBeDefined(); // Ensure the value is defined
459+
expect(BigInt(uuid, 16).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number
460+
expect(TracingIdentifierUtils.isWithin128Bits(uuid)).toBe(true); // Ensure the value is within 128 bits
461+
expect(uuid).toMatch(/^[0-9a-f]{32}$/); // Ensure the value is in paddedHex format
459462
});
463+
460464
it('generates a valid span id in decimal format', () => {
461-
const spanUUID = DdRum.generateUUID(TracingIdType.span);
465+
const uuid = DdRum.generateUUID(TracingIdType.span);
466+
467+
expect(uuid).toBeDefined(); // Ensure the value is defined
468+
expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number
469+
expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits
470+
expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits
471+
});
472+
473+
it('falls back to 64 bit span id when wrong tracingIdType is passed', () => {
474+
const uuid = DdRum.generateUUID(
475+
('wrong' as unknown) as TracingIdType
476+
);
462477

463-
expect(spanUUID).toBeDefined();
464-
expect(BigInt(spanUUID).greater(BigInt(0))).toBe(true);
478+
expect(uuid).toBeDefined(); // Ensure the value is defined
479+
expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number
480+
expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits
481+
expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits
465482
});
466483
});
467484

0 commit comments

Comments
 (0)