Skip to content

Commit

Permalink
Expose method generateUUID in DdRUM
Browse files Browse the repository at this point in the history
  • Loading branch information
jhssilva authored and marco-saia-datadog committed Dec 16, 2024
1 parent d984be1 commit ebbc59c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 54 deletions.
59 changes: 30 additions & 29 deletions packages/core/jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,99 +25,100 @@ module.exports = {
DdSdkReactNative: {
initialize: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
isInitialized: jest.fn().mockImplementation(() => true),
setUser: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
addUserExtraInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
setAttributes: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
setTrackingConsent: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
telemetryDebug: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
telemetryError: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
clearAllData: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve()))
.mockImplementation(() => new Promise((resolve) => resolve())),
},

DdLogs: {
debug: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
info: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
warn: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
error: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve()))
.mockImplementation(() => new Promise((resolve) => resolve())),
},

DdTrace: {
startSpan: jest
.fn()
.mockImplementation(
() => new Promise(resolve => resolve('fakeSpanId'))
() => new Promise((resolve) => resolve('fakeSpanId'))
),
finishSpan: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve()))
.mockImplementation(() => new Promise((resolve) => resolve())),
},

DdRum: {
startView: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
stopView: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
startAction: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
stopAction: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
addAction: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
startResource: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
stopResource: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
generateUUID: jest.fn().mockImplementation(() => 'fakeUUID'),
addError: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
addTiming: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
addFeatureFlagEvaluation: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
stopSession: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
.mockImplementation(() => new Promise((resolve) => resolve())),
getCurrentSessionId: jest
.fn()
.mockImplementation(
() => new Promise(resolve => resolve('test-session-id'))
() => new Promise((resolve) => resolve('test-session-id'))
),
setTimeProvider: jest.fn().mockImplementation(() => {}),
timeProvider: jest.fn().mockReturnValue(undefined)
timeProvider: jest.fn().mockReturnValue(undefined),
},

DatadogProvider: DatadogProviderMock
};
DatadogProvider: DatadogProviderMock,
}
62 changes: 37 additions & 25 deletions packages/core/src/rum/DdRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import type {
ResourceKind
} from './types';

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

const generateEmptyPromise = () => new Promise<void>(resolve => resolve());

class DdRumWrapper implements DdRumType {
Expand Down Expand Up @@ -97,11 +99,11 @@ class DdRumWrapper implements DdRumType {
stopAction = (
...args:
| [
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
| [context?: object, timestampMs?: number]
): Promise<void> => {
InternalLog.log('Stopping current RUM Action', SdkVerbosity.DEBUG);
Expand Down Expand Up @@ -225,6 +227,16 @@ 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 Expand Up @@ -359,19 +371,19 @@ class DdRumWrapper implements DdRumType {
private getStopActionNativeCallArgs = (
args:
| [
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
| [context?: object, timestampMs?: number]
):
| [
type: RumActionType,
name: string,
context: object,
timestampMs: number
]
type: RumActionType,
name: string,
context: object,
timestampMs: number
]
| null => {
if (isNewStopActionAPI(args)) {
return [
Expand Down Expand Up @@ -412,11 +424,11 @@ class DdRumWrapper implements DdRumType {
const isNewStopActionAPI = (
args:
| [
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
| [context?: object, timestampMs?: number]
): args is [
type: RumActionType,
Expand All @@ -430,11 +442,11 @@ const isNewStopActionAPI = (
const isOldStopActionAPI = (
args:
| [
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
type: RumActionType,
name: string,
context?: object,
timestampMs?: number
]
| [context?: object, timestampMs?: number]
): args is [context?: object, timestampMs?: number] => {
return typeof args[0] === 'object' || typeof args[0] === 'undefined';
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 @@ -17,6 +17,7 @@ import type { ActionEventMapper } from '../eventMappers/actionEventMapper';
import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
import type { ResourceEventMapper } from '../eventMappers/resourceEventMapper';
import { ErrorSource, PropagatorType, RumActionType } from '../types';
import { TracingIdType } from '../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';

jest.mock('../../utils/time-provider/DefaultTimeProvider', () => {
return {
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 ebbc59c

Please sign in to comment.