@datadog/mobile-react-native 1.1.5 → 1.2.0-beta2

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 (160) hide show
  1. package/DatadogSDKReactNative.podspec +2 -2
  2. package/README.md +18 -14
  3. package/android/build.gradle +28 -3
  4. package/android/detekt.yml +572 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +12 -0
  6. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +15 -0
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +143 -8
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +25 -1
  9. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +31 -1
  10. package/android/src/main/kotlin/com/datadog/reactnative/VitalFrameCallback.kt +41 -0
  11. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +7 -2
  12. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +429 -27
  13. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +5 -1
  14. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +40 -1
  15. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +21 -1
  16. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +1 -1
  17. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +1 -0
  18. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +22 -2
  19. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +2 -3
  20. package/android/src/test/kotlin/com/datadog/tools/unit/forge/TelemetryConfigurationEventForgeryFactory.kt +94 -0
  21. package/datadog-sourcemaps.gradle +85 -28
  22. package/ios/Sources/Attributes.swift +0 -4
  23. package/ios/Sources/DdSdk.m +10 -0
  24. package/ios/Sources/DdSdk.swift +113 -10
  25. package/ios/Sources/DdSdkConfiguration.swift +38 -1
  26. package/ios/Sources/JSRefreshRateListener.swift +86 -0
  27. package/ios/Sources/QueueHolder.swift +7 -0
  28. package/ios/Sources/RNDdSdkConfiguration.swift +25 -1
  29. package/lib/commonjs/DatadogProvider/Buffer/BoundedBuffer.js +173 -0
  30. package/lib/commonjs/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -0
  31. package/lib/commonjs/DatadogProvider/Buffer/BufferSingleton.js +32 -0
  32. package/lib/commonjs/DatadogProvider/Buffer/BufferSingleton.js.map +1 -0
  33. package/lib/commonjs/DatadogProvider/Buffer/DatadogBuffer.js +29 -0
  34. package/lib/commonjs/DatadogProvider/Buffer/DatadogBuffer.js.map +1 -0
  35. package/lib/commonjs/DatadogProvider/Buffer/PassThroughBuffer.js +28 -0
  36. package/lib/commonjs/DatadogProvider/Buffer/PassThroughBuffer.js.map +1 -0
  37. package/lib/commonjs/DatadogProvider/Buffer/bufferNativeCall.js +32 -0
  38. package/lib/commonjs/DatadogProvider/Buffer/bufferNativeCall.js.map +1 -0
  39. package/lib/commonjs/DatadogProvider/DatadogProvider.js +101 -0
  40. package/lib/commonjs/DatadogProvider/DatadogProvider.js.map +1 -0
  41. package/lib/commonjs/DdSdkReactNative.js +140 -59
  42. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  43. package/lib/commonjs/DdSdkReactNativeConfiguration.js +166 -9
  44. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  45. package/lib/commonjs/errorUtils.js +69 -0
  46. package/lib/commonjs/errorUtils.js.map +1 -0
  47. package/lib/commonjs/foundation.js +19 -12
  48. package/lib/commonjs/foundation.js.map +1 -1
  49. package/lib/commonjs/index.js +20 -0
  50. package/lib/commonjs/index.js.map +1 -1
  51. package/lib/commonjs/longTasksUtils.js +40 -0
  52. package/lib/commonjs/longTasksUtils.js.map +1 -0
  53. package/lib/commonjs/nativeModulesTypes.js.map +1 -1
  54. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +8 -45
  55. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  56. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +16 -1
  57. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  58. package/lib/commonjs/rum/instrumentation/getJsxRuntime.js +1 -1
  59. package/lib/commonjs/rum/instrumentation/getJsxRuntime.js.map +1 -1
  60. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js +2 -1
  61. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  62. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +2 -1
  63. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  64. package/lib/commonjs/types.js +6 -1
  65. package/lib/commonjs/types.js.map +1 -1
  66. package/lib/commonjs/version.js +1 -1
  67. package/lib/commonjs/version.js.map +1 -1
  68. package/lib/module/DatadogProvider/Buffer/BoundedBuffer.js +163 -0
  69. package/lib/module/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -0
  70. package/lib/module/DatadogProvider/Buffer/BufferSingleton.js +26 -0
  71. package/lib/module/DatadogProvider/Buffer/BufferSingleton.js.map +1 -0
  72. package/lib/module/DatadogProvider/Buffer/DatadogBuffer.js +20 -0
  73. package/lib/module/DatadogProvider/Buffer/DatadogBuffer.js.map +1 -0
  74. package/lib/module/DatadogProvider/Buffer/PassThroughBuffer.js +23 -0
  75. package/lib/module/DatadogProvider/Buffer/PassThroughBuffer.js.map +1 -0
  76. package/lib/module/DatadogProvider/Buffer/bufferNativeCall.js +16 -0
  77. package/lib/module/DatadogProvider/Buffer/bufferNativeCall.js.map +1 -0
  78. package/lib/module/DatadogProvider/DatadogProvider.js +82 -0
  79. package/lib/module/DatadogProvider/DatadogProvider.js.map +1 -0
  80. package/lib/module/DdSdkReactNative.js +132 -57
  81. package/lib/module/DdSdkReactNative.js.map +1 -1
  82. package/lib/module/DdSdkReactNativeConfiguration.js +154 -8
  83. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  84. package/lib/module/errorUtils.js +54 -0
  85. package/lib/module/errorUtils.js.map +1 -0
  86. package/lib/module/foundation.js +18 -12
  87. package/lib/module/foundation.js.map +1 -1
  88. package/lib/module/index.js +3 -2
  89. package/lib/module/index.js.map +1 -1
  90. package/lib/module/longTasksUtils.js +31 -0
  91. package/lib/module/longTasksUtils.js.map +1 -0
  92. package/lib/module/nativeModulesTypes.js.map +1 -1
  93. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +5 -42
  94. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  95. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +14 -1
  96. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  97. package/lib/module/rum/instrumentation/getJsxRuntime.js +1 -1
  98. package/lib/module/rum/instrumentation/getJsxRuntime.js.map +1 -1
  99. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js +2 -1
  100. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  101. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +2 -1
  102. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  103. package/lib/module/types.js +6 -1
  104. package/lib/module/types.js.map +1 -1
  105. package/lib/module/version.js +1 -1
  106. package/lib/module/version.js.map +1 -1
  107. package/lib/typescript/DatadogProvider/Buffer/BoundedBuffer.d.ts +15 -0
  108. package/lib/typescript/DatadogProvider/Buffer/BufferSingleton.d.ts +7 -0
  109. package/lib/typescript/DatadogProvider/Buffer/DatadogBuffer.d.ts +20 -0
  110. package/lib/typescript/DatadogProvider/Buffer/PassThroughBuffer.d.ts +7 -0
  111. package/lib/typescript/DatadogProvider/Buffer/bufferNativeCall.d.ts +3 -0
  112. package/lib/typescript/DatadogProvider/DatadogProvider.d.ts +26 -0
  113. package/lib/typescript/DdSdkReactNative.d.ts +18 -3
  114. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +114 -0
  115. package/lib/typescript/errorUtils.d.ts +12 -0
  116. package/lib/typescript/index.d.ts +3 -2
  117. package/lib/typescript/longTasksUtils.d.ts +6 -0
  118. package/lib/typescript/nativeModulesTypes.d.ts +12 -0
  119. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +0 -2
  120. package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -0
  121. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +1 -0
  122. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +1 -1
  123. package/lib/typescript/types.d.ts +16 -1
  124. package/lib/typescript/version.d.ts +1 -1
  125. package/package.json +3 -2
  126. package/src/DatadogProvider/Buffer/BoundedBuffer.ts +209 -0
  127. package/src/DatadogProvider/Buffer/BufferSingleton.ts +26 -0
  128. package/src/DatadogProvider/Buffer/DatadogBuffer.ts +34 -0
  129. package/src/DatadogProvider/Buffer/PassThroughBuffer.ts +16 -0
  130. package/src/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts +202 -0
  131. package/src/DatadogProvider/Buffer/__tests__/BufferSingleton.test.ts +50 -0
  132. package/src/DatadogProvider/Buffer/bufferNativeCall.ts +24 -0
  133. package/src/DatadogProvider/DatadogProvider.tsx +122 -0
  134. package/src/DatadogProvider/__tests__/__utils__/renderWithProvider.tsx +111 -0
  135. package/src/DatadogProvider/__tests__/initialization.test.tsx +182 -0
  136. package/src/DatadogProvider/__tests__/initializationModes.test.tsx +174 -0
  137. package/src/DdSdkReactNative.tsx +206 -109
  138. package/src/DdSdkReactNativeConfiguration.tsx +225 -8
  139. package/src/__tests__/DdRumWrapper.test.tsx +3 -27
  140. package/src/__tests__/DdSdkReactNative.test.tsx +57 -13
  141. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +181 -0
  142. package/src/__tests__/foundation.test.ts +11 -2
  143. package/src/__tests__/longTasksUtils.ts +23 -0
  144. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +18 -4
  145. package/src/errorUtils.ts +58 -0
  146. package/src/foundation.tsx +15 -12
  147. package/src/index.tsx +9 -1
  148. package/src/longTasksUtils.ts +30 -0
  149. package/src/nativeModulesTypes.ts +14 -0
  150. package/src/rum/instrumentation/DdRumErrorTracking.tsx +9 -46
  151. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +11 -1
  152. package/src/rum/instrumentation/getJsxRuntime.ts +1 -1
  153. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +2 -0
  154. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +3 -1
  155. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +1 -1
  156. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +2 -1
  157. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +2 -0
  158. package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +12 -47
  159. package/src/types.tsx +11 -1
  160. package/src/version.ts +1 -1
@@ -0,0 +1,202 @@
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 { DdSdk } from '../../../foundation';
9
+ import { BoundedBuffer } from '../BoundedBuffer';
10
+
11
+ describe('BoundedBuffer', () => {
12
+ beforeEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+
16
+ describe('void callbacks', () => {
17
+ it('adds void callbacks then drains them', async () => {
18
+ const buffer = new BoundedBuffer();
19
+ const fakeCallback = jest.fn();
20
+ buffer.addCallback(fakeCallback);
21
+ await buffer.drain();
22
+
23
+ expect(fakeCallback).toHaveBeenCalledTimes(1);
24
+
25
+ await buffer.drain();
26
+ expect(fakeCallback).toHaveBeenCalledTimes(1);
27
+ });
28
+ });
29
+
30
+ describe('callbacks with ids', () => {
31
+ it('links the callbacks to pass the returned id from first callback', async () => {
32
+ const buffer = new BoundedBuffer();
33
+
34
+ const callbackReturningId = jest
35
+ .fn()
36
+ .mockReturnValueOnce('callbackId');
37
+ const callbackWithId = jest.fn();
38
+
39
+ const bufferId = await buffer.addCallbackReturningId(
40
+ callbackReturningId
41
+ );
42
+ buffer.addCallbackWithId(callbackWithId, bufferId);
43
+
44
+ await buffer.drain();
45
+ expect(callbackReturningId).toHaveBeenCalledTimes(1);
46
+ expect(callbackWithId).toHaveBeenCalledTimes(1);
47
+ expect(callbackWithId).toHaveBeenCalledWith('callbackId');
48
+ });
49
+
50
+ it('links the correct the returned id when multiple callbacks are registered', async () => {
51
+ const buffer = new BoundedBuffer();
52
+
53
+ const callbackReturningId = jest
54
+ .fn()
55
+ .mockReturnValueOnce('callbackId1')
56
+ .mockReturnValueOnce('callbackId2');
57
+ const callbackWithId = jest.fn();
58
+
59
+ const bufferId1 = await buffer.addCallbackReturningId(
60
+ callbackReturningId
61
+ );
62
+ const bufferId2 = await buffer.addCallbackReturningId(
63
+ callbackReturningId
64
+ );
65
+ buffer.addCallbackWithId(callbackWithId, bufferId1);
66
+ buffer.addCallbackWithId(callbackWithId, bufferId2);
67
+
68
+ await buffer.drain();
69
+ expect(callbackWithId).toHaveBeenCalledTimes(2);
70
+ expect(callbackWithId).toHaveBeenNthCalledWith(1, 'callbackId1');
71
+ expect(callbackWithId).toHaveBeenNthCalledWith(2, 'callbackId2');
72
+ });
73
+
74
+ it('does not run the linked callback when the callback returning id fails', async () => {
75
+ const buffer = new BoundedBuffer();
76
+ const logSpy = jest.spyOn(InternalLog, 'log');
77
+
78
+ const callbackReturningId = jest.fn().mockImplementationOnce(() => {
79
+ throw new Error('issue running callback');
80
+ });
81
+ const callbackWithId = jest.fn();
82
+
83
+ const bufferId = await buffer.addCallbackReturningId(
84
+ callbackReturningId
85
+ );
86
+ buffer.addCallbackWithId(callbackWithId, bufferId);
87
+
88
+ await buffer.drain();
89
+ expect(callbackWithId).not.toHaveBeenCalled();
90
+ expect(logSpy).toHaveBeenCalledWith(
91
+ 'Error running a callback returning an id in Buffer: Error: issue running callback',
92
+ 'warn'
93
+ );
94
+ expect(logSpy).toHaveBeenCalledWith(
95
+ '1 event was not sent as callback id was not set',
96
+ 'warn'
97
+ );
98
+ });
99
+
100
+ it('does not crash when Math.random is mocked', async () => {
101
+ const spy = jest.spyOn(Math, 'random').mockReturnValue(42);
102
+
103
+ const buffer = new BoundedBuffer();
104
+
105
+ const callbackReturningId = jest
106
+ .fn()
107
+ .mockReturnValueOnce('callbackId1')
108
+ .mockReturnValueOnce('callbackId2')
109
+ .mockReturnValueOnce('callbackId3');
110
+ const callbackWithId = jest.fn();
111
+
112
+ const bufferId1 = await buffer.addCallbackReturningId(
113
+ callbackReturningId
114
+ );
115
+ const bufferId2 = await buffer.addCallbackReturningId(
116
+ callbackReturningId
117
+ );
118
+ const bufferId3 = await buffer.addCallbackReturningId(
119
+ callbackReturningId
120
+ );
121
+ spy.mockRestore();
122
+ buffer.addCallbackWithId(callbackWithId, bufferId1);
123
+ buffer.addCallbackWithId(callbackWithId, bufferId2);
124
+ buffer.addCallbackWithId(callbackWithId, bufferId3);
125
+
126
+ await buffer.drain();
127
+ expect(callbackWithId).toHaveBeenCalledTimes(1);
128
+ expect(callbackWithId).toHaveBeenNthCalledWith(1, 'callbackId1');
129
+ expect(DdSdk.telemetryError).toHaveBeenCalledWith(
130
+ 'Could not generate enough random numbers happened 2 times.',
131
+ '',
132
+ 'RandomIdGenerationError'
133
+ );
134
+ });
135
+ });
136
+
137
+ describe('buffer size', () => {
138
+ it('does not add any new void callback when the limit size is reached', async () => {
139
+ const buffer = new BoundedBuffer(3);
140
+ const fakeCallback = jest.fn();
141
+
142
+ buffer.addCallback(fakeCallback);
143
+ buffer.addCallback(fakeCallback);
144
+ buffer.addCallback(fakeCallback);
145
+ buffer.addCallback(fakeCallback);
146
+
147
+ await buffer.drain();
148
+ expect(fakeCallback).toHaveBeenCalledTimes(3);
149
+ expect(DdSdk.telemetryError).toHaveBeenCalledWith(
150
+ 'Buffer overflow happened 1 times.',
151
+ '',
152
+ 'BufferOverflow'
153
+ );
154
+ });
155
+
156
+ it('does not add any new callback with id when the limit size is reached', async () => {
157
+ const buffer = new BoundedBuffer(1);
158
+ const fakeCallback = jest.fn();
159
+ const callbackReturningId = jest
160
+ .fn()
161
+ .mockReturnValueOnce('callbackId');
162
+ const callbackWithId = jest.fn();
163
+
164
+ buffer.addCallback(fakeCallback);
165
+ const bufferId = await buffer.addCallbackReturningId(
166
+ callbackReturningId
167
+ );
168
+ buffer.addCallbackWithId(callbackWithId, bufferId);
169
+
170
+ await buffer.drain();
171
+ expect(fakeCallback).toHaveBeenCalledTimes(1);
172
+ expect(callbackReturningId).not.toHaveBeenCalled();
173
+ expect(callbackWithId).not.toHaveBeenCalled();
174
+ expect(DdSdk.telemetryError).toHaveBeenCalledWith(
175
+ 'Buffer overflow happened 2 times.',
176
+ '',
177
+ 'BufferOverflow'
178
+ );
179
+ });
180
+
181
+ it('adds corresponding callback with id even if the limit size is reached when corresponding callback exists', async () => {
182
+ const buffer = new BoundedBuffer(2);
183
+ const fakeCallback = jest.fn();
184
+ const callbackReturningId = jest
185
+ .fn()
186
+ .mockReturnValueOnce('callbackId');
187
+ const callbackWithId = jest.fn();
188
+
189
+ buffer.addCallback(fakeCallback);
190
+ const bufferId = await buffer.addCallbackReturningId(
191
+ callbackReturningId
192
+ );
193
+ buffer.addCallbackWithId(callbackWithId, bufferId);
194
+
195
+ await buffer.drain();
196
+ expect(fakeCallback).toHaveBeenCalledTimes(1);
197
+ expect(callbackReturningId).toHaveBeenCalledTimes(1);
198
+ expect(callbackWithId).toHaveBeenCalledTimes(1);
199
+ expect(DdSdk.telemetryError).not.toHaveBeenCalled();
200
+ });
201
+ });
202
+ });
@@ -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 { BufferSingleton } from '../BufferSingleton';
8
+
9
+ const flushPromises = () => new Promise<void>(setImmediate);
10
+
11
+ describe('BufferSingleton', () => {
12
+ afterEach(() => {
13
+ BufferSingleton.reset();
14
+ });
15
+ describe('addCallback', () => {
16
+ it('drains callbacks and replaces the buffer by a pass through on initialization', async () => {
17
+ const fakeCallback = jest.fn();
18
+ const callbackReturningId = jest
19
+ .fn()
20
+ .mockReturnValueOnce('callbackId');
21
+ const callbackWithId = jest.fn();
22
+
23
+ // registering callbacks
24
+ BufferSingleton.getInstance().addCallback(fakeCallback);
25
+ const bufferId = await BufferSingleton.getInstance().addCallbackReturningId(
26
+ callbackReturningId
27
+ );
28
+ BufferSingleton.getInstance().addCallbackWithId(
29
+ callbackWithId,
30
+ bufferId
31
+ );
32
+
33
+ expect(fakeCallback).not.toHaveBeenCalled();
34
+
35
+ // initialization
36
+ BufferSingleton.onInitialization();
37
+ await flushPromises();
38
+
39
+ expect(fakeCallback).toHaveBeenCalledTimes(1);
40
+
41
+ // registering a new callback
42
+ BufferSingleton.getInstance().addCallback(fakeCallback);
43
+
44
+ expect(fakeCallback).toHaveBeenCalledTimes(2);
45
+ expect(callbackReturningId).toHaveBeenCalledTimes(1);
46
+ expect(callbackWithId).toHaveBeenCalledTimes(1);
47
+ expect(callbackWithId).toHaveBeenCalledWith('callbackId');
48
+ });
49
+ });
50
+ });
@@ -0,0 +1,24 @@
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 { BufferSingleton } from './BufferSingleton';
8
+
9
+ export const bufferVoidNativeCall = (callback: () => Promise<void>) => {
10
+ return BufferSingleton.getInstance().addCallback(callback);
11
+ };
12
+
13
+ export const bufferNativeCallReturningId = (
14
+ callback: () => Promise<string>
15
+ ) => {
16
+ return BufferSingleton.getInstance().addCallbackReturningId(callback);
17
+ };
18
+
19
+ export const bufferNativeCallWithId = (
20
+ callback: (id: string) => Promise<void>,
21
+ id: string
22
+ ) => {
23
+ return BufferSingleton.getInstance().addCallbackWithId(callback, id);
24
+ };
@@ -0,0 +1,122 @@
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
+ import React from 'react';
7
+ import type { PropsWithChildren } from 'react';
8
+
9
+ import {
10
+ DatadogProviderConfiguration,
11
+ DdSdkReactNativeConfiguration
12
+ } from '../DdSdkReactNativeConfiguration';
13
+ import type {
14
+ PartialInitializationConfiguration,
15
+ AutoInstrumentationConfiguration
16
+ } from '../DdSdkReactNativeConfiguration';
17
+ import { DdSdkReactNative } from '../DdSdkReactNative';
18
+ import { InternalLog } from '../InternalLog';
19
+ import { SdkVerbosity } from '../SdkVerbosity';
20
+
21
+ let isInitialized = false;
22
+
23
+ type Props = PropsWithChildren<{
24
+ /**
25
+ * If a `DatadogProviderConfiguration` instance is passed, the SDK will start tracking errors, resources and actions and sending events.
26
+ *
27
+ * If a `AutoInstrumentationConfiguration` object is passed, the SDK will start tracking errors, resources and actions. To start sending events, call `DatadogProvider.initialize`.
28
+ */
29
+ configuration:
30
+ | DatadogProviderConfiguration
31
+ | AutoInstrumentationConfiguration;
32
+ /**
33
+ * Callback to be run once the SDK starts sending events.
34
+ */
35
+ onInitialization?: () => void;
36
+ }>;
37
+
38
+ type StaticProperties = {
39
+ initialize: (
40
+ configuration: PartialInitializationConfiguration
41
+ ) => Promise<void>;
42
+ onInitialization?: () => void;
43
+ };
44
+
45
+ const isConfigurationPartial = (
46
+ configuration:
47
+ | DatadogProviderConfiguration
48
+ | AutoInstrumentationConfiguration
49
+ ): configuration is AutoInstrumentationConfiguration => {
50
+ if (configuration instanceof DatadogProviderConfiguration) {
51
+ return false;
52
+ }
53
+ if (configuration instanceof DdSdkReactNativeConfiguration) {
54
+ // Not using InternalLog here as it is not yet instanciated
55
+ console.warn(
56
+ 'A DdSdkReactNativeConfiguration was passed to DatadogProvider. Please use DatadogProviderConfiguration instead.'
57
+ );
58
+ return false;
59
+ }
60
+ return true;
61
+ };
62
+
63
+ const initializeDatadog = async (
64
+ configuration: DatadogProviderConfiguration,
65
+ onInitialization?: () => void
66
+ ) => {
67
+ await DdSdkReactNative._initializeFromDatadogProvider(configuration);
68
+ if (onInitialization) {
69
+ try {
70
+ onInitialization();
71
+ } catch (error) {
72
+ InternalLog.log(
73
+ `Error running onInitialization callback ${error}`,
74
+ SdkVerbosity.WARN
75
+ );
76
+ }
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Set up the Datadog React Native SDK.
82
+ */
83
+ export const DatadogProvider: React.FC<Props> & StaticProperties = ({
84
+ children,
85
+ configuration,
86
+ onInitialization
87
+ }) => {
88
+ if (!isInitialized) {
89
+ // Here we cannot use a useEffect hook since it would be called after
90
+ // the first render. Thus, we wouldn't enable auto-instrumentation on
91
+ // the elements rendered in this first render and what happens during
92
+ // the first render.
93
+ if (isConfigurationPartial(configuration)) {
94
+ DdSdkReactNative._enableFeaturesFromDatadogProvider(configuration);
95
+ DatadogProvider.onInitialization = onInitialization;
96
+ } else {
97
+ initializeDatadog(configuration, onInitialization);
98
+ }
99
+ isInitialized = true;
100
+ }
101
+
102
+ return <>{children}</>;
103
+ };
104
+
105
+ /**
106
+ * Initialize the Datadog SDK to start sending RUM events, logs and traces,
107
+ * then execute onInitialization callback if any was provided.
108
+ */
109
+ DatadogProvider.initialize = async (
110
+ configuration: PartialInitializationConfiguration
111
+ ) => {
112
+ await DdSdkReactNative._initializeFromDatadogProviderWithConfigurationAsync(
113
+ configuration
114
+ );
115
+ if (DatadogProvider.onInitialization) {
116
+ DatadogProvider.onInitialization();
117
+ }
118
+ };
119
+
120
+ export const __internalResetIsInitializedForTesting = () => {
121
+ isInitialized = false;
122
+ };
@@ -0,0 +1,111 @@
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 { render } from '@testing-library/react-native';
8
+ import { Animated, Button, InteractionManager, Text, View } from 'react-native';
9
+ import React, { useState } from 'react';
10
+
11
+ import type { AutoInstrumentationConfiguration } from '../../../DdSdkReactNativeConfiguration';
12
+ import { DatadogProviderConfiguration } from '../../../DdSdkReactNativeConfiguration';
13
+ import { DatadogProvider } from '../../DatadogProvider';
14
+
15
+ const DefaultTestApp = () => {
16
+ return (
17
+ <View>
18
+ <Text>I am a test application</Text>
19
+ <Button title="test button" onPress={() => {}} />
20
+ </View>
21
+ );
22
+ };
23
+
24
+ const AppWithAnimation = () => {
25
+ const [opacity] = useState(new Animated.Value(0));
26
+ Animated.timing(opacity, {
27
+ toValue: 1,
28
+ duration: 500,
29
+ useNativeDriver: false
30
+ }).start();
31
+
32
+ return (
33
+ <View>
34
+ <Button onPress={() => {}} title="test button" />
35
+ <Animated.View style={{ opacity }} />
36
+ </View>
37
+ );
38
+ };
39
+
40
+ export const getDefaultConfiguration = () =>
41
+ new DatadogProviderConfiguration(
42
+ 'fakeToken',
43
+ 'fakeEnv',
44
+ 'fakeApplicationId',
45
+ true,
46
+ false, // TODO: the initialization is broken with trackResources in test, fix it
47
+ true
48
+ );
49
+
50
+ export const renderWithProviderAndAnimation = (params?: {
51
+ configuration?: DatadogProviderConfiguration;
52
+ }) => {
53
+ return renderWithProvider({
54
+ AppComponent: <AppWithAnimation />,
55
+ configuration: params?.configuration
56
+ });
57
+ };
58
+
59
+ export const renderWithProvider = (params?: {
60
+ AppComponent?: React.ReactNode;
61
+ configuration?:
62
+ | DatadogProviderConfiguration
63
+ | AutoInstrumentationConfiguration;
64
+ onInitialization?: () => void;
65
+ }) => {
66
+ const AppComponent = params?.AppComponent || <DefaultTestApp />;
67
+ const configuration = params?.configuration || getDefaultConfiguration();
68
+
69
+ const result = render(
70
+ <DatadogProvider
71
+ configuration={configuration}
72
+ onInitialization={params?.onInitialization}
73
+ >
74
+ {AppComponent}
75
+ </DatadogProvider>
76
+ );
77
+
78
+ const rerenderWithRandomConfig = () => {
79
+ const randomConfiguration = new DatadogProviderConfiguration(
80
+ Math.random().toString(),
81
+ 'fakeEnv',
82
+ 'fakeApplicationId',
83
+ true,
84
+ false,
85
+ true
86
+ );
87
+ result.rerender(
88
+ <DatadogProvider configuration={randomConfiguration}>
89
+ {AppComponent}
90
+ </DatadogProvider>
91
+ );
92
+ };
93
+
94
+ return {
95
+ ...result,
96
+ rerenderWithRandomConfig
97
+ };
98
+ };
99
+
100
+ /**
101
+ * Mocks an animation for InteractionManager.runAfterInteractions. Returns
102
+ * a function to be called to finish the animation
103
+ */
104
+ export const mockAnimation = () => {
105
+ const fakeAnimationHandle = InteractionManager.createInteractionHandle();
106
+
107
+ return {
108
+ finishAnimation: () =>
109
+ InteractionManager.clearInteractionHandle(fakeAnimationHandle)
110
+ };
111
+ };
@@ -0,0 +1,182 @@
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 { NativeModules } from 'react-native';
8
+
9
+ import { InitializationMode } from '../../DdSdkReactNativeConfiguration';
10
+ import { DdSdkReactNative } from '../../DdSdkReactNative';
11
+ import { TimeProvider } from '../../TimeProvider';
12
+ import { DdRum, DdTrace } from '../../foundation';
13
+ import { RumActionType } from '../../types';
14
+ import { BufferSingleton } from '../Buffer/BufferSingleton';
15
+ import {
16
+ DatadogProvider,
17
+ __internalResetIsInitializedForTesting
18
+ } from '../DatadogProvider';
19
+
20
+ import {
21
+ getDefaultConfiguration,
22
+ mockAnimation,
23
+ renderWithProvider
24
+ } from './__utils__/renderWithProvider';
25
+
26
+ jest.mock('../../TimeProvider', () => {
27
+ const now = jest.fn();
28
+ return {
29
+ TimeProvider: jest.fn().mockImplementation(() => {
30
+ return { now };
31
+ })
32
+ };
33
+ });
34
+ const nowMock = new TimeProvider().now;
35
+
36
+ const flushPromises = () => new Promise<void>(setImmediate);
37
+
38
+ describe('DatadogProvider', () => {
39
+ afterEach(() => {
40
+ jest.clearAllMocks();
41
+ DdSdkReactNative['wasInitialized'] = false;
42
+ __internalResetIsInitializedForTesting();
43
+ BufferSingleton.reset();
44
+ (nowMock as any).mockReturnValue('timestamp_not_specified');
45
+ });
46
+ describe('initialization', () => {
47
+ it('renders its children and initializes the SDK once', async () => {
48
+ const {
49
+ getByText,
50
+ rerenderWithRandomConfig
51
+ } = renderWithProvider();
52
+ getByText('I am a test application');
53
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
54
+
55
+ // We remove the sdk version from the configuration as it would require to update this snapshot
56
+ const receivedConfiguration =
57
+ NativeModules.DdSdk.initialize.mock.calls[0][0];
58
+ delete receivedConfiguration.additionalConfig['_dd.sdk_version'];
59
+ expect(receivedConfiguration).toMatchInlineSnapshot(`
60
+ DdSdkConfiguration {
61
+ "additionalConfig": Object {
62
+ "_dd.first_party_hosts": Array [],
63
+ "_dd.native_view_tracking": false,
64
+ "_dd.source": "react-native",
65
+ },
66
+ "applicationId": "fakeApplicationId",
67
+ "clientToken": "fakeToken",
68
+ "configurationForTelemetry": Object {
69
+ "initializationType": "SYNC",
70
+ "trackErrors": true,
71
+ "trackInteractions": true,
72
+ "trackNetworkRequests": false,
73
+ },
74
+ "env": "fakeEnv",
75
+ "longTaskThresholdMs": 0,
76
+ "nativeCrashReportEnabled": false,
77
+ "nativeLongTaskThresholdMs": 200,
78
+ "sampleRate": 100,
79
+ "site": "US1",
80
+ "telemetrySampleRate": 20,
81
+ "trackingConsent": "granted",
82
+ "vitalsUpdateFrequency": "AVERAGE",
83
+ }
84
+ `);
85
+
86
+ // Re-render
87
+ rerenderWithRandomConfig();
88
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
89
+ });
90
+
91
+ it('keeps events in the buffer then executes the buffer once initialization is done', async () => {
92
+ // Given
93
+ NativeModules.DdTrace.startSpan.mockReturnValueOnce('good_span_id');
94
+ (nowMock as any).mockReturnValue('good_timestamp');
95
+ await DdRum.addAction(RumActionType.TAP, 'fakeAction');
96
+
97
+ // When
98
+ const spanId = await DdTrace.startSpan('fakeOperation');
99
+ await DdTrace.finishSpan(spanId);
100
+ (nowMock as any).mockReturnValue('bad_timestamp');
101
+
102
+ // Then
103
+ expect(NativeModules.DdRum.addAction).not.toHaveBeenCalled();
104
+ expect(NativeModules.DdTrace.startSpan).not.toHaveBeenCalled();
105
+ expect(NativeModules.DdTrace.finishSpan).not.toHaveBeenCalled();
106
+
107
+ // When initialization
108
+ renderWithProvider();
109
+ await flushPromises();
110
+
111
+ // Then
112
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
113
+ expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
114
+ expect(NativeModules.DdTrace.startSpan).toHaveBeenCalledTimes(1);
115
+ expect(NativeModules.DdTrace.startSpan).toHaveBeenLastCalledWith(
116
+ 'fakeOperation',
117
+ {},
118
+ 'good_timestamp'
119
+ );
120
+ expect(NativeModules.DdTrace.finishSpan).toHaveBeenCalledTimes(1);
121
+ expect(NativeModules.DdTrace.finishSpan).toHaveBeenLastCalledWith(
122
+ 'good_span_id',
123
+ {},
124
+ 'good_timestamp'
125
+ );
126
+ });
127
+ });
128
+
129
+ describe('onInitialization callback', () => {
130
+ it('runs after initialization when SYNC initialization', async () => {
131
+ const onInitialization = jest.fn();
132
+ const { getByText } = renderWithProvider({ onInitialization });
133
+ getByText('I am a test application');
134
+ expect(onInitialization).not.toHaveBeenCalled();
135
+
136
+ await flushPromises();
137
+ expect(onInitialization).toHaveBeenCalledTimes(1);
138
+ });
139
+
140
+ it('runs after initialization when ASYNC initialization', async () => {
141
+ const onInitialization = jest.fn();
142
+ const { finishAnimation } = mockAnimation();
143
+ const configuration = getDefaultConfiguration();
144
+ configuration.initializationMode = InitializationMode.ASYNC;
145
+ const { getByText } = renderWithProvider({
146
+ onInitialization,
147
+ configuration
148
+ });
149
+ getByText('I am a test application');
150
+ await flushPromises();
151
+ expect(onInitialization).not.toHaveBeenCalled();
152
+
153
+ finishAnimation();
154
+ await flushPromises();
155
+ expect(onInitialization).toHaveBeenCalledTimes(1);
156
+ });
157
+ it('runs after initialization when partial initialization', async () => {
158
+ const onInitialization = jest.fn();
159
+ const { getByText } = renderWithProvider({
160
+ onInitialization,
161
+ configuration: {
162
+ trackErrors: true,
163
+ trackResources: true,
164
+ trackInteractions: true,
165
+ firstPartyHosts: ['api.com'],
166
+ resourceTracingSamplingRate: 100
167
+ }
168
+ });
169
+ getByText('I am a test application');
170
+ await flushPromises();
171
+ expect(onInitialization).not.toHaveBeenCalled();
172
+
173
+ await DatadogProvider.initialize({
174
+ applicationId: 'fake-application-id',
175
+ clientToken: 'fake-client-token',
176
+ env: 'fake-env'
177
+ });
178
+ await flushPromises();
179
+ expect(onInitialization).toHaveBeenCalledTimes(1);
180
+ });
181
+ });
182
+ });