Skip to content

Commit

Permalink
RUM-6870 use deterministic sampling for distributed tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Dec 16, 2024
1 parent d984be1 commit 08baa03
Showing 1 changed file with 9 additions and 2 deletions.
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 = (traceId.id * knuthFactor) % twoPow64

const isSampled =Number(hash) <= (tracingSamplingRate / 100) * Number(twoPow64)

const tracingAttributes: DdRumResourceTracingAttributes = {
traceId: TracingIdentifier.createTraceId(),
traceId: traceId,
spanId: TracingIdentifier.createSpanId(),
samplingPriorityHeader: isSampled ? '1' : '0',
tracingStrategy: 'KEEP',
Expand Down

0 comments on commit 08baa03

Please sign in to comment.