@datadog/mobile-react-native 1.0.0-rc8 → 1.1.0

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 (192) hide show
  1. package/DatadogSDKReactNative.podspec +2 -1
  2. package/android/build.gradle +73 -3
  3. package/android/gradle.properties +1 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +54 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +86 -0
  6. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdLogs.kt +27 -11
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt +305 -0
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +280 -0
  9. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkBridgeExt.kt +2 -2
  10. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +29 -0
  11. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -4
  12. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -0
  13. package/android/src/main/kotlin/com/datadog/reactnative/DdTrace.kt +86 -0
  14. package/android/src/main/kotlin/com/datadog/reactnative/GlobalState.kt +32 -0
  15. package/android/src/main/kotlin/com/datadog/reactnative/NoOpViewTrackingStrategy.kt +23 -0
  16. package/android/src/main/kotlin/com/datadog/reactnative/ProxyAuthenticator.kt +60 -0
  17. package/android/src/test/kotlin/com/datadog/reactnative/DdLogsTest.kt +170 -0
  18. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +408 -0
  19. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +1547 -0
  20. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +306 -0
  21. package/android/src/test/kotlin/com/datadog/reactnative/GlobalStateTest.kt +118 -0
  22. package/android/src/test/kotlin/com/datadog/reactnative/ProxyAuthenticatorTest.kt +123 -0
  23. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +33 -0
  24. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +43 -0
  25. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +22 -0
  26. package/android/src/test/kotlin/com/datadog/tools/unit/ReflectUtils.kt +267 -0
  27. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +26 -0
  28. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +33 -0
  29. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +20 -0
  30. package/android/src/test/kotlin/com/datadog/tools/unit/forge/Throwable.kt +31 -0
  31. package/android/src/test/kotlin/com/datadog/tools/unit/forge/ThrowableForgeryFactory.kt +21 -0
  32. package/android/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +1 -0
  33. package/datadog-sourcemaps.gradle +137 -0
  34. package/ios/Sources/AnyEncodable.swift +138 -0
  35. package/ios/Sources/Attributes.swift +101 -0
  36. package/ios/Sources/DdLogs.swift +35 -11
  37. package/ios/Sources/DdRum.m +3 -1
  38. package/ios/Sources/DdRum.swift +184 -22
  39. package/ios/Sources/DdSdk.swift +190 -10
  40. package/ios/Sources/DdSdkConfiguration.swift +51 -0
  41. package/ios/Sources/DdTrace.swift +41 -6
  42. package/ios/Sources/GlobalState.swift +28 -0
  43. package/ios/Sources/QueueHolder.swift +10 -0
  44. package/ios/Sources/RNDdSdkConfiguration.swift +5 -6
  45. package/lib/commonjs/DdSdkReactNative.js +16 -2
  46. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  47. package/lib/commonjs/DdSdkReactNativeConfiguration.js +24 -0
  48. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  49. package/lib/commonjs/foundation.js +36 -2
  50. package/lib/commonjs/foundation.js.map +1 -1
  51. package/lib/commonjs/nativeModulesTypes.js +36 -0
  52. package/lib/commonjs/nativeModulesTypes.js.map +1 -0
  53. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +4 -17
  54. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  55. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +4 -4
  56. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  57. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
  58. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  59. package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
  60. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  61. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +51 -0
  62. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  63. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
  64. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  65. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  66. package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  67. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
  68. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  69. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
  70. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  71. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
  72. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  73. package/lib/commonjs/rum/instrumentation/{URLHostParser.js → resourceTracking/implementation/URLHostParser.js} +16 -7
  74. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  75. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
  76. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  77. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
  78. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  79. package/lib/commonjs/types.js +26 -1
  80. package/lib/commonjs/types.js.map +1 -1
  81. package/lib/commonjs/version.js +1 -1
  82. package/lib/commonjs/version.js.map +1 -1
  83. package/lib/module/DdSdkReactNative.js +15 -1
  84. package/lib/module/DdSdkReactNative.js.map +1 -1
  85. package/lib/module/DdSdkReactNativeConfiguration.js +24 -0
  86. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  87. package/lib/module/foundation.js +36 -2
  88. package/lib/module/foundation.js.map +1 -1
  89. package/lib/module/nativeModulesTypes.js +26 -0
  90. package/lib/module/nativeModulesTypes.js.map +1 -0
  91. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +2 -14
  92. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  93. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +3 -4
  94. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  95. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
  96. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  97. package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
  98. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  99. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +34 -0
  100. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  101. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
  102. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  103. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  104. package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  105. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
  106. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  107. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
  108. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  109. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
  110. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  111. package/{src/rum/instrumentation/URLHostParser.ts → lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js} +15 -8
  112. package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  113. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
  114. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  115. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
  116. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  117. package/lib/module/types.js +23 -0
  118. package/lib/module/types.js.map +1 -1
  119. package/lib/module/version.js +1 -1
  120. package/lib/module/version.js.map +1 -1
  121. package/lib/typescript/DdSdkReactNative.d.ts +2 -0
  122. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +20 -0
  123. package/lib/typescript/foundation.d.ts +3 -2
  124. package/lib/typescript/nativeModulesTypes.d.ts +168 -0
  125. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  126. package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  127. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  128. package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  129. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  130. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +20 -0
  131. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  132. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  133. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  134. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  135. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  136. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  137. package/lib/typescript/types.d.ts +42 -7
  138. package/lib/typescript/version.d.ts +1 -1
  139. package/package.json +7 -5
  140. package/src/DdSdkReactNative.tsx +21 -1
  141. package/src/DdSdkReactNativeConfiguration.tsx +20 -0
  142. package/src/__tests__/DdSdkReactNative.test.tsx +87 -8
  143. package/src/__tests__/foundation.test.ts +79 -0
  144. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +9 -9
  145. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
  146. package/src/foundation.tsx +40 -11
  147. package/src/nativeModulesTypes.ts +258 -0
  148. package/src/rum/instrumentation/DdEventsInterceptor.tsx +2 -17
  149. package/src/rum/instrumentation/DdRumErrorTracking.tsx +3 -4
  150. package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
  151. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
  152. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
  153. package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
  154. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
  155. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
  156. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +52 -0
  157. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
  158. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +27 -0
  159. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
  160. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
  161. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
  162. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
  163. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
  164. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
  165. package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
  166. package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
  167. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/implementation/__tests__}/URLHostParser.test.ts +3 -3
  168. package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +83 -49
  169. package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
  170. package/src/types.tsx +62 -7
  171. package/src/version.ts +1 -1
  172. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +0 -142
  173. package/android/src/main/java/com/datadog/reactnative/DdSdk.kt +0 -67
  174. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +0 -51
  175. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -356
  176. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  177. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
  178. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
  179. package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +0 -1
  180. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -331
  181. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  182. package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
  183. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
  184. package/lib/module/rum/instrumentation/URLHostParser.js +0 -24
  185. package/lib/module/rum/instrumentation/URLHostParser.js.map +0 -1
  186. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -40
  187. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
  188. package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
  189. package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +0 -6
  190. package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -426
  191. package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
  192. package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { filterDevResource } from '../internalDevResourceBlocklist';
8
+
9
+ import { ResourceMockFactory } from './__utils__/ResourceMockFactory';
10
+
11
+ const resourceMockFactory = new ResourceMockFactory();
12
+
13
+ describe('internalDevResourceBlocklist', () => {
14
+ describe('filterDevResource', () => {
15
+ it.each(['192.168.1.20', '10.46.29.155', '172.28.1.20'])(
16
+ 'returns null when a expo logs call with ip %s is made',
17
+ ip => {
18
+ const resource = resourceMockFactory.getCustomResource({
19
+ request: {
20
+ method: 'GET',
21
+ url: `http://${ip}:8081/logs`,
22
+ kind: 'xhr'
23
+ }
24
+ });
25
+ expect(filterDevResource(resource)).toBeNull();
26
+ }
27
+ );
28
+ it('returns null when an expo logs call with custom port is made', () => {
29
+ const resource = resourceMockFactory.getCustomResource({
30
+ request: {
31
+ method: 'GET',
32
+ url: 'http://10.46.29.155:19000/logs',
33
+ kind: 'xhr'
34
+ }
35
+ });
36
+ expect(filterDevResource(resource)).toBeNull();
37
+ });
38
+ it('returns null when a rn symbolicate call is made', () => {
39
+ const resource = resourceMockFactory.getCustomResource({
40
+ request: {
41
+ method: 'GET',
42
+ url: 'http://localhost:8081/symbolicate',
43
+ kind: 'xhr'
44
+ }
45
+ });
46
+ expect(filterDevResource(resource)).toBeNull();
47
+ });
48
+ it('returns null when an rn symbolicate call with custom port is made', () => {
49
+ const resource = resourceMockFactory.getCustomResource({
50
+ request: {
51
+ method: 'GET',
52
+ url: 'http://localhost:40/symbolicate',
53
+ kind: 'xhr'
54
+ }
55
+ });
56
+ expect(filterDevResource(resource)).toBeNull();
57
+ });
58
+ it('returns the resource when the resource is not a dev resource', () => {
59
+ const resource = resourceMockFactory.getCustomResource({
60
+ request: {
61
+ method: 'GET',
62
+ url: 'http://11.22.33.22:443/logs',
63
+ kind: 'xhr'
64
+ }
65
+ });
66
+ expect(filterDevResource(resource)).not.toBeNull();
67
+ });
68
+ });
69
+ });
@@ -0,0 +1,50 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type { RUMResource } from '../../domain/interfaces/RumResource';
8
+
9
+ /**
10
+ * Expo sends all console.* calls to the packager. As we log all API calls
11
+ * when the SDKVerbosity is DEBUG, this would result in an infinite loop of
12
+ * `console.log` and API calls, creating 250 RUM resources per second.
13
+ *
14
+ * The hostname is always going to be localhost or a local IP.
15
+ *
16
+ * An example URL is http://192.168.1.20:8081/logs or http://10.46.29.155:19000/logs
17
+ */
18
+ const EXPO_DEV_LOGS_REGEX = new RegExp(
19
+ '^http://((10|172|192).[0-9]+.[0-9]+.[0-9]+|localhost):[0-9]+/logs$'
20
+ );
21
+
22
+ /**
23
+ * This call is made every time the RN packager reloads the js in dev mode.
24
+ */
25
+ const RN_PACKAGER_SYMBOLICATE_REGEX = new RegExp(
26
+ '^http://localhost:[0-9]+/symbolicate$'
27
+ );
28
+
29
+ const internalResourceBlocklist: RegExp[] = [
30
+ EXPO_DEV_LOGS_REGEX,
31
+ RN_PACKAGER_SYMBOLICATE_REGEX
32
+ ];
33
+
34
+ /**
35
+ * Filters RN symbolicate calls and Expo logs calls that happen only in dev.
36
+ * @param resource RUMResource
37
+ */
38
+ export const filterDevResource = (
39
+ resource: RUMResource
40
+ ): RUMResource | null => {
41
+ // TODO: if we get the confirmation by Expo that the
42
+ // logs call is only made when __DEV__ is true, add an
43
+ // early return for when it is false.
44
+ for (const resourceRegex of internalResourceBlocklist) {
45
+ if (resourceRegex.test(resource.request.url)) {
46
+ return null;
47
+ }
48
+ }
49
+ return resource;
50
+ };
@@ -0,0 +1,70 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { Platform } from 'react-native';
8
+
9
+ interface Timing {
10
+ /**
11
+ * Time relative (absolute in case of iOS) to some point, in ns.
12
+ */
13
+ startTime: number;
14
+ /**
15
+ * Duration in ns.
16
+ */
17
+ duration: number;
18
+ }
19
+
20
+ interface ResourceTimings {
21
+ // unlike in Performance API it is not the time until request
22
+ // starts (requestStart, before it can be connect, SSL, DNS),
23
+ // but the time until the response is first seen
24
+ firstByte: Timing;
25
+ download: Timing;
26
+ // required by iOS, total timing from the beginning to the end
27
+ fetch: Timing;
28
+ }
29
+
30
+ export function createTimings(
31
+ startTime: number,
32
+ responseStartTime: number,
33
+ responseEndTime: number
34
+ ): ResourceTimings {
35
+ const firstByte = formatTiming(startTime, startTime, responseStartTime);
36
+ const download = formatTiming(
37
+ startTime,
38
+ responseStartTime,
39
+ responseEndTime
40
+ );
41
+ // needed for iOS, simply total duration from start to end
42
+ const fetch = formatTiming(startTime, startTime, responseEndTime);
43
+
44
+ return {
45
+ firstByte,
46
+ download,
47
+ fetch
48
+ };
49
+ }
50
+
51
+ /**
52
+ * @param origin Start time (absolute) of the request
53
+ * @param start Start time (absolute) of the timing
54
+ * @param end End time (absolute) of the timing
55
+ */
56
+ function formatTiming(origin: number, start: number, end: number): Timing {
57
+ return {
58
+ duration: timeToNanos(end - start),
59
+ // if it is Android, startTime should be relative to the origin,
60
+ // if it is iOS - absolute (unix timestamp)
61
+ startTime:
62
+ Platform.OS === 'ios'
63
+ ? timeToNanos(start)
64
+ : timeToNanos(start - origin)
65
+ };
66
+ }
67
+
68
+ function timeToNanos(durationMs: number): number {
69
+ return +(durationMs * 1e6).toFixed(0);
70
+ }
@@ -0,0 +1,30 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { InternalLog } from '../../../../InternalLog';
8
+ import { SdkVerbosity } from '../../../../SdkVerbosity';
9
+ import type { Hostname } from '../domain/firstPartyHosts';
10
+
11
+ // matches what is between the first "://" and the next "/", ":" or whitespace
12
+ const hostRegex = '^.+://([^:/\\s]+)';
13
+
14
+ /**
15
+ * Returns the host from an URL.
16
+ * @returns the host (without the port) or null if could not cast the URL as host
17
+ */
18
+ export const URLHostParser = (url: string): Hostname | null => {
19
+ try {
20
+ const matchedHost = url.match(hostRegex);
21
+ if (matchedHost === null) {
22
+ return null;
23
+ }
24
+ // [0] is the input, [1] is the captured group
25
+ return matchedHost[1] as Hostname;
26
+ } catch (e) {
27
+ InternalLog.log(`Impossible to cast ${url} as URL`, SdkVerbosity.WARN);
28
+ return null;
29
+ }
30
+ };
@@ -0,0 +1,197 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import Timer from '../../../../Timer';
8
+ import type { DdRumResourceTracingAttributes } from '../domain/distributedTracing';
9
+ import { getTracingAttributes } from '../domain/distributedTracing';
10
+ import type { RequestProxyOptions } from '../domain/interfaces/RequestProxy';
11
+ import { RequestProxy } from '../domain/interfaces/RequestProxy';
12
+
13
+ import type { ResourceReporter } from './DatadogRumResource/ResourceReporter';
14
+ import { URLHostParser } from './URLHostParser';
15
+ import { calculateResponseSize } from './responseSize';
16
+
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
+
23
+ const RESPONSE_START_LABEL = 'response_start';
24
+
25
+ interface DdRumXhr extends XMLHttpRequest {
26
+ _datadog_xhr: DdRumXhrContext;
27
+ }
28
+
29
+ interface DdRumXhrContext {
30
+ method: string;
31
+ url: string;
32
+ reported: boolean;
33
+ timer: Timer;
34
+ tracingAttributes: DdRumResourceTracingAttributes;
35
+ }
36
+
37
+ interface XHRProxyProviders {
38
+ xhrType: typeof XMLHttpRequest;
39
+ resourceReporter: ResourceReporter;
40
+ }
41
+
42
+ /**
43
+ * Proxies XMLHTTPRequest to track resources.
44
+ */
45
+ export class XHRProxy extends RequestProxy {
46
+ private providers: XHRProxyProviders;
47
+ private static originalXhrOpen: typeof XMLHttpRequest.prototype.open;
48
+ private static originalXhrSend: typeof XMLHttpRequest.prototype.send;
49
+
50
+ constructor(providers: XHRProxyProviders) {
51
+ super();
52
+ this.providers = providers;
53
+ }
54
+
55
+ onTrackingStart = (context: RequestProxyOptions) => {
56
+ XHRProxy.originalXhrOpen = this.providers.xhrType.prototype.open;
57
+ XHRProxy.originalXhrSend = this.providers.xhrType.prototype.send;
58
+ proxyRequests(this.providers, context);
59
+ };
60
+
61
+ onTrackingStop = () => {
62
+ this.providers.xhrType.prototype.open = XHRProxy.originalXhrOpen;
63
+ this.providers.xhrType.prototype.send = XHRProxy.originalXhrSend;
64
+ };
65
+ }
66
+
67
+ const proxyRequests = (
68
+ providers: XHRProxyProviders,
69
+ context: RequestProxyOptions
70
+ ): void => {
71
+ proxyOpen(providers, context);
72
+ proxySend(providers);
73
+ };
74
+
75
+ const proxyOpen = (
76
+ { xhrType }: XHRProxyProviders,
77
+ context: RequestProxyOptions
78
+ ): void => {
79
+ const originalXhrOpen = xhrType.prototype.open;
80
+ const firstPartyHostsRegex = context.firstPartyHostsRegex;
81
+ const tracingSamplingRate = context.tracingSamplingRate;
82
+
83
+ xhrType.prototype.open = function (
84
+ this: DdRumXhr,
85
+ method: string,
86
+ url: string
87
+ ) {
88
+ const hostname = URLHostParser(url);
89
+ // Keep track of the method and url
90
+ // start time is tracked by the `send` method
91
+ this._datadog_xhr = {
92
+ method,
93
+ url,
94
+ reported: false,
95
+ timer: new Timer(),
96
+ tracingAttributes: getTracingAttributes({
97
+ hostname,
98
+ firstPartyHostsRegex,
99
+ tracingSamplingRate
100
+ })
101
+ };
102
+ // eslint-disable-next-line prefer-rest-params
103
+ return originalXhrOpen.apply(this, arguments as any);
104
+ };
105
+ };
106
+
107
+ const proxySend = (providers: XHRProxyProviders): void => {
108
+ const xhrType = providers.xhrType;
109
+ const originalXhrSend = xhrType.prototype.send;
110
+
111
+ xhrType.prototype.send = function (this: DdRumXhr) {
112
+ if (this._datadog_xhr) {
113
+ // keep track of start time
114
+ this._datadog_xhr.timer.start();
115
+ this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM);
116
+
117
+ const tracingAttributes = this._datadog_xhr.tracingAttributes;
118
+ this.setRequestHeader(
119
+ SAMPLING_PRIORITY_HEADER_KEY,
120
+ tracingAttributes.samplingPriorityHeader
121
+ );
122
+ if (tracingAttributes.tracingStrategy !== 'DISCARD') {
123
+ this.setRequestHeader(
124
+ TRACE_ID_HEADER_KEY,
125
+ tracingAttributes.traceId
126
+ );
127
+ this.setRequestHeader(
128
+ PARENT_ID_HEADER_KEY,
129
+ tracingAttributes.spanId
130
+ );
131
+ }
132
+ }
133
+
134
+ proxyOnReadyStateChange(this, providers);
135
+
136
+ // eslint-disable-next-line prefer-rest-params
137
+ return originalXhrSend.apply(this, arguments as any);
138
+ };
139
+ };
140
+
141
+ const proxyOnReadyStateChange = (
142
+ xhrProxy: DdRumXhr,
143
+ providers: XHRProxyProviders
144
+ ): void => {
145
+ const xhrType = providers.xhrType;
146
+ const originalOnreadystatechange = xhrProxy.onreadystatechange;
147
+
148
+ xhrProxy.onreadystatechange = function () {
149
+ if (xhrProxy.readyState === xhrType.DONE) {
150
+ if (!xhrProxy._datadog_xhr.reported) {
151
+ reportXhr(xhrProxy, providers.resourceReporter);
152
+ xhrProxy._datadog_xhr.reported = true;
153
+ }
154
+ } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
155
+ xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL);
156
+ }
157
+
158
+ if (originalOnreadystatechange) {
159
+ // eslint-disable-next-line prefer-rest-params
160
+ originalOnreadystatechange.apply(xhrProxy, arguments as any);
161
+ }
162
+ };
163
+ };
164
+
165
+ const reportXhr = async (
166
+ xhrProxy: DdRumXhr,
167
+ resourceReporter: ResourceReporter
168
+ ): Promise<void> => {
169
+ const responseSize = calculateResponseSize(xhrProxy);
170
+
171
+ const context = xhrProxy._datadog_xhr;
172
+
173
+ const key = `${context.timer.startTime}/${context.method}`;
174
+
175
+ context.timer.stop();
176
+
177
+ resourceReporter.reportResource({
178
+ key,
179
+ request: {
180
+ method: context.method,
181
+ url: context.url,
182
+ kind: 'xhr'
183
+ },
184
+ tracingAttributes: context.tracingAttributes,
185
+ response: {
186
+ statusCode: xhrProxy.status,
187
+ size: responseSize
188
+ },
189
+ timings: {
190
+ startTime: context.timer.startTime,
191
+ stopTime: context.timer.stopTime,
192
+ responseStartTime: context.timer.hasTickFor(RESPONSE_START_LABEL)
193
+ ? context.timer.timeAt(RESPONSE_START_LABEL)
194
+ : undefined
195
+ }
196
+ });
197
+ };
@@ -4,7 +4,7 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { URLHostParser } from '../../../rum/instrumentation/URLHostParser';
7
+ import { URLHostParser } from '../URLHostParser';
8
8
 
9
9
  describe('URLHostParser', () => {
10
10
  it('returns the host when a classic URL is provided', () => {
@@ -33,8 +33,8 @@ describe('URLHostParser', () => {
33
33
  const egyptianURL = 'https://api.example.مصر./api.json';
34
34
  expect(URLHostParser(egyptianURL)).toBe('api.example.مصر.');
35
35
  });
36
- it('throws if not given an URL', () => {
36
+ it('returns null if not given an URL', () => {
37
37
  const url = 'crash';
38
- expect(() => URLHostParser(url)).toThrow();
38
+ expect(URLHostParser(url)).toBeNull();
39
39
  });
40
40
  });