Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUM-7629] Expose method generateUUID in DdRUM #749

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading