@datadog/mobile-react-native 1.0.0-rc4 → 1.0.0-rc7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +47 -16
  3. package/android/build.gradle +1 -1
  4. package/android/gradle.properties +4 -4
  5. package/lib/commonjs/DdSdkReactNative.js +38 -23
  6. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  7. package/lib/commonjs/DdSdkReactNativeConfiguration.js +17 -2
  8. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  9. package/lib/commonjs/InternalLog.js +56 -0
  10. package/lib/commonjs/InternalLog.js.map +1 -0
  11. package/lib/commonjs/ProxyConfiguration.js.map +1 -1
  12. package/lib/commonjs/SdkVerbosity.js +26 -0
  13. package/lib/commonjs/SdkVerbosity.js.map +1 -0
  14. package/lib/commonjs/TimeProvider.js +72 -0
  15. package/lib/commonjs/TimeProvider.js.map +1 -0
  16. package/lib/commonjs/Timer.js +10 -23
  17. package/lib/commonjs/Timer.js.map +1 -1
  18. package/lib/commonjs/TrackingConsent.js.map +1 -1
  19. package/lib/commonjs/foundation.js +51 -11
  20. package/lib/commonjs/foundation.js.map +1 -1
  21. package/lib/commonjs/index.js +34 -18
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +14 -5
  24. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  25. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +34 -23
  26. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +62 -36
  28. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +46 -7
  30. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  32. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js +34 -0
  33. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  34. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +1 -1
  35. package/lib/commonjs/types.js.map +1 -1
  36. package/lib/commonjs/version.js +1 -1
  37. package/lib/commonjs/version.js.map +1 -1
  38. package/lib/module/DdSdkReactNative.js +31 -23
  39. package/lib/module/DdSdkReactNative.js.map +1 -1
  40. package/lib/module/DdSdkReactNativeConfiguration.js +18 -3
  41. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  42. package/lib/module/InternalLog.js +52 -0
  43. package/lib/module/InternalLog.js.map +1 -0
  44. package/lib/module/ProxyConfiguration.js.map +1 -1
  45. package/lib/module/SdkVerbosity.js +18 -0
  46. package/lib/module/SdkVerbosity.js.map +1 -0
  47. package/lib/module/TimeProvider.js +63 -0
  48. package/lib/module/TimeProvider.js.map +1 -0
  49. package/lib/module/Timer.js +14 -23
  50. package/lib/module/Timer.js.map +1 -1
  51. package/lib/module/TrackingConsent.js.map +1 -1
  52. package/lib/module/foundation.js +31 -11
  53. package/lib/module/foundation.js.map +1 -1
  54. package/lib/module/index.js +4 -2
  55. package/lib/module/index.js.map +1 -1
  56. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +11 -5
  57. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  58. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +30 -23
  59. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  60. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +57 -34
  61. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  62. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +41 -6
  63. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  64. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  65. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js +25 -0
  66. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  67. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +1 -1
  68. package/lib/module/types.js.map +1 -1
  69. package/lib/module/version.js +1 -1
  70. package/lib/module/version.js.map +1 -1
  71. package/lib/typescript/DdSdkReactNative.d.ts +6 -6
  72. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +15 -4
  73. package/lib/typescript/InternalLog.d.ts +11 -0
  74. package/lib/typescript/SdkVerbosity.d.ts +9 -0
  75. package/lib/typescript/TimeProvider.d.ts +20 -0
  76. package/lib/typescript/Timer.d.ts +3 -2
  77. package/lib/typescript/index.d.ts +4 -2
  78. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
  79. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -2
  80. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +10 -9
  81. package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  82. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +13 -3
  83. package/lib/typescript/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  84. package/lib/typescript/version.d.ts +1 -1
  85. package/package.json +6 -3
  86. package/src/DdSdkReactNative.tsx +112 -62
  87. package/src/DdSdkReactNativeConfiguration.tsx +22 -14
  88. package/src/InternalLog.tsx +50 -0
  89. package/src/ProxyConfiguration.tsx +5 -5
  90. package/src/{NativeSdkVerbosity.tsx → SdkVerbosity.tsx} +6 -6
  91. package/src/TimeProvider.tsx +64 -0
  92. package/src/Timer.ts +20 -35
  93. package/src/TrackingConsent.tsx +3 -3
  94. package/src/__mocks__/InternalLog.tsx +9 -0
  95. package/src/__tests__/DdRumWrapper.test.tsx +40 -33
  96. package/src/__tests__/DdSdkReactNative.test.tsx +697 -513
  97. package/src/__tests__/InternalLog.test.tsx +379 -0
  98. package/src/__tests__/TimeProvider.test.tsx +75 -0
  99. package/src/__tests__/Timer.test.ts +148 -57
  100. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +151 -92
  101. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +179 -106
  102. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +714 -644
  103. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +239 -85
  104. package/src/__tests__/rum/instrumentation/ShallowObjectEqualityChecker.test.ts +31 -0
  105. package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +13 -15
  106. package/src/__tests__/rum/instrumentation/__utils__/XMLHttpRequestMock.ts +97 -0
  107. package/src/foundation.tsx +80 -61
  108. package/src/index.tsx +15 -3
  109. package/src/rum/instrumentation/DdEventsInterceptor.tsx +51 -35
  110. package/src/rum/instrumentation/DdRumErrorTracking.tsx +61 -49
  111. package/src/rum/instrumentation/DdRumResourceTracking.tsx +307 -233
  112. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +72 -25
  113. package/src/rum/instrumentation/DdRumXhr.tsx +21 -11
  114. package/src/rum/instrumentation/EventsInterceptor.tsx +3 -3
  115. package/src/rum/instrumentation/NoOpEventsInterceptor.tsx +2 -2
  116. package/src/rum/instrumentation/ShallowObjectEqualityChecker.ts +24 -0
  117. package/src/rum/instrumentation/TraceIdentifier.tsx +19 -19
  118. package/src/types.tsx +199 -158
  119. package/src/version.ts +1 -1
  120. package/lib/commonjs/NativeSdkVerbosity.js +0 -26
  121. package/lib/commonjs/NativeSdkVerbosity.js.map +0 -1
  122. package/lib/module/NativeSdkVerbosity.js +0 -18
  123. package/lib/module/NativeSdkVerbosity.js.map +0 -1
  124. package/lib/typescript/NativeSdkVerbosity.d.ts +0 -9
@@ -5,55 +5,66 @@
5
5
  */
6
6
 
7
7
  import { Platform } from 'react-native';
8
- import { DdRum } from '../../foundation'
8
+
9
+ import { InternalLog } from '../../InternalLog';
10
+ import { SdkVerbosity } from '../../SdkVerbosity';
9
11
  import Timer from '../../Timer';
10
- import type { DdRumXhr } from './DdRumXhr'
12
+ import { DdRum } from '../../foundation';
13
+
14
+ import type { DdRumResourceTracingAttributes, DdRumXhr } from './DdRumXhr';
11
15
  import { generateTraceId } from './TraceIdentifier';
12
16
 
13
- export const TRACE_ID_HEADER_KEY = "x-datadog-trace-id"
14
- export const PARENT_ID_HEADER_KEY = "x-datadog-parent-id"
15
- export const ORIGIN_HEADER_KEY = "x-datadog-origin"
16
- export const SAMPLING_PRIORITY_HEADER_KEY = "x-datadog-sampling-priority"
17
- export const SAMPLED_HEADER_KEY = "x-datadog-sampled"
18
- export const ORIGIN_RUM = "rum"
17
+ export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
18
+ export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
19
+ export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
20
+ export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
21
+ export const ORIGIN_RUM = 'rum';
22
+ export const RESOURCE_SIZE_ERROR_MESSAGE =
23
+ "Couldn't get resource size, because an error occured: ";
19
24
 
20
- const RESPONSE_START_LABEL = "response_start"
21
- const MISSING_RESOURCE_SIZE = -1
25
+ const RESPONSE_START_LABEL = 'response_start';
26
+ const MISSING_RESOURCE_SIZE = -1;
22
27
 
23
28
  interface Timing {
24
- /**
25
- * Time relative (absolute in case of iOS) to some point, in ns.
26
- */
27
- startTime: number
28
- /**
29
- * Duration in ns.
30
- */
31
- duration: number
29
+ /**
30
+ * Time relative (absolute in case of iOS) to some point, in ns.
31
+ */
32
+ startTime: number;
33
+ /**
34
+ * Duration in ns.
35
+ */
36
+ duration: number;
32
37
  }
33
38
 
34
39
  interface ResourceTimings {
35
- // unlike in Performance API it is not the time until request
36
- // starts (requestStart, before it can be connect, SSL, DNS),
37
- // but the time until the response is first seen
38
- firstByte: Timing,
39
- download: Timing,
40
- // required by iOS, total timing from the beginning to the end
41
- fetch: Timing
40
+ // unlike in Performance API it is not the time until request
41
+ // starts (requestStart, before it can be connect, SSL, DNS),
42
+ // but the time until the response is first seen
43
+ firstByte: Timing;
44
+ download: Timing;
45
+ // required by iOS, total timing from the beginning to the end
46
+ fetch: Timing;
42
47
  }
43
48
 
44
- function createTimings(startTime: number,
45
- responseStartTime: number, responseEndTime: number): ResourceTimings {
46
-
47
- const firstByte = formatTiming(startTime, startTime, responseStartTime)
48
- const download = formatTiming(startTime, responseStartTime, responseEndTime)
49
- // needed for iOS, simply total duration from start to end
50
- const fetch = formatTiming(startTime, startTime, responseEndTime)
51
-
52
- return {
53
- firstByte,
54
- download,
55
- fetch
56
- }
49
+ function createTimings(
50
+ startTime: number,
51
+ responseStartTime: number,
52
+ responseEndTime: number
53
+ ): ResourceTimings {
54
+ const firstByte = formatTiming(startTime, startTime, responseStartTime);
55
+ const download = formatTiming(
56
+ startTime,
57
+ responseStartTime,
58
+ responseEndTime
59
+ );
60
+ // needed for iOS, simply total duration from start to end
61
+ const fetch = formatTiming(startTime, startTime, responseEndTime);
62
+
63
+ return {
64
+ firstByte,
65
+ download,
66
+ fetch
67
+ };
57
68
  }
58
69
 
59
70
  /**
@@ -62,226 +73,289 @@ function createTimings(startTime: number,
62
73
  * @param end End time (absolute) of the timing
63
74
  */
64
75
  function formatTiming(origin: number, start: number, end: number): Timing {
65
- return {
66
- duration: timeToNanos(end - start),
67
- // if it is Android, startTime should be relative to the origin,
68
- // if it is iOS - absolute (unix timestamp)
69
- startTime: Platform.OS === 'ios' ? timeToNanos(start) : timeToNanos(start - origin)
70
- }
76
+ return {
77
+ duration: timeToNanos(end - start),
78
+ // if it is Android, startTime should be relative to the origin,
79
+ // if it is iOS - absolute (unix timestamp)
80
+ startTime:
81
+ Platform.OS === 'ios'
82
+ ? timeToNanos(start)
83
+ : timeToNanos(start - origin)
84
+ };
71
85
  }
72
86
 
73
87
  function timeToNanos(durationMs: number): number {
74
- return +(durationMs * 1e6).toFixed(0)
88
+ return +(durationMs * 1e6).toFixed(0);
75
89
  }
76
90
 
77
91
  function byteLength(str: string): number {
78
- // This is a weird trick, but it works.
79
- // Output is the same as TextEncoder.encode(...).length
80
- return unescape(encodeURI(str)).length;
92
+ // This is a weird trick, but it works.
93
+ // Output is the same as TextEncoder.encode(...).length
94
+ return unescape(encodeURI(str)).length;
81
95
  }
82
96
 
83
- function getResponseContentLengthFromHeader(xhr: XMLHttpRequest): number | null {
84
- const contentLengthHeader = parseInt(xhr.getResponseHeader('Content-Length') ?? '', 10);
85
- if (!isNaN(contentLengthHeader)) {
86
- return contentLengthHeader;
87
- }
88
- return null;
97
+ function getResponseContentLengthFromHeader(
98
+ xhr: XMLHttpRequest
99
+ ): number | null {
100
+ const contentLengthHeader = parseInt(
101
+ xhr.getResponseHeader('Content-Length') ?? '',
102
+ 10
103
+ );
104
+ if (!isNaN(contentLengthHeader)) {
105
+ return contentLengthHeader;
106
+ }
107
+ return null;
89
108
  }
90
109
 
91
110
  export function calculateResponseSize(xhr: XMLHttpRequest): number {
111
+ const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
112
+ if (contentLengthHeader != null) {
113
+ return contentLengthHeader as number;
114
+ }
92
115
 
93
- const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
94
- if (contentLengthHeader != null) {
95
- return contentLengthHeader as number;
96
- }
97
-
98
- const response = xhr.response
99
- if (!response) {
100
- return MISSING_RESOURCE_SIZE;
101
- }
102
-
103
- let size;
104
- try {
105
- switch (xhr.responseType) {
106
- case '':
107
- case 'text':
108
- // String
109
- size = byteLength(response);
110
- break;
111
- case 'blob':
112
- size = response.size;
113
- break;
114
- case 'arraybuffer':
115
- size = response.byteLength;
116
- break;
117
- case 'document':
118
- // currently not supported by RN as of 0.66
119
- // HTML Document or XML Document
120
- break;
121
- case 'json':
122
- // plain JS object
123
- // original size was lost, because this is the object which was parsed.
124
- // We can only convert back to the string and calculate the size,
125
- // which will roughly match original.
126
- size = byteLength(JSON.stringify(response))
127
- break;
128
- default:
129
- break;
116
+ const response = xhr.response;
117
+ if (!response) {
118
+ return MISSING_RESOURCE_SIZE;
130
119
  }
131
- } catch (e) {
132
- console.error("Couldn't get resource size, because of the error", e);
133
- }
134
-
135
- if (typeof size !== 'number') {
136
- return MISSING_RESOURCE_SIZE;
137
- }
138
- return size;
120
+
121
+ let size;
122
+ try {
123
+ switch (xhr.responseType) {
124
+ case '':
125
+ case 'text':
126
+ // String
127
+ size = byteLength(response);
128
+ break;
129
+ case 'blob':
130
+ size = response.size;
131
+ break;
132
+ case 'arraybuffer':
133
+ size = response.byteLength;
134
+ break;
135
+ case 'document':
136
+ // currently not supported by RN as of 0.66
137
+ // HTML Document or XML Document
138
+ break;
139
+ case 'json':
140
+ // plain JS object
141
+ // original size was lost, because this is the object which was parsed.
142
+ // We can only convert back to the string and calculate the size,
143
+ // which will roughly match original.
144
+ size = byteLength(JSON.stringify(response));
145
+ break;
146
+ default:
147
+ break;
148
+ }
149
+ } catch (e) {
150
+ InternalLog.log(
151
+ `${RESOURCE_SIZE_ERROR_MESSAGE}${e}`,
152
+ SdkVerbosity.ERROR
153
+ );
154
+ }
155
+
156
+ if (typeof size !== 'number') {
157
+ return MISSING_RESOURCE_SIZE;
158
+ }
159
+ return size;
139
160
  }
140
161
 
162
+ const generateTracingAttributes = (
163
+ tracingSamplingRate: number
164
+ ): DdRumResourceTracingAttributes => {
165
+ if (Math.random() * 100 <= tracingSamplingRate) {
166
+ return {
167
+ traceId: generateTraceId(),
168
+ spanId: generateTraceId(),
169
+ samplingPriorityHeader: '1',
170
+ tracingStrategy: 'KEEP'
171
+ };
172
+ }
173
+ return {
174
+ samplingPriorityHeader: '0',
175
+ tracingStrategy: 'DISCARD'
176
+ };
177
+ };
178
+
141
179
  /**
142
- * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
143
- */
180
+ * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
181
+ */
144
182
  export class DdRumResourceTracking {
145
-
146
- private static isTracking = false
147
-
148
- private static originalXhrOpen: any
149
- private static originalXhrSend: any
150
-
151
- /**
152
- * Starts tracking resources and sends a RUM Resource event every time a network request is detected.
153
- */
154
- static startTracking(): void {
155
- DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
156
- }
157
-
158
- /**
159
- * Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
160
- */
161
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
162
- static startTrackingInternal(xhrType: any): void {
163
- // extra safety to avoid proxying the XHR class twice
164
- if (DdRumResourceTracking.isTracking) {
165
- return
183
+ private static isTracking = false;
184
+ private static tracingSamplingRate: number;
185
+
186
+ private static originalXhrOpen: any;
187
+ private static originalXhrSend: any;
188
+
189
+ /**
190
+ * Starts tracking resources and sends a RUM Resource event every time a network request is detected.
191
+ */
192
+ static startTracking(tracingSamplingRate: number): void {
193
+ DdRumResourceTracking.startTrackingInternal(
194
+ XMLHttpRequest,
195
+ tracingSamplingRate
196
+ );
166
197
  }
167
198
 
168
- DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
169
- DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
199
+ /**
200
+ * Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
201
+ */
202
+ static startTrackingInternal(
203
+ xhrType: typeof XMLHttpRequest,
204
+ tracingSamplingRate: number
205
+ ): void {
206
+ // extra safety to avoid proxying the XHR class twice
207
+ if (DdRumResourceTracking.isTracking) {
208
+ InternalLog.log(
209
+ 'Datadog SDK is already tracking XHR resources',
210
+ SdkVerbosity.WARN
211
+ );
212
+ return;
213
+ }
170
214
 
171
- DdRumResourceTracking.proxyXhr(xhrType)
172
- }
215
+ DdRumResourceTracking.tracingSamplingRate = tracingSamplingRate;
173
216
 
217
+ DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
218
+ DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
174
219
 
175
- static stopTracking(): void {
176
- if (DdRumResourceTracking.isTracking) {
177
- DdRumResourceTracking.isTracking = false;
178
- XMLHttpRequest.prototype.open = DdRumResourceTracking.originalXhrOpen;
179
- XMLHttpRequest.prototype.send = DdRumResourceTracking.originalXhrSend;
220
+ DdRumResourceTracking.proxyXhr(xhrType);
221
+ InternalLog.log(
222
+ 'Datadog SDK is tracking XHR resources',
223
+ SdkVerbosity.INFO
224
+ );
180
225
  }
181
- }
182
-
183
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
184
- static proxyXhr(xhrType: any): void {
185
- this.proxyOpen(xhrType);
186
- this.proxySend(xhrType);
187
- }
188
-
189
- private static proxyOpen(xhrType: any): void {
190
- const originalXhrOpen = this.originalXhrOpen;
191
- xhrType.prototype.open = function (this: DdRumXhr, method: string, url: string) {
192
- // Keep track of the method and url
193
- // start time is tracked by the `send` method
194
- const spanId = generateTraceId()
195
- const traceId = generateTraceId()
196
- this._datadog_xhr = {
197
- method,
198
- url: url,
199
- reported: false,
200
- timer: new Timer(),
201
- spanId: spanId,
202
- traceId: traceId
203
- }
204
- // eslint-disable-next-line prefer-rest-params
205
- return originalXhrOpen.apply(this, arguments as any)
226
+
227
+ static stopTracking(): void {
228
+ if (DdRumResourceTracking.isTracking) {
229
+ DdRumResourceTracking.isTracking = false;
230
+ XMLHttpRequest.prototype.open =
231
+ DdRumResourceTracking.originalXhrOpen;
232
+ XMLHttpRequest.prototype.send =
233
+ DdRumResourceTracking.originalXhrSend;
234
+ }
206
235
  }
207
- }
208
-
209
- private static proxySend(xhrType: any): void {
210
- const originalXhrSend = this.originalXhrSend;
211
- xhrType.prototype.send = function (this: DdRumXhr) {
212
-
213
- if (this._datadog_xhr) {
214
- // keep track of start time
215
- this._datadog_xhr.timer.start()
216
- this.setRequestHeader(TRACE_ID_HEADER_KEY, this._datadog_xhr.traceId)
217
- this.setRequestHeader(PARENT_ID_HEADER_KEY, this._datadog_xhr.spanId)
218
- this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM)
219
- this.setRequestHeader(SAMPLING_PRIORITY_HEADER_KEY, "1")
220
- this.setRequestHeader(SAMPLED_HEADER_KEY, "1")
221
- }
222
-
223
- DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
224
-
225
- // eslint-disable-next-line prefer-rest-params
226
- return originalXhrSend.apply(this, arguments as any);
236
+
237
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
238
+ static proxyXhr(xhrType: any): void {
239
+ this.proxyOpen(xhrType);
240
+ this.proxySend(xhrType);
227
241
  }
228
- }
229
-
230
- private static proxyOnReadyStateChange(xhrProxy: DdRumXhr, xhrType: any): void {
231
- const originalOnreadystatechange = xhrProxy.onreadystatechange
232
- xhrProxy.onreadystatechange = function () {
233
- if (xhrProxy.readyState === xhrType.DONE) {
234
- if (!xhrProxy._datadog_xhr.reported) {
235
- DdRumResourceTracking.reportXhr(xhrProxy);
236
- xhrProxy._datadog_xhr.reported = true;
237
- }
238
- } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
239
- xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL)
240
- }
241
-
242
- if (originalOnreadystatechange) {
243
- // eslint-disable-next-line prefer-rest-params
244
- originalOnreadystatechange.apply(xhrProxy, arguments as any)
245
- }
242
+
243
+ private static proxyOpen(xhrType: any): void {
244
+ const originalXhrOpen = this.originalXhrOpen;
245
+ xhrType.prototype.open = function (
246
+ this: DdRumXhr,
247
+ method: string,
248
+ url: string
249
+ ) {
250
+ // Keep track of the method and url
251
+ // start time is tracked by the `send` method
252
+ this._datadog_xhr = {
253
+ method,
254
+ url,
255
+ reported: false,
256
+ timer: new Timer(),
257
+ tracingAttributes: generateTracingAttributes(
258
+ DdRumResourceTracking.tracingSamplingRate
259
+ )
260
+ };
261
+ // eslint-disable-next-line prefer-rest-params
262
+ return originalXhrOpen.apply(this, arguments as any);
263
+ };
264
+ }
265
+
266
+ private static proxySend(xhrType: any): void {
267
+ const originalXhrSend = this.originalXhrSend;
268
+ xhrType.prototype.send = function (this: DdRumXhr) {
269
+ if (this._datadog_xhr) {
270
+ // keep track of start time
271
+ this._datadog_xhr.timer.start();
272
+ this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM);
273
+
274
+ const tracingAttributes = this._datadog_xhr.tracingAttributes;
275
+ this.setRequestHeader(
276
+ SAMPLING_PRIORITY_HEADER_KEY,
277
+ tracingAttributes.samplingPriorityHeader
278
+ );
279
+ if (tracingAttributes.tracingStrategy !== 'DISCARD') {
280
+ this.setRequestHeader(
281
+ TRACE_ID_HEADER_KEY,
282
+ tracingAttributes.traceId
283
+ );
284
+ this.setRequestHeader(
285
+ PARENT_ID_HEADER_KEY,
286
+ tracingAttributes.spanId
287
+ );
288
+ }
289
+ }
290
+
291
+ DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
292
+
293
+ // eslint-disable-next-line prefer-rest-params
294
+ return originalXhrSend.apply(this, arguments as any);
295
+ };
246
296
  }
247
- }
248
-
249
- private static reportXhr(xhrProxy: DdRumXhr): void {
250
-
251
- const responseSize = calculateResponseSize(xhrProxy)
252
-
253
- const context = xhrProxy._datadog_xhr
254
-
255
- const key = context.timer.startTime + "/" + context.method
256
-
257
- context.timer.stop()
258
-
259
- DdRum.startResource(
260
- key,
261
- context.method,
262
- context.url,
263
- {
264
- "_dd.span_id": context.spanId,
265
- "_dd.trace_id": context.traceId
266
- },
267
- context.timer.startTime
268
- ).then(() => {
269
- DdRum.stopResource(
270
- key,
271
- xhrProxy.status,
272
- "xhr",
273
- responseSize,
274
- {
275
- "_dd.resource_timings": context.timer.hasTickFor(RESPONSE_START_LABEL) ?
276
- createTimings(
277
- context.timer.startTime,
278
- context.timer.timeAt(RESPONSE_START_LABEL),
279
- context.timer.stopTime
280
- ) : null
281
- },
282
- context.timer.stopTime
283
- );
284
- })
285
- }
286
297
 
298
+ private static proxyOnReadyStateChange(
299
+ xhrProxy: DdRumXhr,
300
+ xhrType: any
301
+ ): void {
302
+ const originalOnreadystatechange = xhrProxy.onreadystatechange;
303
+ xhrProxy.onreadystatechange = function () {
304
+ if (xhrProxy.readyState === xhrType.DONE) {
305
+ if (!xhrProxy._datadog_xhr.reported) {
306
+ DdRumResourceTracking.reportXhr(xhrProxy);
307
+ xhrProxy._datadog_xhr.reported = true;
308
+ }
309
+ } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
310
+ xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL);
311
+ }
312
+
313
+ if (originalOnreadystatechange) {
314
+ // eslint-disable-next-line prefer-rest-params
315
+ originalOnreadystatechange.apply(xhrProxy, arguments as any);
316
+ }
317
+ };
318
+ }
319
+
320
+ private static reportXhr(xhrProxy: DdRumXhr): void {
321
+ const responseSize = calculateResponseSize(xhrProxy);
322
+
323
+ const context = xhrProxy._datadog_xhr;
324
+
325
+ const key = `${context.timer.startTime}/${context.method}`;
326
+
327
+ context.timer.stop();
328
+
329
+ DdRum.startResource(
330
+ key,
331
+ context.method,
332
+ context.url,
333
+ context.tracingAttributes.tracingStrategy === 'DISCARD'
334
+ ? undefined
335
+ : {
336
+ '_dd.span_id': context.tracingAttributes.spanId,
337
+ '_dd.trace_id': context.tracingAttributes.traceId
338
+ },
339
+ context.timer.startTime
340
+ ).then(() => {
341
+ DdRum.stopResource(
342
+ key,
343
+ xhrProxy.status,
344
+ 'xhr',
345
+ responseSize,
346
+ {
347
+ '_dd.resource_timings': context.timer.hasTickFor(
348
+ RESPONSE_START_LABEL
349
+ )
350
+ ? createTimings(
351
+ context.timer.startTime,
352
+ context.timer.timeAt(RESPONSE_START_LABEL),
353
+ context.timer.stopTime
354
+ )
355
+ : null
356
+ },
357
+ context.timer.stopTime
358
+ );
359
+ });
360
+ }
287
361
  }