@datadog/mobile-react-native 1.0.0-rc3 → 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 (137) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +47 -16
  3. package/android/build.gradle +2 -3
  4. package/android/gradle.properties +4 -4
  5. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
  6. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
  7. package/ios/Sources/DdRum.m +10 -9
  8. package/ios/Sources/DdRum.swift +24 -24
  9. package/ios/Sources/DdTrace.m +2 -2
  10. package/ios/Sources/DdTrace.swift +6 -6
  11. package/lib/commonjs/DdSdkReactNative.js +42 -22
  12. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js +17 -2
  14. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  15. package/lib/commonjs/InternalLog.js +51 -0
  16. package/lib/commonjs/InternalLog.js.map +1 -0
  17. package/lib/commonjs/ProxyConfiguration.js.map +1 -1
  18. package/lib/commonjs/SdkVerbosity.js +26 -0
  19. package/lib/commonjs/SdkVerbosity.js.map +1 -0
  20. package/lib/commonjs/TimeProvider.js +72 -0
  21. package/lib/commonjs/TimeProvider.js.map +1 -0
  22. package/lib/commonjs/Timer.js +10 -23
  23. package/lib/commonjs/Timer.js.map +1 -1
  24. package/lib/commonjs/TrackingConsent.js.map +1 -1
  25. package/lib/commonjs/foundation.js +62 -40
  26. package/lib/commonjs/foundation.js.map +1 -1
  27. package/lib/commonjs/index.js +26 -18
  28. package/lib/commonjs/index.js.map +1 -1
  29. package/lib/commonjs/internal.js +14 -0
  30. package/lib/commonjs/internal.js.map +1 -0
  31. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +14 -5
  32. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  33. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +51 -20
  34. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  35. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +141 -30
  36. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  37. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +46 -7
  38. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  39. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  40. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js +34 -0
  41. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  42. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +1 -1
  43. package/lib/commonjs/types.js.map +1 -1
  44. package/lib/commonjs/version.js +10 -0
  45. package/lib/commonjs/version.js.map +1 -0
  46. package/lib/module/DdSdkReactNative.js +34 -22
  47. package/lib/module/DdSdkReactNative.js.map +1 -1
  48. package/lib/module/DdSdkReactNativeConfiguration.js +18 -3
  49. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  50. package/lib/module/InternalLog.js +46 -0
  51. package/lib/module/InternalLog.js.map +1 -0
  52. package/lib/module/ProxyConfiguration.js.map +1 -1
  53. package/lib/module/SdkVerbosity.js +18 -0
  54. package/lib/module/SdkVerbosity.js.map +1 -0
  55. package/lib/module/TimeProvider.js +63 -0
  56. package/lib/module/TimeProvider.js.map +1 -0
  57. package/lib/module/Timer.js +14 -23
  58. package/lib/module/Timer.js.map +1 -1
  59. package/lib/module/TrackingConsent.js.map +1 -1
  60. package/lib/module/foundation.js +42 -40
  61. package/lib/module/foundation.js.map +1 -1
  62. package/lib/module/index.js +3 -2
  63. package/lib/module/index.js.map +1 -1
  64. package/lib/module/internal.js +2 -0
  65. package/lib/module/internal.js.map +1 -0
  66. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +11 -5
  67. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  68. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +46 -20
  69. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  70. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +133 -29
  71. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  72. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +41 -6
  73. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  74. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  75. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js +25 -0
  76. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  77. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +1 -1
  78. package/lib/module/types.js.map +1 -1
  79. package/lib/module/version.js +3 -0
  80. package/lib/module/version.js.map +1 -0
  81. package/lib/typescript/DdSdkReactNative.d.ts +7 -6
  82. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +15 -4
  83. package/lib/typescript/InternalLog.d.ts +6 -0
  84. package/lib/typescript/SdkVerbosity.d.ts +9 -0
  85. package/lib/typescript/TimeProvider.d.ts +20 -0
  86. package/lib/typescript/Timer.d.ts +3 -2
  87. package/lib/typescript/index.d.ts +3 -2
  88. package/lib/typescript/internal.d.ts +1 -0
  89. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
  90. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +3 -2
  91. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +13 -8
  92. package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  93. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +13 -3
  94. package/lib/typescript/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  95. package/lib/typescript/types.d.ts +12 -11
  96. package/lib/typescript/version.d.ts +1 -0
  97. package/package.json +7 -4
  98. package/src/DdSdkReactNative.tsx +112 -58
  99. package/src/DdSdkReactNativeConfiguration.tsx +22 -14
  100. package/src/InternalLog.tsx +45 -0
  101. package/src/ProxyConfiguration.tsx +5 -5
  102. package/src/{NativeSdkVerbosity.tsx → SdkVerbosity.tsx} +6 -6
  103. package/src/TimeProvider.tsx +64 -0
  104. package/src/Timer.ts +20 -35
  105. package/src/TrackingConsent.tsx +3 -3
  106. package/src/__mocks__/InternalLog.tsx +9 -0
  107. package/src/__tests__/DdRumWrapper.test.tsx +98 -0
  108. package/src/__tests__/DdSdkReactNative.test.tsx +699 -500
  109. package/src/__tests__/InternalLog.test.tsx +379 -0
  110. package/src/__tests__/TimeProvider.test.tsx +75 -0
  111. package/src/__tests__/Timer.test.ts +148 -57
  112. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +151 -92
  113. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +243 -76
  114. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +723 -403
  115. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +239 -85
  116. package/src/__tests__/rum/instrumentation/ShallowObjectEqualityChecker.test.ts +31 -0
  117. package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +13 -15
  118. package/src/__tests__/rum/instrumentation/__utils__/XMLHttpRequestMock.ts +97 -0
  119. package/src/foundation.tsx +55 -42
  120. package/src/index.tsx +13 -3
  121. package/src/internal.tsx +1 -0
  122. package/src/rum/instrumentation/DdEventsInterceptor.tsx +51 -35
  123. package/src/rum/instrumentation/DdRumErrorTracking.tsx +75 -42
  124. package/src/rum/instrumentation/DdRumResourceTracking.tsx +312 -166
  125. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +72 -25
  126. package/src/rum/instrumentation/DdRumXhr.tsx +21 -11
  127. package/src/rum/instrumentation/EventsInterceptor.tsx +3 -3
  128. package/src/rum/instrumentation/NoOpEventsInterceptor.tsx +2 -2
  129. package/src/rum/instrumentation/ShallowObjectEqualityChecker.ts +24 -0
  130. package/src/rum/instrumentation/TraceIdentifier.tsx +19 -19
  131. package/src/types.tsx +199 -157
  132. package/src/version.ts +2 -0
  133. package/lib/commonjs/NativeSdkVerbosity.js +0 -26
  134. package/lib/commonjs/NativeSdkVerbosity.js.map +0 -1
  135. package/lib/module/NativeSdkVerbosity.js +0 -18
  136. package/lib/module/NativeSdkVerbosity.js.map +0 -1
  137. package/lib/typescript/NativeSdkVerbosity.d.ts +0 -9
@@ -5,52 +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 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: ";
17
24
 
18
- const RESPONSE_START_LABEL = "response_start"
25
+ const RESPONSE_START_LABEL = 'response_start';
26
+ const MISSING_RESOURCE_SIZE = -1;
19
27
 
20
28
  interface Timing {
21
- /**
22
- * Time relative (absolute in case of iOS) to some point, in ns.
23
- */
24
- startTime: number
25
- /**
26
- * Duration in ns.
27
- */
28
- 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;
29
37
  }
30
38
 
31
39
  interface ResourceTimings {
32
- // unlike in Performance API it is not the time until request
33
- // starts (requestStart, before it can be connect, SSL, DNS),
34
- // but the time until the response is first seen
35
- firstByte: Timing,
36
- download: Timing,
37
- // required by iOS, total timing from the beginning to the end
38
- 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;
39
47
  }
40
48
 
41
- function createTimings(startTime: number,
42
- responseStartTime: number, responseEndTime: number): ResourceTimings {
43
-
44
- const firstByte = formatTiming(startTime, startTime, responseStartTime)
45
- const download = formatTiming(startTime, responseStartTime, responseEndTime)
46
- // needed for iOS, simply total duration from start to end
47
- const fetch = formatTiming(startTime, startTime, responseEndTime)
48
-
49
- return {
50
- firstByte,
51
- download,
52
- fetch
53
- }
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
+ };
54
68
  }
55
69
 
56
70
  /**
@@ -59,157 +73,289 @@ function createTimings(startTime: number,
59
73
  * @param end End time (absolute) of the timing
60
74
  */
61
75
  function formatTiming(origin: number, start: number, end: number): Timing {
62
- return {
63
- duration: timeToNanos(end - start),
64
- // if it is Android, startTime should be relative to the origin,
65
- // if it is iOS - absolute (unix timestamp)
66
- startTime: Platform.OS === 'ios' ? timeToNanos(start) : timeToNanos(start - origin)
67
- }
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
+ };
68
85
  }
69
86
 
70
87
  function timeToNanos(durationMs: number): number {
71
- return +(durationMs * 1e6).toFixed(0)
88
+ return +(durationMs * 1e6).toFixed(0);
72
89
  }
73
90
 
74
- /**
75
- * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
76
- */
77
- export class DdRumResourceTracking {
91
+ function byteLength(str: string): number {
92
+ // This is a weird trick, but it works.
93
+ // Output is the same as TextEncoder.encode(...).length
94
+ return unescape(encodeURI(str)).length;
95
+ }
78
96
 
79
- private static isTracking = false
80
-
81
- private static originalXhrOpen: any
82
- private static originalXhrSend: any
83
-
84
- /**
85
- * Starts tracking resources and sends a RUM Resource event every time a network request is detected.
86
- */
87
- static startTracking(): void {
88
- DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
89
- }
90
-
91
- /**
92
- * Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
93
- */
94
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
95
- static startTrackingInternal(xhrType: any): void {
96
- // extra safety to avoid proxying the XHR class twice
97
- if (DdRumResourceTracking.isTracking) {
98
- return
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;
99
106
  }
107
+ return null;
108
+ }
100
109
 
101
- DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
102
- DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
110
+ export function calculateResponseSize(xhr: XMLHttpRequest): number {
111
+ const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
112
+ if (contentLengthHeader != null) {
113
+ return contentLengthHeader as number;
114
+ }
103
115
 
104
- DdRumResourceTracking.proxyXhr(xhrType)
105
- }
116
+ const response = xhr.response;
117
+ if (!response) {
118
+ return MISSING_RESOURCE_SIZE;
119
+ }
106
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
+ }
107
155
 
108
- static stopTracking(): void {
109
- if (DdRumResourceTracking.isTracking) {
110
- DdRumResourceTracking.isTracking = false;
111
- XMLHttpRequest.prototype.open = DdRumResourceTracking.originalXhrOpen;
112
- XMLHttpRequest.prototype.send = DdRumResourceTracking.originalXhrSend;
156
+ if (typeof size !== 'number') {
157
+ return MISSING_RESOURCE_SIZE;
113
158
  }
114
- }
115
-
116
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
117
- static proxyXhr(xhrType: any): void {
118
- this.proxyOpen(xhrType);
119
- this.proxySend(xhrType);
120
- }
121
-
122
- private static proxyOpen(xhrType: any): void {
123
- const originalXhrOpen = this.originalXhrOpen;
124
- xhrType.prototype.open = function (this: DdRumXhr, method: string, url: string) {
125
- // Keep track of the method and url
126
- // start time is tracked by the `send` method
127
- const spanId = generateTraceId()
128
- const traceId = generateTraceId()
129
- this._datadog_xhr = {
130
- method,
131
- url: url,
132
- reported: false,
133
- timer: new Timer(),
134
- spanId: spanId,
135
- traceId: traceId
136
- }
137
- // eslint-disable-next-line prefer-rest-params
138
- return originalXhrOpen.apply(this, arguments as any)
159
+ return size;
160
+ }
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
+ };
139
172
  }
140
- }
173
+ return {
174
+ samplingPriorityHeader: '0',
175
+ tracingStrategy: 'DISCARD'
176
+ };
177
+ };
141
178
 
142
- private static proxySend(xhrType: any): void {
143
- const originalXhrSend = this.originalXhrSend;
144
- xhrType.prototype.send = function (this: DdRumXhr) {
179
+ /**
180
+ * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
181
+ */
182
+ export class DdRumResourceTracking {
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
+ );
197
+ }
145
198
 
146
- if (this._datadog_xhr) {
147
- // keep track of start time
148
- this._datadog_xhr.timer.start()
149
- this.setRequestHeader(TRACE_ID_HEADER_KEY, this._datadog_xhr.traceId)
150
- this.setRequestHeader(PARENT_ID_HEADER_KEY, this._datadog_xhr.spanId)
151
- this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM)
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
+ }
152
214
 
153
- }
215
+ DdRumResourceTracking.tracingSamplingRate = tracingSamplingRate;
154
216
 
155
- DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
217
+ DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
218
+ DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
156
219
 
157
- // eslint-disable-next-line prefer-rest-params
158
- return originalXhrSend.apply(this, arguments as any);
220
+ DdRumResourceTracking.proxyXhr(xhrType);
221
+ InternalLog.log(
222
+ 'Datadog SDK is tracking XHR resources',
223
+ SdkVerbosity.INFO
224
+ );
159
225
  }
160
- }
161
-
162
- private static proxyOnReadyStateChange(xhrProxy: DdRumXhr, xhrType: any): void {
163
- const originalOnreadystatechange = xhrProxy.onreadystatechange
164
- xhrProxy.onreadystatechange = function () {
165
- if (xhrProxy.readyState === xhrType.DONE) {
166
- if (!xhrProxy._datadog_xhr.reported) {
167
- DdRumResourceTracking.reportXhr(xhrProxy);
168
- xhrProxy._datadog_xhr.reported = true;
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;
169
234
  }
170
- } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
171
- xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL)
172
- }
173
-
174
- if (originalOnreadystatechange) {
175
- // eslint-disable-next-line prefer-rest-params
176
- originalOnreadystatechange.apply(xhrProxy, arguments as any)
177
- }
178
235
  }
179
- }
180
-
181
- private static reportXhr(xhrProxy: DdRumXhr): void {
182
-
183
- const context = xhrProxy._datadog_xhr
184
-
185
- const key = context.timer.startTime + "/" + context.method
186
-
187
- context.timer.stop()
188
-
189
- DdRum.startResource(
190
- key,
191
- context.method,
192
- context.url,
193
- {
194
- "_dd.span_id": context.spanId,
195
- "_dd.trace_id": context.traceId
196
- },
197
- context.timer.startTime
198
- ).then(() => {
199
- DdRum.stopResource(
200
- key,
201
- xhrProxy.status,
202
- "xhr",
203
- {
204
- "_dd.resource_timings": context.timer.hasTickFor(RESPONSE_START_LABEL) ?
205
- createTimings(
206
- context.timer.startTime,
207
- context.timer.timeAt(RESPONSE_START_LABEL),
208
- context.timer.stopTime
209
- ) : null
210
- },
211
- context.timer.stopTime);
212
- })
213
- }
214
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);
241
+ }
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
+ };
296
+ }
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
+ }
215
361
  }
@@ -4,22 +4,23 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import React from 'react'
8
- import { DdEventsInterceptor } from './DdEventsInterceptor'
9
- import NoOpEventsInterceptor from './NoOpEventsInterceptor'
10
- import type EventsInterceptor from './EventsInterceptor'
7
+ import React from 'react';
11
8
 
9
+ import { InternalLog } from '../../InternalLog';
10
+ import { SdkVerbosity } from '../../SdkVerbosity';
12
11
 
13
- const PROPERTY_FUNCTION_TYPE = "function"
12
+ import { DdEventsInterceptor } from './DdEventsInterceptor';
13
+ import type EventsInterceptor from './EventsInterceptor';
14
+ import NoOpEventsInterceptor from './NoOpEventsInterceptor';
15
+ import { areObjectShallowEqual } from './ShallowObjectEqualityChecker';
14
16
 
15
17
  /**
16
- * Provides RUM auto-instrumentation feature to track user interaction as RUM events.
17
- * For now we are only covering the "onPress" events.
18
- */
18
+ * Provides RUM auto-instrumentation feature to track user interaction as RUM events.
19
+ * For now we are only covering the "onPress" events.
20
+ */
19
21
  export class DdRumUserInteractionTracking {
20
-
21
- private static isTracking = false
22
- private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor()
22
+ private static isTracking = false;
23
+ private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor();
23
24
 
24
25
  /**
25
26
  * Starts tracking user interactions and sends a RUM Action event every time a new interaction was detected.
@@ -29,23 +30,69 @@ export class DdRumUserInteractionTracking {
29
30
  static startTracking(): void {
30
31
  // extra safety to avoid wrapping more than 1 time this function
31
32
  if (DdRumUserInteractionTracking.isTracking) {
32
- return
33
+ InternalLog.log(
34
+ 'Datadog SDK is already tracking interactions',
35
+ SdkVerbosity.WARN
36
+ );
37
+ return;
33
38
  }
34
- DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor()
35
- const original = React.createElement
36
- React.createElement = (element: any, props: any, ...children: any): any => {
39
+ DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor();
40
+ const original = React.createElement;
41
+ React.createElement = (
42
+ element: any,
43
+ props: any,
44
+ ...children: any
45
+ ): any => {
37
46
  // check if we have an 'onPress' property and that this is really a function
38
- if (props && typeof props.onPress === PROPERTY_FUNCTION_TYPE) {
39
- const originalOnPress = props.onPress
47
+ if (props && typeof props.onPress === 'function') {
48
+ const originalOnPress = props.onPress;
40
49
  props.onPress = (...args: any[]) => {
41
- DdRumUserInteractionTracking.eventsInterceptor.interceptOnPress(...args)
42
- return originalOnPress(...args)
43
- }
50
+ DdRumUserInteractionTracking.eventsInterceptor.interceptOnPress(
51
+ ...args
52
+ );
53
+ return originalOnPress(...args);
54
+ };
55
+ // we store the original onPress prop so we can keep memoization working
56
+ props.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__ = originalOnPress;
44
57
  }
45
- return original(element, props, ...children)
46
- }
47
- DdRumUserInteractionTracking.isTracking = true
48
- }
58
+ return original(element, props, ...children);
59
+ };
49
60
 
50
- }
61
+ const originalMemo = React.memo;
62
+ React.memo = (
63
+ component: any,
64
+ propsAreEqual?: (prevProps: any, newProps: any) => boolean
65
+ ) => {
66
+ return originalMemo(component, (prev, next) => {
67
+ if (!next.onPress || !prev.onPress) {
68
+ return propsAreEqual
69
+ ? propsAreEqual(prev, next)
70
+ : areObjectShallowEqual(prev, next);
71
+ }
72
+ // we replace "our" onPress from the props by the original for comparison
73
+ const { onPress: _prevOnPress, ...partialPrevProps } = prev;
74
+ const prevProps = {
75
+ ...partialPrevProps,
76
+ onPress: prev.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__
77
+ };
78
+
79
+ const { onPress: _nextOnPress, ...partialNextProps } = next;
80
+ const nextProps = {
81
+ ...partialNextProps,
82
+ onPress: next.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__
83
+ };
84
+
85
+ // if no comparison function is provided we do shallow comparison
86
+ return propsAreEqual
87
+ ? propsAreEqual(prevProps, nextProps)
88
+ : areObjectShallowEqual(nextProps, prevProps);
89
+ });
90
+ };
51
91
 
92
+ DdRumUserInteractionTracking.isTracking = true;
93
+ InternalLog.log(
94
+ 'Datadog SDK is tracking interactions',
95
+ SdkVerbosity.INFO
96
+ );
97
+ }
98
+ }
@@ -4,20 +4,30 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type Timer from "../../Timer";
8
-
7
+ import type Timer from '../../Timer';
9
8
 
10
9
  export interface DdRumXhr extends XMLHttpRequest {
11
-
12
- _datadog_xhr: DdRumXhrContext
13
-
10
+ _datadog_xhr: DdRumXhrContext;
14
11
  }
15
12
 
16
13
  export interface DdRumXhrContext {
17
- method: string,
18
- url: string,
19
- reported: boolean,
20
- timer: Timer,
21
- spanId: string,
22
- traceId: string
14
+ method: string;
15
+ url: string;
16
+ reported: boolean;
17
+ timer: Timer;
18
+ tracingAttributes: DdRumResourceTracingAttributes;
23
19
  }
20
+
21
+ export type DdRumResourceTracingAttributes =
22
+ | {
23
+ tracingStrategy: 'KEEP';
24
+ traceId: string;
25
+ spanId: string;
26
+ samplingPriorityHeader: '1';
27
+ }
28
+ | {
29
+ tracingStrategy: 'DISCARD';
30
+ traceId?: void;
31
+ spanId?: void;
32
+ samplingPriorityHeader: '0';
33
+ };