From ebbc59c3659f977274ab4b945f6dae4afd6591e0 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Mon, 9 Dec 2024 18:05:05 +0100 Subject: [PATCH] Expose method generateUUID in DdRUM --- packages/core/jest/mock.js | 59 +++++++++--------- packages/core/src/rum/DdRum.ts | 62 +++++++++++-------- packages/core/src/rum/__tests__/DdRum.test.ts | 16 +++++ packages/core/src/rum/types.ts | 8 +++ 4 files changed, 91 insertions(+), 54 deletions(-) diff --git a/packages/core/jest/mock.js b/packages/core/jest/mock.js index 193c98a36..fcd6c880d 100644 --- a/packages/core/jest/mock.js +++ b/packages/core/jest/mock.js @@ -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, +} diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index 9a45a5226..dbd88f794 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -30,6 +30,8 @@ import type { ResourceKind } from './types'; +import { TracingIdType, TracingIdentifier } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier'; + const generateEmptyPromise = () => new Promise(resolve => resolve()); class DdRumWrapper implements DdRumType { @@ -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 => { InternalLog.log('Stopping current RUM Action', SdkVerbosity.DEBUG); @@ -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, @@ -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 [ @@ -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, @@ -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'; diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index c99ed3602..32470abdf 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -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 { @@ -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 = { diff --git a/packages/core/src/rum/types.ts b/packages/core/src/rum/types.ts index af9d96357..4960fe074 100644 --- a/packages/core/src/rum/types.ts +++ b/packages/core/src/rum/types.ts @@ -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. */ @@ -121,6 +123,12 @@ export type DdRumType = { timestampMs?: number ): Promise; + /** + * 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.