@datadog/mobile-react-native 1.0.0-rc5 → 1.0.0-rc6

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