44 * Copyright 2016-Present Datadog, Inc.
55 */
66
7+ import BigInt from 'big-integer' ;
8+
79import { InternalLog } from '../../../InternalLog' ;
810import { SdkVerbosity } from '../../../SdkVerbosity' ;
11+ import { getGlobalInstance } from '../../../utils/singletonUtils' ;
912import type { FirstPartyHost } from '../../types' ;
1013
1114import { firstPartyHostsRegexMapBuilder } from './distributedTracing/firstPartyHosts' ;
@@ -14,37 +17,50 @@ import { filterDevResource } from './requestProxy/XHRProxy/DatadogRumResource/in
1417import { XHRProxy } from './requestProxy/XHRProxy/XHRProxy' ;
1518import 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+ ) ;
0 commit comments