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

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 (46) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/android/build.gradle +2 -3
  3. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
  4. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
  5. package/ios/Sources/DdRum.m +10 -9
  6. package/ios/Sources/DdRum.swift +24 -24
  7. package/ios/Sources/DdTrace.m +2 -2
  8. package/ios/Sources/DdTrace.swift +6 -6
  9. package/lib/commonjs/DdSdkReactNative.js +5 -0
  10. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  11. package/lib/commonjs/foundation.js +13 -31
  12. package/lib/commonjs/foundation.js.map +1 -1
  13. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +24 -4
  14. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  15. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +87 -2
  16. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  17. package/lib/commonjs/types.js.map +1 -1
  18. package/lib/commonjs/version.js +10 -0
  19. package/lib/commonjs/version.js.map +1 -0
  20. package/lib/module/DdSdkReactNative.js +4 -0
  21. package/lib/module/DdSdkReactNative.js.map +1 -1
  22. package/lib/module/foundation.js +13 -31
  23. package/lib/module/foundation.js.map +1 -1
  24. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +23 -4
  25. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  26. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +83 -2
  27. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  28. package/lib/module/types.js.map +1 -1
  29. package/lib/module/version.js +3 -0
  30. package/lib/module/version.js.map +1 -0
  31. package/lib/typescript/DdSdkReactNative.d.ts +1 -0
  32. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +1 -0
  33. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  34. package/lib/typescript/types.d.ts +12 -11
  35. package/lib/typescript/version.d.ts +1 -0
  36. package/package.json +3 -3
  37. package/src/DdSdkReactNative.tsx +4 -0
  38. package/src/__tests__/DdRumWrapper.test.tsx +91 -0
  39. package/src/__tests__/DdSdkReactNative.test.tsx +15 -0
  40. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +104 -10
  41. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +265 -15
  42. package/src/foundation.tsx +64 -70
  43. package/src/rum/instrumentation/DdRumErrorTracking.tsx +28 -7
  44. package/src/rum/instrumentation/DdRumResourceTracking.tsx +74 -2
  45. package/src/types.tsx +12 -11
  46. package/src/version.ts +2 -0
@@ -16,6 +16,8 @@ import { TrackingConsent } from '../TrackingConsent'
16
16
  import { NativeSdkVerbosity } from '../NativeSdkVerbosity'
17
17
  import { ProxyType } from '../ProxyConfiguration'
18
18
 
19
+ import { version as sdkVersion } from '../version';
20
+
19
21
  jest.mock('react-native', () => {
20
22
  return {
21
23
  NativeModules: {
@@ -86,6 +88,7 @@ it('M initialize the SDK W initialize', async () => {
86
88
  expect(ddSdkConfiguration.trackingConsent).toBe(TrackingConsent.GRANTED)
87
89
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
88
90
  '_dd.source': 'react-native',
91
+ '_dd.sdk_version': sdkVersion,
89
92
  '_dd.native_view_tracking': false,
90
93
  '_dd.long_task.threshold': 200
91
94
  })
@@ -112,6 +115,7 @@ it('M give rejection W initialize', async () => {
112
115
  expect(ddSdkConfiguration.trackingConsent).toBe(TrackingConsent.GRANTED)
113
116
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
114
117
  '_dd.source': 'react-native',
118
+ '_dd.sdk_version': sdkVersion,
115
119
  '_dd.native_view_tracking': false,
116
120
  '_dd.long_task.threshold': 200
117
121
  })
@@ -145,6 +149,7 @@ it('M initialize the SDK W initialize { explicit tracking consent }', async () =
145
149
  expect(ddSdkConfiguration.trackingConsent).toBe(fakeConsent)
146
150
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
147
151
  '_dd.source': 'react-native',
152
+ '_dd.sdk_version': sdkVersion,
148
153
  '_dd.native_view_tracking': false,
149
154
  '_dd.long_task.threshold': 200
150
155
  })
@@ -173,6 +178,7 @@ it('M initialize once W initialize { multiple times in a row }', async () => {
173
178
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
174
179
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
175
180
  '_dd.source': 'react-native',
181
+ '_dd.sdk_version': sdkVersion,
176
182
  '_dd.native_view_tracking': false,
177
183
  '_dd.long_task.threshold': 200
178
184
  })
@@ -198,6 +204,7 @@ it('M enable user interaction feature W initialize { user interaction config ena
198
204
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
199
205
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
200
206
  '_dd.source': 'react-native',
207
+ '_dd.sdk_version': sdkVersion,
201
208
  '_dd.native_view_tracking': false,
202
209
  '_dd.long_task.threshold': 200
203
210
  })
@@ -224,6 +231,7 @@ it('M enable resource tracking feature W initialize { resource tracking config e
224
231
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
225
232
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
226
233
  '_dd.source': 'react-native',
234
+ '_dd.sdk_version': sdkVersion,
227
235
  '_dd.native_view_tracking': false,
228
236
  '_dd.long_task.threshold': 200
229
237
  })
@@ -250,6 +258,7 @@ it('M enable error tracking feature W initialize { error tracking config enabled
250
258
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
251
259
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
252
260
  '_dd.source': 'react-native',
261
+ '_dd.sdk_version': sdkVersion,
253
262
  '_dd.native_view_tracking': false,
254
263
  '_dd.long_task.threshold': 200
255
264
  })
@@ -278,6 +287,7 @@ it('M enable custom service name W initialize { service name }', async () => {
278
287
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
279
288
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
280
289
  '_dd.source': 'react-native',
290
+ '_dd.sdk_version': sdkVersion,
281
291
  '_dd.service_name': fakeServiceName,
282
292
  '_dd.native_view_tracking': false,
283
293
  '_dd.long_task.threshold': 200
@@ -306,6 +316,7 @@ it('M enable sdk verbosity W initialize { sdk verbosity }', async () => {
306
316
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
307
317
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
308
318
  '_dd.source': 'react-native',
319
+ '_dd.sdk_version': sdkVersion,
309
320
  '_dd.sdk_verbosity': NativeSdkVerbosity.DEBUG,
310
321
  '_dd.native_view_tracking': false,
311
322
  '_dd.long_task.threshold': 200
@@ -334,6 +345,7 @@ it('M enable native view tracking W initialize { native_view_tracking enabled }'
334
345
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
335
346
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
336
347
  '_dd.source': 'react-native',
348
+ '_dd.sdk_version': sdkVersion,
337
349
  '_dd.native_view_tracking': true,
338
350
  '_dd.long_task.threshold': 200
339
351
  })
@@ -416,6 +428,7 @@ describe.each([[ProxyType.HTTP], [ProxyType.HTTPS], [ProxyType.SOCKS]])('proxy c
416
428
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
417
429
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
418
430
  '_dd.source': 'react-native',
431
+ '_dd.sdk_version': sdkVersion,
419
432
  '_dd.native_view_tracking': false,
420
433
  '_dd.proxy.type': proxyType,
421
434
  '_dd.proxy.address': proxyAddress,
@@ -467,6 +480,7 @@ describe.each([[ProxyType.HTTP], [ProxyType.HTTPS]])('proxy configs test + auth'
467
480
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
468
481
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
469
482
  '_dd.source': 'react-native',
483
+ '_dd.sdk_version': sdkVersion,
470
484
  '_dd.native_view_tracking': false,
471
485
  '_dd.proxy.type': proxyType,
472
486
  '_dd.proxy.address': proxyAddress,
@@ -513,6 +527,7 @@ it('M log a warning W initialize { with socks proxy config + proxy credentials }
513
527
  expect(ddSdkConfiguration.env).toBe(fakeEnvName)
514
528
  expect(ddSdkConfiguration.additionalConfig).toStrictEqual({
515
529
  '_dd.source': 'react-native',
530
+ '_dd.sdk_version': sdkVersion,
516
531
  '_dd.native_view_tracking': false,
517
532
  '_dd.proxy.type': proxyType,
518
533
  '_dd.proxy.address': proxyAddress,
@@ -51,12 +51,36 @@ afterEach(() => {
51
51
  console.error = originalConsoleError
52
52
  })
53
53
 
54
+ it('M intercept and send a RUM event W onGlobalError() {no message}', async () => {
55
+ // GIVEN
56
+ DdRumErrorTracking.startTracking();
57
+ const is_fatal = Math.random() < 0.5;
58
+ const error = {
59
+ stack: ["doSomething() at ./path/to/file.js:67:3"]
60
+ };
61
+
62
+ // WHEN
63
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
64
+ await flushPromises();
65
+
66
+ // THEN
67
+ expect(DdRum.addError.mock.calls.length).toBe(1);
68
+ expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
69
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
70
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3");
71
+ const attributes = DdRum.addError.mock.calls[0][3];
72
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
73
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
74
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
75
+ })
54
76
 
55
77
  it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', async () => {
56
78
  // GIVEN
57
79
  DdRumErrorTracking.startTracking();
58
80
  const is_fatal = Math.random() < 0.5;
59
- const error = new Error('Something bad happened');
81
+ const error = {
82
+ message: "Something bad happened"
83
+ };
60
84
 
61
85
  // WHEN
62
86
  DdRumErrorTracking.onGlobalError(error, is_fatal);
@@ -64,7 +88,7 @@ it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', asy
64
88
 
65
89
  // THEN
66
90
  expect(DdRum.addError.mock.calls.length).toBe(1);
67
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
91
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
68
92
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
69
93
  expect(DdRum.addError.mock.calls[0][2]).toBe("");
70
94
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -73,6 +97,28 @@ it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', asy
73
97
  expect(baseErrorHandlerCalled).toStrictEqual(true);
74
98
  })
75
99
 
100
+ it('M intercept and send a RUM event W onGlobalError() {Error object}', async () => {
101
+ // GIVEN
102
+ DdRumErrorTracking.startTracking();
103
+ const is_fatal = Math.random() < 0.5;
104
+ const error = new Error('Something bad happened');
105
+
106
+ // WHEN
107
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
108
+ await flushPromises();
109
+
110
+ // THEN
111
+ expect(DdRum.addError.mock.calls.length).toBe(1);
112
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
113
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
114
+ expect(DdRum.addError.mock.calls[0][2]).toContain("Error: Something bad happened");
115
+ expect(DdRum.addError.mock.calls[0][2]).toContain("/packages/core/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx");
116
+ const attributes = DdRum.addError.mock.calls[0][3];
117
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
118
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
119
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
120
+ })
121
+
76
122
 
77
123
  it('M intercept and send a RUM event W onGlobalError() {with source file info}', async () => {
78
124
  // GIVEN
@@ -91,7 +137,7 @@ it('M intercept and send a RUM event W onGlobalError() {with source file info}',
91
137
 
92
138
  // THEN
93
139
  expect(DdRum.addError.mock.calls.length).toBe(1);
94
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
140
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
95
141
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
96
142
  expect(DdRum.addError.mock.calls[0][2]).toBe("at ./path/to/file.js:1038:57");
97
143
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -116,7 +162,55 @@ it('M intercept and send a RUM event W onGlobalError() {with component stack}',
116
162
 
117
163
  // THEN
118
164
  expect(DdRum.addError.mock.calls.length).toBe(1);
119
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
165
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
166
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
167
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
168
+ const attributes = DdRum.addError.mock.calls[0][3];
169
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
170
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
171
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
172
+ })
173
+
174
+ it('M intercept and send a RUM event W onGlobalError() {with stack}', async () => {
175
+ // GIVEN
176
+ DdRumErrorTracking.startTracking();
177
+ const is_fatal = Math.random() < 0.5;
178
+ const error = {
179
+ stack: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
180
+ message: "Something bad happened"
181
+ };
182
+
183
+ // WHEN
184
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
185
+ await flushPromises();
186
+
187
+ // THEN
188
+ expect(DdRum.addError.mock.calls.length).toBe(1);
189
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
190
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
191
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
192
+ const attributes = DdRum.addError.mock.calls[0][3];
193
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
194
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
195
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
196
+ })
197
+
198
+ it('M intercept and send a RUM event W onGlobalError() {with stacktrace}', async () => {
199
+ // GIVEN
200
+ DdRumErrorTracking.startTracking();
201
+ const is_fatal = Math.random() < 0.5;
202
+ const error = {
203
+ stacktrace: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
204
+ message: "Something bad happened"
205
+ };
206
+
207
+ // WHEN
208
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
209
+ await flushPromises();
210
+
211
+ // THEN
212
+ expect(DdRum.addError.mock.calls.length).toBe(1);
213
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
120
214
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
121
215
  expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
122
216
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -145,7 +239,7 @@ it('M not report error in console handler W onGlobalError() {with console report
145
239
 
146
240
  // THEN
147
241
  expect(DdRum.addError.mock.calls.length).toBe(1);
148
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
242
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
149
243
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
150
244
  expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
151
245
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -158,12 +252,12 @@ it('M not report error in console handler W onGlobalError() {with console report
158
252
  it('M intercept and send a RUM event W onConsole() {Error with source file info}', async () => {
159
253
  // GIVEN
160
254
  DdRumErrorTracking.startTracking();
161
- const message= "Something bad happened";
255
+ const message= "Oops I did it again!";
162
256
  const error = {
163
257
  sourceURL: "./path/to/file.js",
164
258
  line: 1038,
165
259
  column: 57,
166
- message: message
260
+ message: "Something bad happened"
167
261
  };
168
262
 
169
263
  // WHEN
@@ -172,7 +266,7 @@ it('M intercept and send a RUM event W onConsole() {Error with source file info}
172
266
 
173
267
  // THEN
174
268
  expect(DdRum.addError.mock.calls.length).toBe(1);
175
- expect(DdRum.addError.mock.calls[0][0]).toBe(message + " " + JSON.stringify(error));
269
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Oops I did it again! Something bad happened");
176
270
  expect(DdRum.addError.mock.calls[0][1]).toBe("CONSOLE");
177
271
  expect(DdRum.addError.mock.calls[0][2]).toBe("at ./path/to/file.js:1038:57");
178
272
  expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
@@ -182,7 +276,7 @@ it('M intercept and send a RUM event W onConsole() {Error with source file info}
182
276
  it('M intercept and send a RUM event W onConsole() {Error with component stack}', async () => {
183
277
  // GIVEN
184
278
  DdRumErrorTracking.startTracking();
185
- const message= "Something bad happened";
279
+ const message= "Oops I did it again!";
186
280
  const error = {
187
281
  componentStack: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
188
282
  message: "Something bad happened"
@@ -194,7 +288,7 @@ it('M intercept and send a RUM event W onConsole() {Error with component stack}'
194
288
 
195
289
  // THEN
196
290
  expect(DdRum.addError.mock.calls.length).toBe(1);
197
- expect(DdRum.addError.mock.calls[0][0]).toBe(message + " " + JSON.stringify(error));
291
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Oops I did it again! Something bad happened");
198
292
  expect(DdRum.addError.mock.calls[0][1]).toBe("CONSOLE");
199
293
  expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
200
294
  expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
@@ -4,7 +4,16 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { DdRumResourceTracking, PARENT_ID_HEADER_KEY, TRACE_ID_HEADER_KEY, ORIGIN_RUM, ORIGIN_HEADER_KEY } from '../../../rum/instrumentation/DdRumResourceTracking'
7
+ import {
8
+ DdRumResourceTracking,
9
+ PARENT_ID_HEADER_KEY,
10
+ TRACE_ID_HEADER_KEY,
11
+ ORIGIN_RUM,
12
+ ORIGIN_HEADER_KEY,
13
+ SAMPLING_PRIORITY_HEADER_KEY,
14
+ SAMPLED_HEADER_KEY,
15
+ calculateResponseSize
16
+ } from '../../../rum/instrumentation/DdRumResourceTracking'
8
17
  import { DdRum } from '../../../index';
9
18
  import { Platform } from 'react-native';
10
19
 
@@ -24,6 +33,10 @@ jest.mock('../../../foundation', () => {
24
33
  };
25
34
  });
26
35
 
36
+ function randomInt(max: number): number {
37
+ return Math.floor(Math.random() * max)
38
+ }
39
+
27
40
  class XMLHttpRequest {
28
41
 
29
42
  static readonly UNSENT = 0;
@@ -32,10 +45,12 @@ class XMLHttpRequest {
32
45
  static readonly LOADING = 3;
33
46
  static readonly DONE = 4;
34
47
 
35
- public response: string | undefined = undefined
36
- public status: number | undefined = undefined
37
- public readyState: number = XMLHttpRequest.UNSENT
38
- public headers: Map<String, String> = new Map()
48
+ public response?: any;
49
+ public responseType?: string;
50
+ public status: number = 0;
51
+ public readyState: number = XMLHttpRequest.UNSENT;
52
+ public requestHeaders: Map<String, String> = new Map();
53
+ public responseHeaders: Map<String, String> = new Map();
39
54
 
40
55
  constructor() {
41
56
 
@@ -64,15 +79,26 @@ class XMLHttpRequest {
64
79
  this.onreadystatechange()
65
80
  }
66
81
 
67
- complete(status: number, response?: string) {
82
+ complete(status: number, response?: any, responseType?: string) {
68
83
  this.response = response
84
+ if (response) {
85
+ this.responseType = responseType ?? 'text'
86
+ }
69
87
  this.status = status
70
88
  this.readyState = XMLHttpRequest.DONE
71
89
  this.onreadystatechange()
72
90
  }
73
91
 
74
92
  setRequestHeader(header: string, value: string): void {
75
- this.headers[header] = value
93
+ this.requestHeaders[header] = value
94
+ }
95
+
96
+ setResponseHeader(header: string, value: string): void {
97
+ this.responseHeaders[header] = value
98
+ }
99
+
100
+ getResponseHeader(header: string): string | null {
101
+ return this.responseHeaders[header];
76
102
  }
77
103
  }
78
104
 
@@ -122,6 +148,7 @@ it('M intercept XHR request W startTracking() + XHR.open() + XHR.send()', async
122
148
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
123
149
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(200);
124
150
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
151
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
125
152
 
126
153
  expect(xhr.originalOpenCalled).toBe(true);
127
154
  expect(xhr.originalSendCalled).toBe(true);
@@ -151,6 +178,7 @@ it('M intercept failing XHR request W startTracking() + XHR.open() + XHR.send()'
151
178
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
152
179
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(500);
153
180
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
181
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
154
182
 
155
183
  expect(xhr.originalOpenCalled).toBe(true);
156
184
  expect(xhr.originalSendCalled).toBe(true);
@@ -180,6 +208,7 @@ it('M intercept aborted XHR request W startTracking() + XHR.open() + XHR.send()
180
208
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
181
209
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(0);
182
210
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
211
+ expect(DdRum.stopResource.mock.calls[0][3]).toBe(-1);
183
212
 
184
213
  expect(xhr.originalOpenCalled).toBe(true);
185
214
  expect(xhr.originalSendCalled).toBe(true);
@@ -202,7 +231,7 @@ it('M add the span id in the request headers W startTracking() + XHR.open() + XH
202
231
  await flushPromises();
203
232
 
204
233
  // THEN
205
- const spanId = xhr.headers[PARENT_ID_HEADER_KEY];
234
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
206
235
  expect(spanId).toBeDefined();
207
236
  expect(spanId).toMatch(/[1-9].+/);
208
237
  })
@@ -222,7 +251,7 @@ it('M add the trace id in the request headers W startTracking() + XHR.open() + X
222
251
  await flushPromises();
223
252
 
224
253
  // THEN
225
- const traceId = xhr.headers[TRACE_ID_HEADER_KEY];
254
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
226
255
  expect(traceId).toBeDefined();
227
256
  expect(traceId).toMatch(/[1-9].+/);
228
257
  })
@@ -242,8 +271,8 @@ it('M generate different ids for spanId and traceId in request headers', async (
242
271
  await flushPromises();
243
272
 
244
273
  // THEN
245
- const traceId = xhr.headers[TRACE_ID_HEADER_KEY];
246
- const spanId = xhr.headers[PARENT_ID_HEADER_KEY];
274
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
275
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
247
276
  expect(traceId !== spanId).toBeTruthy()
248
277
  })
249
278
 
@@ -262,7 +291,43 @@ it('M add origin as RUM in the request headers W startTracking() + XHR.open() +
262
291
  await flushPromises();
263
292
 
264
293
  // THEN
265
- expect(xhr.headers[ORIGIN_HEADER_KEY]).toBe(ORIGIN_RUM)
294
+ expect(xhr.requestHeaders[ORIGIN_HEADER_KEY]).toBe(ORIGIN_RUM)
295
+ })
296
+
297
+ it('M force the agent to keep the request generated trace W startTracking() + XHR.open() + XHR.send()', async () => {
298
+ // GIVEN
299
+ let method = "GET"
300
+ let url = "https://api.example.com/v2/user"
301
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
302
+
303
+ // WHEN
304
+ const xhr = new XMLHttpRequest();
305
+ xhr.open(method, url);
306
+ xhr.send();
307
+ xhr.notifyResponseArrived();
308
+ xhr.complete(200, 'ok');
309
+ await flushPromises();
310
+
311
+ // THEN
312
+ expect(xhr.requestHeaders[SAMPLING_PRIORITY_HEADER_KEY]).toBe("1")
313
+ })
314
+
315
+ it('M mark the request generated trace for sampling W startTracking() + XHR.open() + XHR.send()', async () => {
316
+ // GIVEN
317
+ let method = "GET"
318
+ let url = "https://api.example.com/v2/user"
319
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
320
+
321
+ // WHEN
322
+ const xhr = new XMLHttpRequest();
323
+ xhr.open(method, url);
324
+ xhr.send();
325
+ xhr.notifyResponseArrived();
326
+ xhr.complete(200, 'ok');
327
+ await flushPromises();
328
+
329
+ // THEN
330
+ expect(xhr.requestHeaders[SAMPLED_HEADER_KEY]).toBe("1")
266
331
  })
267
332
 
268
333
  it('M add the span id as resource attributes W startTracking() + XHR.open() + XHR.send()', async () => {
@@ -345,7 +410,7 @@ describe.each([['android'], ['ios']])('timings test', (platform) => {
345
410
  await flushPromises();
346
411
 
347
412
  // THEN
348
- const timings = DdRum.stopResource.mock.calls[0][3]["_dd.resource_timings"];
413
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
349
414
 
350
415
  if (Platform.OS === 'ios') {
351
416
  expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
@@ -385,7 +450,7 @@ describe.each([['android'], ['ios']])('timings test', (platform) => {
385
450
  await flushPromises();
386
451
 
387
452
  // THEN
388
- const timings = DdRum.stopResource.mock.calls[0][3]["_dd.resource_timings"];
453
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
389
454
 
390
455
  if (Platform.OS === 'ios') {
391
456
  expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
@@ -421,7 +486,192 @@ it('M not generate resource timings W startTracking() + XHR.open() + XHR.send()
421
486
  await flushPromises();
422
487
 
423
488
  // THEN
424
- const attributes = DdRum.stopResource.mock.calls[0][3];
489
+ const attributes = DdRum.stopResource.mock.calls[0][4];
425
490
 
426
491
  expect(attributes['_dd.resource_timings']).toBeNull()
427
492
  })
493
+
494
+ describe.each([
495
+ 'blob',
496
+ 'arraybuffer',
497
+ 'text',
498
+ '',
499
+ 'json'
500
+ ].map((responseType) => {
501
+ const xhr = new XMLHttpRequest();
502
+ xhr.readyState = XMLHttpRequest.DONE;
503
+ xhr.responseType = responseType;
504
+ xhr.response = {};
505
+
506
+ const contentLength = randomInt(1_000_000_000)
507
+ xhr.setResponseHeader('Content-Length', contentLength.toString());
508
+ return [xhr, responseType, contentLength]
509
+ }))('Response size from response header', (xhr, responseType, expectedSize) => {
510
+ if (responseType === '') {
511
+ responseType = '_empty_'
512
+ }
513
+ it(`M calculate response size W calculateResponseSize(), responseType=${responseType}`, () => {
514
+ // WHEN
515
+ // @ts-ignore
516
+ const size = calculateResponseSize(xhr);
517
+
518
+ // THEN
519
+ expect(size).toEqual(expectedSize);
520
+ })
521
+ })
522
+
523
+ it('M calculate response size W calculateResponseSize() { responseType=blob }', () => {
524
+ // GIVEN
525
+ const xhr = new XMLHttpRequest();
526
+ xhr.readyState = XMLHttpRequest.DONE;
527
+ xhr.responseType = 'blob';
528
+
529
+ const expectedSize = randomInt(1_000_000)
530
+ xhr.response = { get size() { return expectedSize } }
531
+
532
+ // WHEN
533
+ // @ts-ignore
534
+ const size = calculateResponseSize(xhr);
535
+
536
+ // THEN
537
+ expect(size).toEqual(expectedSize);
538
+ })
539
+
540
+ it('M calculate response size W calculateResponseSize() { responseType=arraybuffer }', () => {
541
+ // GIVEN
542
+ const xhr = new XMLHttpRequest();
543
+ xhr.readyState = XMLHttpRequest.DONE;
544
+ xhr.responseType = 'arraybuffer';
545
+
546
+ const expectedSize = randomInt(100_000);
547
+ xhr.response = new ArrayBuffer(expectedSize);
548
+
549
+ // WHEN
550
+ // @ts-ignore
551
+ const size = calculateResponseSize(xhr);
552
+
553
+ // THEN
554
+ expect(size).toEqual(expectedSize);
555
+ })
556
+
557
+ it('M calculate response size W calculateResponseSize() { responseType=text }', () => {
558
+ // GIVEN
559
+ const xhr = new XMLHttpRequest();
560
+ xhr.readyState = XMLHttpRequest.DONE;
561
+ xhr.responseType = 'text';
562
+
563
+ // size per char is 24, but in bytes it is 33.
564
+ const expectedSize = 33;
565
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
566
+
567
+ // WHEN
568
+ // @ts-ignore
569
+ const size = calculateResponseSize(xhr);
570
+
571
+ // THEN
572
+ expect(size).toEqual(expectedSize);
573
+ })
574
+
575
+ it('M calculate response size W calculateResponseSize() { responseType=_empty_ }', () => {
576
+ // GIVEN
577
+ const xhr = new XMLHttpRequest();
578
+ xhr.readyState = XMLHttpRequest.DONE;
579
+ xhr.responseType = '';
580
+
581
+ // size per char is 24, but in bytes it is 33.
582
+ const expectedSize = 33;
583
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
584
+
585
+ // WHEN
586
+ // @ts-ignore
587
+ const size = calculateResponseSize(xhr);
588
+
589
+ // THEN
590
+ expect(size).toEqual(expectedSize);
591
+ })
592
+
593
+ it('M calculate response size W calculateResponseSize() { responseType=json }', () => {
594
+ // GIVEN
595
+ const xhr = new XMLHttpRequest();
596
+ xhr.readyState = XMLHttpRequest.DONE;
597
+ xhr.responseType = 'json';
598
+
599
+ const expectedSize = 24;
600
+ xhr.response = { "foo": { "bar": "foobar" } }
601
+
602
+ // WHEN
603
+ // @ts-ignore
604
+ const size = calculateResponseSize(xhr);
605
+
606
+ // THEN
607
+ expect(size).toEqual(expectedSize);
608
+ })
609
+
610
+ it('M not calculate response size W calculateResponseSize() { responseType=document }', () => {
611
+ // GIVEN
612
+ const xhr = new XMLHttpRequest();
613
+ xhr.readyState = XMLHttpRequest.DONE;
614
+ // document type is not supported by RN, so there are no classes to handle it
615
+ xhr.responseType = 'document';
616
+ xhr.response = {};
617
+
618
+ // WHEN
619
+ // @ts-ignore
620
+ const size = calculateResponseSize(xhr);
621
+
622
+ // THEN
623
+ expect(size).toEqual(-1);
624
+ })
625
+
626
+ it('M return 0 W calculateResponseSize() { error is thrown }', () => {
627
+ // GIVEN
628
+ const consoleErrorMock = jest.spyOn(console, 'error').mockImplementation()
629
+ const xhr = new XMLHttpRequest();
630
+ xhr.readyState = XMLHttpRequest.DONE;
631
+ xhr.responseType = 'blob';
632
+ xhr.response = {
633
+ get size() { throw new Error() }
634
+ };
635
+
636
+ // WHEN
637
+ // @ts-ignore
638
+ const size = calculateResponseSize(xhr);
639
+
640
+ // THEN
641
+ expect(size).toEqual(-1);
642
+ expect(consoleErrorMock).toHaveBeenCalled();
643
+
644
+ consoleErrorMock.mockRestore();
645
+ })
646
+
647
+ it('M return 0 W calculateResponseSize() { size is not a number }', () => {
648
+ // GIVEN
649
+ const xhr = new XMLHttpRequest();
650
+ xhr.readyState = XMLHttpRequest.DONE;
651
+ xhr.responseType = 'blob';
652
+
653
+ // we pass empty object, so that .size property is missing, we will get undefined
654
+ xhr.response = {};
655
+
656
+ // WHEN
657
+ // @ts-ignore
658
+ const size = calculateResponseSize(xhr);
659
+
660
+ // THEN
661
+ expect(size).toEqual(-1);
662
+ })
663
+
664
+ it('M return 0 W calculateResponseSize() { no response }', () => {
665
+ // GIVEN
666
+ const xhr = new XMLHttpRequest();
667
+ xhr.readyState = XMLHttpRequest.DONE;
668
+ xhr.responseType = 'blob';
669
+ xhr.response = null;
670
+
671
+ // WHEN
672
+ // @ts-ignore
673
+ const size = calculateResponseSize(xhr);
674
+
675
+ // THEN
676
+ expect(size).toEqual(-1);
677
+ })