From 10e57ac94f4f94fae043aad1681c78c9105722fd Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Wed, 29 Jan 2025 16:55:14 +0100 Subject: [PATCH 1/8] Update generateUUID method: Add support for propragatorType and updated method parameters --- packages/core/src/rum/DdRum.ts | 56 ++++++++++++++++++++-------------- packages/core/src/rum/types.ts | 4 ++- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index 2eb62665f..f6a4e7eb6 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -26,13 +26,16 @@ import { generateResourceEventMapper } from './eventMappers/resourceEventMapper' import { TracingIdFormat, TracingIdType, - TracingIdentifier + TracingIdentifier, + type SpanId, + type TraceId } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier'; -import type { - ErrorSource, - DdRumType, - RumActionType, - ResourceKind +import { + type ErrorSource, + type DdRumType, + type RumActionType, + type ResourceKind, + PropagatorType } from './types'; const generateEmptyPromise = () => new Promise(resolve => resolve()); @@ -230,26 +233,33 @@ class DdRumWrapper implements DdRumType { ); }; - generateUUID = (type: TracingIdType): string => { - switch (type) { - case TracingIdType.trace: - return TracingIdentifier.createTraceId().toString( - TracingIdFormat.paddedHex - ); - case TracingIdType.span: - return TracingIdentifier.createSpanId().toString( - TracingIdFormat.decimal - ); - default: - console.warn( - `Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID.` - ); - return TracingIdentifier.createSpanId().toString( - TracingIdFormat.decimal - ); + generateUUID = (type: TracingIdType, propagator: PropagatorType): string => { + if (type !== TracingIdType.trace && type !== TracingIdType.span) { + console.warn( + `Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID in Decimal Format.` + ); + return TracingIdentifier.createSpanId().toString(TracingIdFormat.decimal); } + + const tempUUID = this.createUUID(type); + return this.formatUUID(tempUUID, type, propagator); }; + private createUUID(type: TracingIdType): TraceId | SpanId { + return type === TracingIdType.trace + ? TracingIdentifier.createTraceId() + : TracingIdentifier.createSpanId(); + } + + private formatUUID(id: TraceId | SpanId, type: TracingIdType, propagator: PropagatorType): string { + if (propagator === PropagatorType.DATADOG) { + return id.toString(type === TracingIdType.trace + ? TracingIdFormat.lowDecimal + : TracingIdFormat.decimal); + } + return id.toString(TracingIdFormat.paddedHex); + } + addError = ( message: string, source: ErrorSource, diff --git a/packages/core/src/rum/types.ts b/packages/core/src/rum/types.ts index 275bf2641..4f50d2e67 100644 --- a/packages/core/src/rum/types.ts +++ b/packages/core/src/rum/types.ts @@ -126,8 +126,10 @@ export type DdRumType = { /** * Generate a new unique tracing ID. * @param type - The type of the tracing ID to generate. Trace (128-bit) or Span (64-bit). + * @param propragator - The propagator to use for the generated tracing ID. Datadog, TraceContext, B3 or B3Multi. + * @returns The generated tracing ID. */ - generateUUID(type: TracingIdType): string; + generateUUID(type: TracingIdType, propagator: PropagatorType): string; /** * Add a RUM Error. From 6c35196919e6315a0e9b65092cd1002dc0064ac3 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Thu, 30 Jan 2025 10:33:59 +0100 Subject: [PATCH 2/8] Update Ddrum.test.ts: Add/update tests for generateUUID method --- packages/core/src/rum/__tests__/DdRum.test.ts | 149 ++++++++++++++---- 1 file changed, 118 insertions(+), 31 deletions(-) diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index 3f824b5a7..5cc294a46 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -451,37 +451,6 @@ describe('DdRum', () => { }); }); - describe('DdRum.generateUUID', () => { - it('generates a valid trace id in paddedHex format', () => { - const uuid = DdRum.generateUUID(TracingIdType.trace); - - expect(uuid).toBeDefined(); // Ensure the value is defined - expect(BigInt(uuid, 16).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number - expect(TracingIdentifierUtils.isWithin128Bits(uuid)).toBe(true); // Ensure the value is within 128 bits - expect(uuid).toMatch(/^[0-9a-f]{32}$/); // Ensure the value is in paddedHex format - }); - - it('generates a valid span id in decimal format', () => { - const uuid = DdRum.generateUUID(TracingIdType.span); - - expect(uuid).toBeDefined(); // Ensure the value is defined - expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number - expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits - expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits - }); - - it('falls back to 64 bit span id when wrong tracingIdType is passed', () => { - const uuid = DdRum.generateUUID( - ('wrong' as unknown) as TracingIdType - ); - - expect(uuid).toBeDefined(); // Ensure the value is defined - expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number - expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits - expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits - }); - }); - describe('DdRum.addAction', () => { test('uses given context when context is valid', async () => { const context = { @@ -1032,4 +1001,122 @@ describe('DdRum', () => { expect(PropagatorType.TRACECONTEXT).toBe('tracecontext'); }); }); + + describe('DdRum.generateUUID', () => { + describe('required types are exposed', () => { + it('PropragatorType is exposed', () => { + expect(PropagatorType).toBeDefined(); + }); + + it('TracingIdType is exposed', () => { + expect(TracingIdType).toBeDefined(); + }); + }); + + describe('generates a valid trace id', () => { + it('in lowDecimal format with Datadog propagator type', () => { + let iterations = 100; + while (iterations-- > 0) { + const uuidStrLow64 = DdRum.generateUUID( + TracingIdType.trace, + PropagatorType.DATADOG + ); + + expect(uuidStrLow64).toBeDefined(); // Ensure the value is defined + + expect( + TracingIdentifierUtils.isWithin64Bits(uuidStrLow64) + ).toBe(true); + } + }); + + it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { + let iterations = 100; + const randomPropragatorType = [ + PropagatorType.B3, + PropagatorType.B3MULTI, + PropagatorType.TRACECONTEXT + ]; + while (iterations-- > 0) { + const uuidPaddedHex = DdRum.generateUUID( + TracingIdType.trace, + randomPropragatorType[ + Math.floor( + Math.random() * randomPropragatorType.length + ) + ] + ); + + expect(uuidPaddedHex).toBeDefined(); // Ensure the value is defined + + expect(uuidPaddedHex).toMatch( + /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ + ); + expect( + TracingIdentifierUtils.isWithin128Bits( + uuidPaddedHex, + 16 + ) + ).toBe(true); + } + }); + + it('falls back to 64 bit span id when wrong tracingIdType is passed', () => { + const uuid = DdRum.generateUUID( + ('wrong' as unknown) as TracingIdType, + PropagatorType.DATADOG + ); + + expect(uuid).toBeDefined(); // Ensure the value is defined + expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number + expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits + expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits + }); + }); + + describe('generates a valid span id', () => { + it('in Decimal format with Datadog propagator type', () => { + let iterations = 100; + while (iterations-- > 0) { + const uuid6StrDecimal64 = DdRum.generateUUID( + TracingIdType.span, + PropagatorType.DATADOG + ); + + expect(uuid6StrDecimal64).toBeDefined(); // Ensure the value is defined + + expect( + TracingIdentifierUtils.isWithin64Bits(uuid6StrDecimal64) + ).toBe(true); + } + }); + + it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { + let iterations = 100; + const randomPropragatorType = [ + PropagatorType.B3, + PropagatorType.B3MULTI, + PropagatorType.TRACECONTEXT + ]; + + while (iterations-- > 0) { + const uuidStr128 = DdRum.generateUUID( + TracingIdType.span, + randomPropragatorType[ + Math.floor( + Math.random() * randomPropragatorType.length + ) + ] + ); + + expect(uuidStr128).toBeDefined(); // Ensure the value is defined + + expect(uuidStr128).toMatch(/^[0-9a-f]{16}$/); + expect( + TracingIdentifierUtils.isWithin64Bits(uuidStr128, 16) + ).toBe(true); + } + }); + }); + }); }); From fed1aedec136cfd80f3983be7f1d15c1917cde4e Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Thu, 30 Jan 2025 10:39:09 +0100 Subject: [PATCH 3/8] Update DdRum.ts: Update imports to fix lint warnings --- packages/core/src/rum/DdRum.ts | 43 ++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index f6a4e7eb6..f770cab37 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -23,19 +23,21 @@ import type { ErrorEventMapper } from './eventMappers/errorEventMapper'; import { generateErrorEventMapper } from './eventMappers/errorEventMapper'; import type { ResourceEventMapper } from './eventMappers/resourceEventMapper'; import { generateResourceEventMapper } from './eventMappers/resourceEventMapper'; +import type { + SpanId, + TraceId +} from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier'; import { TracingIdFormat, TracingIdType, - TracingIdentifier, - type SpanId, - type TraceId + TracingIdentifier } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier'; -import { - type ErrorSource, - type DdRumType, - type RumActionType, - type ResourceKind, - PropagatorType +import { PropagatorType } from './types'; +import type { + ErrorSource, + DdRumType, + RumActionType, + ResourceKind } from './types'; const generateEmptyPromise = () => new Promise(resolve => resolve()); @@ -233,12 +235,17 @@ class DdRumWrapper implements DdRumType { ); }; - generateUUID = (type: TracingIdType, propagator: PropagatorType): string => { + generateUUID = ( + type: TracingIdType, + propagator: PropagatorType + ): string => { if (type !== TracingIdType.trace && type !== TracingIdType.span) { console.warn( `Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID in Decimal Format.` ); - return TracingIdentifier.createSpanId().toString(TracingIdFormat.decimal); + return TracingIdentifier.createSpanId().toString( + TracingIdFormat.decimal + ); } const tempUUID = this.createUUID(type); @@ -251,11 +258,17 @@ class DdRumWrapper implements DdRumType { : TracingIdentifier.createSpanId(); } - private formatUUID(id: TraceId | SpanId, type: TracingIdType, propagator: PropagatorType): string { + private formatUUID( + id: TraceId | SpanId, + type: TracingIdType, + propagator: PropagatorType + ): string { if (propagator === PropagatorType.DATADOG) { - return id.toString(type === TracingIdType.trace - ? TracingIdFormat.lowDecimal - : TracingIdFormat.decimal); + return id.toString( + type === TracingIdType.trace + ? TracingIdFormat.lowDecimal + : TracingIdFormat.decimal + ); } return id.toString(TracingIdFormat.paddedHex); } From 3ab51ecefc96bc7065e49810907ac9a5c19ccecd Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Thu, 30 Jan 2025 12:21:21 +0100 Subject: [PATCH 4/8] Update generateUUID method: Return object with resource and contextPropragation UUID's for propragation --- packages/core/src/rum/DdRum.ts | 38 +++++++++++++++++++++++++++------- packages/core/src/rum/types.ts | 5 ++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index f770cab37..d77efd174 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -238,18 +238,29 @@ class DdRumWrapper implements DdRumType { generateUUID = ( type: TracingIdType, propagator: PropagatorType - ): string => { + ): + | { + resource: string; + contextPropagation: string; + } + | undefined => { if (type !== TracingIdType.trace && type !== TracingIdType.span) { console.warn( - `Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID in Decimal Format.` - ); - return TracingIdentifier.createSpanId().toString( - TracingIdFormat.decimal + `Unsupported tracing ID type '${type}' for generateUUID.` ); + return undefined; } - const tempUUID = this.createUUID(type); - return this.formatUUID(tempUUID, type, propagator); + const uuid = this.createUUID(type); + + return { + resource: this.formatUUIDForResource(uuid, type), + contextPropagation: this.formatUUIDForContextPropagation( + uuid, + type, + propagator + ) + }; }; private createUUID(type: TracingIdType): TraceId | SpanId { @@ -258,7 +269,18 @@ class DdRumWrapper implements DdRumType { : TracingIdentifier.createSpanId(); } - private formatUUID( + private formatUUIDForResource( + id: TraceId | SpanId, + type: TracingIdType + ): string { + if (type === TracingIdType.trace) { + return id.toString(TracingIdFormat.paddedHex); + } + + return id.toString(TracingIdFormat.decimal); + } + + private formatUUIDForContextPropagation( id: TraceId | SpanId, type: TracingIdType, propagator: PropagatorType diff --git a/packages/core/src/rum/types.ts b/packages/core/src/rum/types.ts index 4f50d2e67..f5b114790 100644 --- a/packages/core/src/rum/types.ts +++ b/packages/core/src/rum/types.ts @@ -129,7 +129,10 @@ export type DdRumType = { * @param propragator - The propagator to use for the generated tracing ID. Datadog, TraceContext, B3 or B3Multi. * @returns The generated tracing ID. */ - generateUUID(type: TracingIdType, propagator: PropagatorType): string; + generateUUID( + type: TracingIdType, + propagator: PropagatorType + ): { resource: string; contextPropagation: string } | undefined; /** * Add a RUM Error. From 748d10a2e9b80fd8b749f6d2e653dbfdad355d92 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Thu, 30 Jan 2025 12:23:58 +0100 Subject: [PATCH 5/8] Update generateUUID type description --- packages/core/src/rum/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/rum/types.ts b/packages/core/src/rum/types.ts index f5b114790..33b6e1fb9 100644 --- a/packages/core/src/rum/types.ts +++ b/packages/core/src/rum/types.ts @@ -127,7 +127,7 @@ export type DdRumType = { * Generate a new unique tracing ID. * @param type - The type of the tracing ID to generate. Trace (128-bit) or Span (64-bit). * @param propragator - The propagator to use for the generated tracing ID. Datadog, TraceContext, B3 or B3Multi. - * @returns The generated tracing ID. + * @returns The generated tracing ID with the resource and context propagation values. */ generateUUID( type: TracingIdType, From 5afacabd1da37bc296e2738464ea6bddd9440ae3 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Thu, 30 Jan 2025 16:32:08 +0100 Subject: [PATCH 6/8] Update generateUUID: Remove undefined and add warnings of incorrect type --- packages/core/src/rum/DdRum.ts | 21 ++++++++++++--------- packages/core/src/rum/types.ts | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index d77efd174..bd79f7313 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -238,17 +238,20 @@ class DdRumWrapper implements DdRumType { generateUUID = ( type: TracingIdType, propagator: PropagatorType - ): - | { - resource: string; - contextPropagation: string; - } - | undefined => { - if (type !== TracingIdType.trace && type !== TracingIdType.span) { + ): { + resource: string; + contextPropagation: string; + } => { + if (!Object.values(TracingIdType).includes(type)) { console.warn( - `Unsupported tracing ID type '${type}' for generateUUID.` + `Unsupported tracing ID type '${type}' provided to generateUUID. Defaulting to 'span' type.` + ); + } + + if (!Object.values(PropagatorType).includes(propagator)) { + console.warn( + `Unsupported propagator '${propagator}' provided to generateUUID. Defaulting to 'Tracecontext' propagator.` ); - return undefined; } const uuid = this.createUUID(type); diff --git a/packages/core/src/rum/types.ts b/packages/core/src/rum/types.ts index 33b6e1fb9..3771550ed 100644 --- a/packages/core/src/rum/types.ts +++ b/packages/core/src/rum/types.ts @@ -132,7 +132,7 @@ export type DdRumType = { generateUUID( type: TracingIdType, propagator: PropagatorType - ): { resource: string; contextPropagation: string } | undefined; + ): { resource: string; contextPropagation: string }; /** * Add a RUM Error. From 7f02a8f19cae82d2dd316b1c91355684ca669d35 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Fri, 31 Jan 2025 14:16:55 +0100 Subject: [PATCH 7/8] Update DdRum.test.ts: Update tests for the improved generateUUID method --- packages/core/src/rum/__tests__/DdRum.test.ts | 202 ++++++++++++++---- 1 file changed, 158 insertions(+), 44 deletions(-) diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index 5cc294a46..c5f2137e4 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -1004,7 +1004,7 @@ describe('DdRum', () => { describe('DdRum.generateUUID', () => { describe('required types are exposed', () => { - it('PropragatorType is exposed', () => { + it('PropagatorType is exposed', () => { expect(PropagatorType).toBeDefined(); }); @@ -1013,64 +1013,145 @@ describe('DdRum', () => { }); }); + describe('attributes not defined', () => { + it('falls back to 64 bit span id when wrong tracingIdType is passed', () => { + const randomPropagatorType = Object.values(PropagatorType); + + for (let i = 0; i < 100; i++) { + const tempContextPropagator = + randomPropagatorType[ + Math.floor( + Math.random() * randomPropagatorType.length + ) + ]; + + const uuid = DdRum.generateUUID( + ('wrong' as unknown) as TracingIdType, + tempContextPropagator + ); + + const uuidPropagator = uuid.contextPropagation; + + expect(uuidPropagator).toBeDefined(); // Ensure the value is defined + + // IF it's Datadog propagator, the contextPropagation should be a valid Decimal positive number + if (tempContextPropagator === PropagatorType.DATADOG) { + // Decimal + expect( + TracingIdentifierUtils.isWithin64Bits( + uuidPropagator + ) + ).toBe(true); + } else { + // IF it's not Datadog propagator, the contextPropagation should be a valid paddedHex + // Paddex + expect(uuidPropagator).toMatch(/^[0-9a-f]{16}$/); + expect( + TracingIdentifierUtils.isWithin64Bits( + uuidPropagator, + 16 + ) + ).toBe(true); + } + + const uuidDecimal = uuid.resource; + // Validate resource + expect(uuidDecimal).toBeDefined(); // Ensure the value is defined + + expect( + TracingIdentifierUtils.isWithin64Bits(uuidDecimal) + ).toBe(true); + } + }); + }); + describe('generates a valid trace id', () => { it('in lowDecimal format with Datadog propagator type', () => { let iterations = 100; while (iterations-- > 0) { - const uuidStrLow64 = DdRum.generateUUID( + const uuidObject = DdRum.generateUUID( TracingIdType.trace, PropagatorType.DATADOG ); - expect(uuidStrLow64).toBeDefined(); // Ensure the value is defined + // Test the Propagator + const uuidStrLow64Propagator = + uuidObject.contextPropagation; + + expect(uuidStrLow64Propagator).toBeDefined(); // Ensure the value is defined expect( - TracingIdentifierUtils.isWithin64Bits(uuidStrLow64) + TracingIdentifierUtils.isWithin64Bits( + uuidStrLow64Propagator + ) + ).toBe(true); + + // Test the Resource + const uuidPaddedHexResource = uuidObject.resource; + expect(uuidPaddedHexResource).toBeDefined(); // Ensure the value is defined + + expect(uuidPaddedHexResource).toMatch( + /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ + ); + expect( + TracingIdentifierUtils.isWithin128Bits( + uuidPaddedHexResource, + 16 + ) ).toBe(true); } }); it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { let iterations = 100; - const randomPropragatorType = [ - PropagatorType.B3, - PropagatorType.B3MULTI, - PropagatorType.TRACECONTEXT - ]; + + const randomPropagatorType = Object.values( + PropagatorType + ).filter(type => type !== PropagatorType.DATADOG); + while (iterations-- > 0) { - const uuidPaddedHex = DdRum.generateUUID( - TracingIdType.trace, - randomPropragatorType[ + const tempPropagatorType = + randomPropagatorType[ Math.floor( - Math.random() * randomPropragatorType.length + Math.random() * randomPropagatorType.length ) - ] + ]; + + const uuidObject = DdRum.generateUUID( + TracingIdType.trace, + tempPropagatorType ); - expect(uuidPaddedHex).toBeDefined(); // Ensure the value is defined + // Test the Propagator + const uuidPaddedHexPropagation = + uuidObject.contextPropagation; + + expect(uuidPaddedHexPropagation).toBeDefined(); // Ensure the value is defined - expect(uuidPaddedHex).toMatch( + expect(uuidPaddedHexPropagation).toMatch( /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ ); expect( TracingIdentifierUtils.isWithin128Bits( - uuidPaddedHex, + uuidPaddedHexPropagation, 16 ) ).toBe(true); - } - }); - it('falls back to 64 bit span id when wrong tracingIdType is passed', () => { - const uuid = DdRum.generateUUID( - ('wrong' as unknown) as TracingIdType, - PropagatorType.DATADOG - ); + // Test the Resource + const uuidPaddedHexResource = uuidObject.resource; + expect(uuidPaddedHexResource).toBeDefined(); // Ensure the value is defined - expect(uuid).toBeDefined(); // Ensure the value is defined - expect(BigInt(uuid).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number - expect(TracingIdentifierUtils.isWithin64Bits(uuid)).toBe(true); // Ensure the value is within 64 bits - expect(uuid).toMatch(/^[0-9]+$/); // Ensure the value contains only decimal digits + expect(uuidPaddedHexResource).toMatch( + /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ + ); + expect( + TracingIdentifierUtils.isWithin128Bits( + uuidPaddedHexResource, + 16 + ) + ).toBe(true); + } }); }); @@ -1078,42 +1159,75 @@ describe('DdRum', () => { it('in Decimal format with Datadog propagator type', () => { let iterations = 100; while (iterations-- > 0) { - const uuid6StrDecimal64 = DdRum.generateUUID( + const uuidObject = DdRum.generateUUID( TracingIdType.span, PropagatorType.DATADOG ); - expect(uuid6StrDecimal64).toBeDefined(); // Ensure the value is defined + // Test the Propagator + const uuid6StrDecimal64Propagator = uuidObject.resource; + + expect(uuid6StrDecimal64Propagator).toBeDefined(); // Ensure the value is defined expect( - TracingIdentifierUtils.isWithin64Bits(uuid6StrDecimal64) + TracingIdentifierUtils.isWithin64Bits( + uuid6StrDecimal64Propagator + ) + ).toBe(true); + + // Test the Resource + const uuid6StrDecimal64Resource = uuidObject.resource; + + expect(uuid6StrDecimal64Resource).toBeDefined(); // Ensure the value is defined + + expect( + TracingIdentifierUtils.isWithin64Bits( + uuid6StrDecimal64Resource + ) ).toBe(true); } }); it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { let iterations = 100; - const randomPropragatorType = [ - PropagatorType.B3, - PropagatorType.B3MULTI, - PropagatorType.TRACECONTEXT - ]; + const randomPropagatorType = Object.values( + PropagatorType + ).filter(type => type !== PropagatorType.DATADOG); while (iterations-- > 0) { - const uuidStr128 = DdRum.generateUUID( - TracingIdType.span, - randomPropragatorType[ + const tempPropagatorType = + randomPropagatorType[ Math.floor( - Math.random() * randomPropragatorType.length + Math.random() * randomPropagatorType.length ) - ] + ]; + + const uuidObject = DdRum.generateUUID( + TracingIdType.span, + tempPropagatorType ); - expect(uuidStr128).toBeDefined(); // Ensure the value is defined + // Test the Propagator + const uuidPaddedHexPropagation = + uuidObject.contextPropagation; - expect(uuidStr128).toMatch(/^[0-9a-f]{16}$/); + expect(uuidPaddedHexPropagation).toMatch(/^[0-9a-f]{16}$/); expect( - TracingIdentifierUtils.isWithin64Bits(uuidStr128, 16) + TracingIdentifierUtils.isWithin64Bits( + uuidPaddedHexPropagation, + 16 + ) + ).toBe(true); + + // Test the Resource (decimal) + const uuidDecimalResource = uuidObject.resource; + + expect(uuidDecimalResource).toBeDefined(); // Ensure the value is defined + + expect( + TracingIdentifierUtils.isWithin64Bits( + uuidDecimalResource + ) ).toBe(true); } }); From 58081fcfad899411f83021ab5aed8bf42ceacc59 Mon Sep 17 00:00:00 2001 From: Hugo Silva Date: Fri, 31 Jan 2025 14:23:45 +0100 Subject: [PATCH 8/8] Update DdRum.test.ts: Improve test names --- packages/core/src/rum/__tests__/DdRum.test.ts | 80 ++++++------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index c5f2137e4..93378a163 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -1001,20 +1001,19 @@ describe('DdRum', () => { expect(PropagatorType.TRACECONTEXT).toBe('tracecontext'); }); }); - describe('DdRum.generateUUID', () => { - describe('required types are exposed', () => { - it('PropagatorType is exposed', () => { + describe('Types and Enums', () => { + it('exposes PropagatorType enum', () => { expect(PropagatorType).toBeDefined(); }); - it('TracingIdType is exposed', () => { + it('exposes TracingIdType enum', () => { expect(TracingIdType).toBeDefined(); }); }); - describe('attributes not defined', () => { - it('falls back to 64 bit span id when wrong tracingIdType is passed', () => { + describe('Default behavior with invalid input', () => { + it('generates 64-bit span id when passed invalid tracing id type', () => { const randomPropagatorType = Object.values(PropagatorType); for (let i = 0; i < 100; i++) { @@ -1032,19 +1031,15 @@ describe('DdRum', () => { const uuidPropagator = uuid.contextPropagation; - expect(uuidPropagator).toBeDefined(); // Ensure the value is defined + expect(uuidPropagator).toBeDefined(); - // IF it's Datadog propagator, the contextPropagation should be a valid Decimal positive number if (tempContextPropagator === PropagatorType.DATADOG) { - // Decimal expect( TracingIdentifierUtils.isWithin64Bits( uuidPropagator ) ).toBe(true); } else { - // IF it's not Datadog propagator, the contextPropagation should be a valid paddedHex - // Paddex expect(uuidPropagator).toMatch(/^[0-9a-f]{16}$/); expect( TracingIdentifierUtils.isWithin64Bits( @@ -1055,9 +1050,7 @@ describe('DdRum', () => { } const uuidDecimal = uuid.resource; - // Validate resource - expect(uuidDecimal).toBeDefined(); // Ensure the value is defined - + expect(uuidDecimal).toBeDefined(); expect( TracingIdentifierUtils.isWithin64Bits(uuidDecimal) ).toBe(true); @@ -1065,8 +1058,8 @@ describe('DdRum', () => { }); }); - describe('generates a valid trace id', () => { - it('in lowDecimal format with Datadog propagator type', () => { + describe('Trace ID generation', () => { + it('generates valid 128-bit trace ID in decimal format for Datadog propagator', () => { let iterations = 100; while (iterations-- > 0) { const uuidObject = DdRum.generateUUID( @@ -1074,22 +1067,17 @@ describe('DdRum', () => { PropagatorType.DATADOG ); - // Test the Propagator const uuidStrLow64Propagator = uuidObject.contextPropagation; - - expect(uuidStrLow64Propagator).toBeDefined(); // Ensure the value is defined - + expect(uuidStrLow64Propagator).toBeDefined(); expect( TracingIdentifierUtils.isWithin64Bits( uuidStrLow64Propagator ) ).toBe(true); - // Test the Resource const uuidPaddedHexResource = uuidObject.resource; - expect(uuidPaddedHexResource).toBeDefined(); // Ensure the value is defined - + expect(uuidPaddedHexResource).toBeDefined(); expect(uuidPaddedHexResource).toMatch( /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ ); @@ -1102,18 +1090,18 @@ describe('DdRum', () => { } }); - it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { + it('generates valid 128-bit trace ID in hex format for B3, B3multi and TraceContext', () => { let iterations = 100; - const randomPropagatorType = Object.values( + const nonDatadogPropagators = Object.values( PropagatorType ).filter(type => type !== PropagatorType.DATADOG); while (iterations-- > 0) { const tempPropagatorType = - randomPropagatorType[ + nonDatadogPropagators[ Math.floor( - Math.random() * randomPropagatorType.length + Math.random() * nonDatadogPropagators.length ) ]; @@ -1122,12 +1110,9 @@ describe('DdRum', () => { tempPropagatorType ); - // Test the Propagator const uuidPaddedHexPropagation = uuidObject.contextPropagation; - - expect(uuidPaddedHexPropagation).toBeDefined(); // Ensure the value is defined - + expect(uuidPaddedHexPropagation).toBeDefined(); expect(uuidPaddedHexPropagation).toMatch( /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ ); @@ -1138,10 +1123,8 @@ describe('DdRum', () => { ) ).toBe(true); - // Test the Resource const uuidPaddedHexResource = uuidObject.resource; - expect(uuidPaddedHexResource).toBeDefined(); // Ensure the value is defined - + expect(uuidPaddedHexResource).toBeDefined(); expect(uuidPaddedHexResource).toMatch( /^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/ ); @@ -1155,8 +1138,8 @@ describe('DdRum', () => { }); }); - describe('generates a valid span id', () => { - it('in Decimal format with Datadog propagator type', () => { + describe('Span ID generation', () => { + it('generates valid 64-bit span ID in decimal format for Datadog propagator', () => { let iterations = 100; while (iterations-- > 0) { const uuidObject = DdRum.generateUUID( @@ -1164,22 +1147,16 @@ describe('DdRum', () => { PropagatorType.DATADOG ); - // Test the Propagator const uuid6StrDecimal64Propagator = uuidObject.resource; - - expect(uuid6StrDecimal64Propagator).toBeDefined(); // Ensure the value is defined - + expect(uuid6StrDecimal64Propagator).toBeDefined(); expect( TracingIdentifierUtils.isWithin64Bits( uuid6StrDecimal64Propagator ) ).toBe(true); - // Test the Resource const uuid6StrDecimal64Resource = uuidObject.resource; - - expect(uuid6StrDecimal64Resource).toBeDefined(); // Ensure the value is defined - + expect(uuid6StrDecimal64Resource).toBeDefined(); expect( TracingIdentifierUtils.isWithin64Bits( uuid6StrDecimal64Resource @@ -1188,17 +1165,17 @@ describe('DdRum', () => { } }); - it('in paddedHex format with B3, B3multi and TraceContext propagator types', () => { + it('generates valid 64-bit span ID in hex format for B3, B3multi and TraceContext', () => { let iterations = 100; - const randomPropagatorType = Object.values( + const nonDatadogPropagators = Object.values( PropagatorType ).filter(type => type !== PropagatorType.DATADOG); while (iterations-- > 0) { const tempPropagatorType = - randomPropagatorType[ + nonDatadogPropagators[ Math.floor( - Math.random() * randomPropagatorType.length + Math.random() * nonDatadogPropagators.length ) ]; @@ -1207,10 +1184,8 @@ describe('DdRum', () => { tempPropagatorType ); - // Test the Propagator const uuidPaddedHexPropagation = uuidObject.contextPropagation; - expect(uuidPaddedHexPropagation).toMatch(/^[0-9a-f]{16}$/); expect( TracingIdentifierUtils.isWithin64Bits( @@ -1219,11 +1194,8 @@ describe('DdRum', () => { ) ).toBe(true); - // Test the Resource (decimal) const uuidDecimalResource = uuidObject.resource; - - expect(uuidDecimalResource).toBeDefined(); // Ensure the value is defined - + expect(uuidDecimalResource).toBeDefined(); expect( TracingIdentifierUtils.isWithin64Bits( uuidDecimalResource