Skip to content

Commit bfdb696

Browse files
feat: Support trace sampling based on RUM Session ID
1 parent ed5e7d0 commit bfdb696

File tree

6 files changed

+429
-30
lines changed

6 files changed

+429
-30
lines changed

packages/core/datadog-configuration.schema.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@
219219
"type": "boolean"
220220
},
221221
"sessionSampleRate": {
222-
"description": "Percentage of sampled RUM sessions. Range `0`-`100`.",
222+
"description": "Percentage of sampled RUM sessions. Range `0`-`100`. (default `100`).",
223223
"type": "integer"
224224
},
225225
"trackBackgroundEvents": {
@@ -253,7 +253,7 @@
253253
"type": "boolean"
254254
},
255255
"telemetrySampleRate": {
256-
"description": "The sampling rate for Internal Telemetry (info related to the work of the SDK internals). Range `0`-`100`.",
256+
"description": "The sampling rate for Internal Telemetry (info related to the work of the SDK internals). Range `0`-`100` (default `20`).",
257257
"type": "integer"
258258
},
259259
"customEndpoint": {
@@ -288,7 +288,7 @@
288288
"type": "object",
289289
"properties": {
290290
"resourceTraceSampleRate": {
291-
"description": "Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.",
291+
"description": "Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100` (default `100`).",
292292
"type": "number"
293293
},
294294
"customEndpoint": {

packages/core/src/DdSdkReactNativeConfiguration.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ export class RumConfiguration {
333333

334334
/**
335335
* Percentage of sampled RUM sessions. Range `0`-`100`.
336+
* Default is `100`.
336337
*/
337338
public sessionSampleRate: number = DEFAULTS.sessionSampleRate;
338339

@@ -419,6 +420,7 @@ export class LogsConfiguration {
419420
export class TraceConfiguration {
420421
/**
421422
* Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
423+
* Default is `100`.
422424
*/
423425
public resourceTraceSampleRate: number = DEFAULTS.resourceTraceSampleRate;
424426

packages/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
* Copyright 2016-Present Datadog, Inc.
55
*/
66

7+
import BigInt from 'big-integer';
8+
79
import { InternalLog } from '../../../InternalLog';
810
import { SdkVerbosity } from '../../../SdkVerbosity';
11+
import { getGlobalInstance } from '../../../utils/singletonUtils';
912
import type { FirstPartyHost } from '../../types';
1013

1114
import { firstPartyHostsRegexMapBuilder } from './distributedTracing/firstPartyHosts';
@@ -14,37 +17,50 @@ import { filterDevResource } from './requestProxy/XHRProxy/DatadogRumResource/in
1417
import { XHRProxy } from './requestProxy/XHRProxy/XHRProxy';
1518
import type { RequestProxy } from './requestProxy/interfaces/RequestProxy';
1619

20+
export const MAX_TRACE_ID = BigInt.one.shiftLeft(64).minus(BigInt.one);
21+
const RUM_RESOURCE_TRACKING_MODULE =
22+
'com.datadog.reactnative.rum.resource_tracking';
23+
1724
/**
1825
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
1926
*/
20-
export class DdRumResourceTracking {
21-
private static isTracking = false;
22-
private static requestProxy: RequestProxy | null;
27+
class RumResourceTracking {
28+
private _isTracking = false;
29+
private _requestProxy: RequestProxy | null = null;
30+
private _maxSampledTraceId: BigInt.BigInteger | null = null;
31+
32+
get isTracking(): boolean {
33+
return this._isTracking;
34+
}
35+
36+
get maxSampledTraceId(): BigInt.BigInteger {
37+
return this._maxSampledTraceId ?? BigInt(0);
38+
}
2339

2440
/**
2541
* Starts tracking resources and sends a RUM Resource event every time a network request is detected.
2642
*/
27-
static startTracking({
43+
startTracking({
2844
tracingSamplingRate,
2945
firstPartyHosts
3046
}: {
3147
tracingSamplingRate: number;
3248
firstPartyHosts: FirstPartyHost[];
3349
}): void {
3450
// extra safety to avoid proxying the XHR class twice
35-
if (DdRumResourceTracking.isTracking) {
51+
if (this._isTracking) {
3652
InternalLog.log(
3753
'Datadog SDK is already tracking XHR resources',
3854
SdkVerbosity.WARN
3955
);
4056
return;
4157
}
4258

43-
this.requestProxy = new XHRProxy({
59+
this._requestProxy = new XHRProxy({
4460
xhrType: XMLHttpRequest,
4561
resourceReporter: new ResourceReporter([filterDevResource])
4662
});
47-
this.requestProxy.onTrackingStart({
63+
this._requestProxy.onTrackingStart({
4864
tracingSamplingRate,
4965
firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder(
5066
firstPartyHosts
@@ -55,16 +71,30 @@ export class DdRumResourceTracking {
5571
'Datadog SDK is tracking XHR resources',
5672
SdkVerbosity.INFO
5773
);
58-
DdRumResourceTracking.isTracking = true;
74+
75+
this._isTracking = true;
76+
this._maxSampledTraceId = RumResourceTracking.getMaxTraceId(
77+
tracingSamplingRate
78+
);
5979
}
6080

61-
static stopTracking(): void {
62-
if (DdRumResourceTracking.isTracking) {
63-
DdRumResourceTracking.isTracking = false;
64-
if (this.requestProxy) {
65-
this.requestProxy.onTrackingStop();
81+
stopTracking(): void {
82+
if (this._isTracking) {
83+
this._isTracking = false;
84+
if (this._requestProxy) {
85+
this._requestProxy.onTrackingStop();
6686
}
67-
this.requestProxy = null;
87+
this._requestProxy = null;
88+
this._maxSampledTraceId = null;
6889
}
6990
}
91+
92+
private static getMaxTraceId(sampleRate: number): BigInt.BigInteger {
93+
return BigInt(MAX_TRACE_ID.toJSNumber() * (sampleRate / 100.0));
94+
}
7095
}
96+
97+
export const DdRumResourceTracking = getGlobalInstance(
98+
RUM_RESOURCE_TRACKING_MODULE,
99+
() => new RumResourceTracking()
100+
);

packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,25 @@ export class TracingIdentifier {
134134
return new TracingIdentifier(TracingIdType.span) as SpanId;
135135
}
136136

137+
/**
138+
* Crate a Tracing Identifier from a given BigInt ID and type.
139+
* @param id the BigInt ID.
140+
* @param type the type of tracing identifier.
141+
* @returns the generated {@link TraceId} or {@link SpanId}.
142+
*/
143+
public static fromBigInt(
144+
id: BigInteger,
145+
type: TracingIdType
146+
): TraceId | SpanId {
147+
return new TracingIdentifier(type, id) as TraceId | SpanId;
148+
}
149+
137150
/**
138151
* Private constructor to initialize the {@link TracingIdentifier} based on the given
139152
* {@link TracingIdType}.
140153
*/
141-
private constructor(type: TracingIdType) {
142-
this.id = this.generateUUID(type);
154+
private constructor(type: TracingIdType, id?: BigInt.BigInteger) {
155+
this.id = id ?? this.generateUUID(type);
143156
this.type = type;
144157
}
145158

0 commit comments

Comments
 (0)