From 19e91203c6021e4aac42bb7103f763b04fd67c32 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 | 1 + packages/core/src/rum/DdRum.ts | 12 ++++++++++++ packages/core/src/rum/__tests__/DdRum.test.ts | 16 ++++++++++++++++ packages/core/src/rum/types.ts | 8 ++++++++ 4 files changed, 37 insertions(+) diff --git a/packages/core/jest/mock.js b/packages/core/jest/mock.js index 193c98a36..fb0d7c462 100644 --- a/packages/core/jest/mock.js +++ b/packages/core/jest/mock.js @@ -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())), diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index 9a45a5226..4ca6f2020 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -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, @@ -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, diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index c99ed3602..1a24eaca7 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -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', () => { @@ -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..275bf2641 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.