From cec6f513a512b4b950d0c345991e4ad6e083babe Mon Sep 17 00:00:00 2001 From: "Xavier F. Gouchet" Date: Mon, 16 Dec 2024 10:20:32 +0100 Subject: [PATCH] RUM-6870 use deterministic sampling for distributed tracing --- .../__tests__/distributedTracing.test.ts | 2 +- .../distributedTracing/distributedTracing.tsx | 11 +++++++++-- .../requestProxy/XHRProxy/__tests__/XHRProxy.test.ts | 11 ++++------- packages/react-native-session-replay/package.json | 1 - 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts b/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts index 207c3a68a..30a4b4778 100644 --- a/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts +++ b/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts @@ -6,7 +6,7 @@ import { TracingIdentifier, TracingIdFormat } from '../TracingIdentifier'; -import { TracingIdentifierUtils } from './__utils__/tracingIdentifierUtils'; +import { TracingIdentifierUtils } from './__utils__/TracingIdentifierUtils'; describe('TracingIdentifier', () => { it('M return an unique identifier W toString', async () => { diff --git a/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx b/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx index 7f5332560..7a59e1692 100644 --- a/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx +++ b/packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx @@ -12,6 +12,9 @@ import type { SpanId, TraceId } from './TracingIdentifier'; import type { Hostname } from './firstPartyHosts'; import { getPropagatorsForHost } from './firstPartyHosts'; +const knuthFactor = BigInt('1111111111111111111'); +const twoPow64 = BigInt('0x10000000000000000'); // 2n ** 64n + export type DdRumResourceTracingAttributes = | { tracingStrategy: 'KEEP'; @@ -62,9 +65,13 @@ const generateTracingAttributesWithSampling = ( tracingSamplingRate: number, propagatorTypes: PropagatorType[] ): DdRumResourceTracingAttributes => { - const isSampled = Math.random() * 100 <= tracingSamplingRate; + const traceId = TracingIdentifier.createTraceId(); + const hash = Number((traceId.id * knuthFactor) % twoPow64); + const threshold = (tracingSamplingRate / 100) * Number(twoPow64); + const isSampled = hash <= threshold; + const tracingAttributes: DdRumResourceTracingAttributes = { - traceId: TracingIdentifier.createTraceId(), + traceId: traceId, spanId: TracingIdentifier.createSpanId(), samplingPriorityHeader: isSampled ? '1' : '0', tracingStrategy: 'KEEP', diff --git a/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts b/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts index 9a5be7b7d..61bb274e4 100644 --- a/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +++ b/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts @@ -12,7 +12,7 @@ import { BufferSingleton } from '../../../../../../sdk/DatadogProvider/Buffer/Bu import { DdRum } from '../../../../../DdRum'; import { PropagatorType } from '../../../../../types'; import { XMLHttpRequestMock } from '../../../__tests__/__utils__/XMLHttpRequestMock'; -import { TracingIdentifierUtils } from '../../../distributedTracing/__tests__/__utils__/tracingIdentifierUtils'; +import { TracingIdentifierUtils } from '../../../distributedTracing/__tests__/__utils__/TracingIdentifierUtils'; import { PARENT_ID_HEADER_KEY, TRACE_ID_HEADER_KEY, @@ -323,7 +323,7 @@ describe('XHRProxy', () => { const method = 'GET'; const url = 'https://api.example.com/v2/user'; xhrProxy.onTrackingStart({ - tracingSamplingRate: 50, + tracingSamplingRate: 0, firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([ { match: 'api.example.com', @@ -331,7 +331,6 @@ describe('XHRProxy', () => { } ]) }); - jest.spyOn(global.Math, 'random').mockReturnValue(0.7); // WHEN const xhr = new XMLHttpRequestMock(); @@ -402,7 +401,7 @@ describe('XHRProxy', () => { const method = 'GET'; const url = 'https://api.example.com/v2/user'; xhrProxy.onTrackingStart({ - tracingSamplingRate: 50, + tracingSamplingRate: 0, firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([ { match: 'api.example.com', @@ -410,7 +409,6 @@ describe('XHRProxy', () => { } ]) }); - jest.spyOn(global.Math, 'random').mockReturnValue(0.7); // WHEN const xhr = new XMLHttpRequestMock(); @@ -811,7 +809,6 @@ describe('XHRProxy', () => { tracingSamplingRate: 50, firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([]) }); - jest.spyOn(global.Math, 'random').mockReturnValue(0.7); // WHEN const xhr = new XMLHttpRequestMock(); @@ -843,7 +840,7 @@ describe('XHRProxy', () => { const method = 'GET'; const url = 'https://api.example.com/v2/user'; xhrProxy.onTrackingStart({ - tracingSamplingRate: 50, + tracingSamplingRate: 0, firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([ { match: 'api.example.com', diff --git a/packages/react-native-session-replay/package.json b/packages/react-native-session-replay/package.json index 77ca684d2..de5359f16 100644 --- a/packages/react-native-session-replay/package.json +++ b/packages/react-native-session-replay/package.json @@ -16,7 +16,6 @@ }, "license": "Apache-2.0", "main": "lib/commonjs/index", - "private": "true", "files": [ "src/**", "lib/**",