Skip to content

Commit

Permalink
Merge pull request #747 from DataDog/xgouchet/RUM-6870/deterministic_…
Browse files Browse the repository at this point in the history
…sampling

RUM-6870 use deterministic sampling for distributed tracing
  • Loading branch information
xgouchet authored Dec 16, 2024
2 parents d984be1 + cec6f51 commit 305b9f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -323,15 +323,14 @@ 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',
propagatorTypes: [PropagatorType.DATADOG]
}
])
});
jest.spyOn(global.Math, 'random').mockReturnValue(0.7);

// WHEN
const xhr = new XMLHttpRequestMock();
Expand Down Expand Up @@ -402,15 +401,14 @@ 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',
propagatorTypes: [PropagatorType.DATADOG]
}
])
});
jest.spyOn(global.Math, 'random').mockReturnValue(0.7);

// WHEN
const xhr = new XMLHttpRequestMock();
Expand Down Expand Up @@ -811,7 +809,6 @@ describe('XHRProxy', () => {
tracingSamplingRate: 50,
firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
});
jest.spyOn(global.Math, 'random').mockReturnValue(0.7);

// WHEN
const xhr = new XMLHttpRequestMock();
Expand Down Expand Up @@ -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',
Expand Down
1 change: 0 additions & 1 deletion packages/react-native-session-replay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"license": "Apache-2.0",
"main": "lib/commonjs/index",
"private": "true",
"files": [
"src/**",
"lib/**",
Expand Down

0 comments on commit 305b9f5

Please sign in to comment.