Skip to content

Commit

Permalink
Merge pull request #749 from DataDog/hugo.silva/expose-generate-uuid
Browse files Browse the repository at this point in the history
[RUM-7629] Expose method generateUUID in DdRUM
  • Loading branch information
marco-saia-datadog authored Dec 17, 2024
2 parents dd9c158 + 19e9120 commit 2f4eee5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports = {
stopResource: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
generateUUID: jest.fn().mockImplementation(() => 'fakeUUID'),
addError: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/rum/DdRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import type { ErrorEventMapper } from './eventMappers/errorEventMapper';
import { generateErrorEventMapper } from './eventMappers/errorEventMapper';
import type { ResourceEventMapper } from './eventMappers/resourceEventMapper';
import { generateResourceEventMapper } from './eventMappers/resourceEventMapper';
import {
TracingIdType,
TracingIdentifier
} from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import type {
ErrorSource,
DdRumType,
Expand Down Expand Up @@ -225,6 +229,14 @@ class DdRumWrapper implements DdRumType {
);
};

generateUUID = (type: TracingIdType): string => {
if (type === TracingIdType.trace) {
return TracingIdentifier.createTraceId().id.toString();
}

return TracingIdentifier.createSpanId().id.toString();
};

addError = (
message: string,
source: ErrorSource,
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/rum/__tests__/DdRum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DdRum } from '../DdRum';
import type { ActionEventMapper } from '../eventMappers/actionEventMapper';
import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
import type { ResourceEventMapper } from '../eventMappers/resourceEventMapper';
import { TracingIdType } from '../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import { ErrorSource, PropagatorType, RumActionType } from '../types';

jest.mock('../../utils/time-provider/DefaultTimeProvider', () => {
Expand Down Expand Up @@ -448,6 +449,21 @@ describe('DdRum', () => {
});
});

describe('DdRum.generateUUID', () => {
it('generates a valid trace id in decimal format', () => {
const traceUUID = DdRum.generateUUID(TracingIdType.trace);

expect(traceUUID).toBeDefined(); // Ensure the value is defined
expect(BigInt(traceUUID)).toBeGreaterThan(0n); // Ensure it's a valid positive number
});
it('generates a valid span id in decimal format', () => {
const spanUUID = DdRum.generateUUID(TracingIdType.span);

expect(spanUUID).toBeDefined();
expect(BigInt(spanUUID)).toBeGreaterThan(0n);
});
});

describe('DdRum.addAction', () => {
test('uses given context when context is valid', async () => {
const context = {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/rum/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Copyright 2016-Present Datadog, Inc.
*/

import type { TracingIdType } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';

/**
* The entry point to use Datadog's RUM feature.
*/
Expand Down Expand Up @@ -121,6 +123,12 @@ export type DdRumType = {
timestampMs?: number
): Promise<void>;

/**
* Generate a new unique tracing ID.
* @param type - The type of the tracing ID to generate. Trace (128-bit) or Span (64-bit).
*/
generateUUID(type: TracingIdType): string;

/**
* Add a RUM Error.
* @param message: The error message.
Expand Down

0 comments on commit 2f4eee5

Please sign in to comment.