@datadog/mobile-react-native 1.1.4 → 1.2.0-beta1
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.
- package/README.md +18 -14
- package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +8 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +10 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +25 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +2 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +52 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +7 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +1 -0
- package/ios/Sources/DdSdk.m +10 -0
- package/ios/Sources/DdSdk.swift +17 -0
- package/ios/Sources/DdSdkConfiguration.swift +4 -0
- package/ios/Sources/RNDdSdkConfiguration.swift +2 -0
- package/lib/commonjs/DatadogProvider/Buffer/BoundedBuffer.js +173 -0
- package/lib/commonjs/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -0
- package/lib/commonjs/DatadogProvider/Buffer/BufferSingleton.js +32 -0
- package/lib/commonjs/DatadogProvider/Buffer/BufferSingleton.js.map +1 -0
- package/lib/commonjs/DatadogProvider/Buffer/DatadogBuffer.js +29 -0
- package/lib/commonjs/DatadogProvider/Buffer/DatadogBuffer.js.map +1 -0
- package/lib/commonjs/DatadogProvider/Buffer/PassThroughBuffer.js +28 -0
- package/lib/commonjs/DatadogProvider/Buffer/PassThroughBuffer.js.map +1 -0
- package/lib/commonjs/DatadogProvider/Buffer/bufferNativeCall.js +32 -0
- package/lib/commonjs/DatadogProvider/Buffer/bufferNativeCall.js.map +1 -0
- package/lib/commonjs/DatadogProvider/DatadogProvider.js +101 -0
- package/lib/commonjs/DatadogProvider/DatadogProvider.js.map +1 -0
- package/lib/commonjs/DdSdkReactNative.js +138 -56
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js +102 -9
- package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/commonjs/errorUtils.js +60 -0
- package/lib/commonjs/errorUtils.js.map +1 -0
- package/lib/commonjs/foundation.js +19 -12
- package/lib/commonjs/foundation.js.map +1 -1
- package/lib/commonjs/index.js +20 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/nativeModulesTypes.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +8 -45
- package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +16 -1
- package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/getJsxRuntime.js +1 -1
- package/lib/commonjs/rum/instrumentation/getJsxRuntime.js.map +1 -1
- package/lib/commonjs/types.js +2 -1
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DatadogProvider/Buffer/BoundedBuffer.js +163 -0
- package/lib/module/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -0
- package/lib/module/DatadogProvider/Buffer/BufferSingleton.js +26 -0
- package/lib/module/DatadogProvider/Buffer/BufferSingleton.js.map +1 -0
- package/lib/module/DatadogProvider/Buffer/DatadogBuffer.js +20 -0
- package/lib/module/DatadogProvider/Buffer/DatadogBuffer.js.map +1 -0
- package/lib/module/DatadogProvider/Buffer/PassThroughBuffer.js +23 -0
- package/lib/module/DatadogProvider/Buffer/PassThroughBuffer.js.map +1 -0
- package/lib/module/DatadogProvider/Buffer/bufferNativeCall.js +16 -0
- package/lib/module/DatadogProvider/Buffer/bufferNativeCall.js.map +1 -0
- package/lib/module/DatadogProvider/DatadogProvider.js +82 -0
- package/lib/module/DatadogProvider/DatadogProvider.js.map +1 -0
- package/lib/module/DdSdkReactNative.js +130 -54
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/DdSdkReactNativeConfiguration.js +91 -8
- package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/module/errorUtils.js +45 -0
- package/lib/module/errorUtils.js.map +1 -0
- package/lib/module/foundation.js +18 -12
- package/lib/module/foundation.js.map +1 -1
- package/lib/module/index.js +3 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/nativeModulesTypes.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumErrorTracking.js +5 -42
- package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +14 -1
- package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/getJsxRuntime.js +1 -1
- package/lib/module/rum/instrumentation/getJsxRuntime.js.map +1 -1
- package/lib/module/types.js +2 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DatadogProvider/Buffer/BoundedBuffer.d.ts +15 -0
- package/lib/typescript/DatadogProvider/Buffer/BufferSingleton.d.ts +7 -0
- package/lib/typescript/DatadogProvider/Buffer/DatadogBuffer.d.ts +20 -0
- package/lib/typescript/DatadogProvider/Buffer/PassThroughBuffer.d.ts +7 -0
- package/lib/typescript/DatadogProvider/Buffer/bufferNativeCall.d.ts +3 -0
- package/lib/typescript/DatadogProvider/DatadogProvider.d.ts +26 -0
- package/lib/typescript/DdSdkReactNative.d.ts +23 -1
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +63 -0
- package/lib/typescript/errorUtils.d.ts +4 -0
- package/lib/typescript/index.d.ts +3 -2
- package/lib/typescript/nativeModulesTypes.d.ts +12 -0
- package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +0 -2
- package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -0
- package/lib/typescript/types.d.ts +2 -1
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/DatadogProvider/Buffer/BoundedBuffer.ts +209 -0
- package/src/DatadogProvider/Buffer/BufferSingleton.ts +26 -0
- package/src/DatadogProvider/Buffer/DatadogBuffer.ts +34 -0
- package/src/DatadogProvider/Buffer/PassThroughBuffer.ts +16 -0
- package/src/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts +202 -0
- package/src/DatadogProvider/Buffer/__tests__/BufferSingleton.test.ts +50 -0
- package/src/DatadogProvider/Buffer/bufferNativeCall.ts +24 -0
- package/src/DatadogProvider/DatadogProvider.tsx +122 -0
- package/src/DatadogProvider/__tests__/__utils__/renderWithProvider.tsx +111 -0
- package/src/DatadogProvider/__tests__/initialization.test.tsx +174 -0
- package/src/DatadogProvider/__tests__/initializationModes.test.tsx +174 -0
- package/src/DdSdkReactNative.tsx +205 -105
- package/src/DdSdkReactNativeConfiguration.tsx +160 -8
- package/src/__tests__/DdRumWrapper.test.tsx +3 -27
- package/src/__tests__/DdSdkReactNative.test.tsx +1 -0
- package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +168 -0
- package/src/__tests__/foundation.test.ts +11 -2
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +18 -4
- package/src/errorUtils.ts +50 -0
- package/src/foundation.tsx +15 -12
- package/src/index.tsx +9 -1
- package/src/nativeModulesTypes.ts +14 -0
- package/src/rum/instrumentation/DdRumErrorTracking.tsx +9 -46
- package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +11 -1
- package/src/rum/instrumentation/getJsxRuntime.ts +1 -1
- package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +2 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +2 -0
- package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +3 -0
- package/src/types.tsx +2 -1
- 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,174 @@
|
|
|
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.long_task.threshold": 200,
|
|
64
|
+
"_dd.native_view_tracking": false,
|
|
65
|
+
"_dd.source": "react-native",
|
|
66
|
+
},
|
|
67
|
+
"applicationId": "fakeApplicationId",
|
|
68
|
+
"clientToken": "fakeToken",
|
|
69
|
+
"env": "fakeEnv",
|
|
70
|
+
"nativeCrashReportEnabled": false,
|
|
71
|
+
"sampleRate": 100,
|
|
72
|
+
"site": "US1",
|
|
73
|
+
"telemetrySampleRate": 20,
|
|
74
|
+
"trackingConsent": "granted",
|
|
75
|
+
}
|
|
76
|
+
`);
|
|
77
|
+
|
|
78
|
+
// Re-render
|
|
79
|
+
rerenderWithRandomConfig();
|
|
80
|
+
expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('keeps events in the buffer then executes the buffer once initialization is done', async () => {
|
|
84
|
+
// Given
|
|
85
|
+
NativeModules.DdTrace.startSpan.mockReturnValueOnce('good_span_id');
|
|
86
|
+
(nowMock as any).mockReturnValue('good_timestamp');
|
|
87
|
+
await DdRum.addAction(RumActionType.TAP, 'fakeAction');
|
|
88
|
+
|
|
89
|
+
// When
|
|
90
|
+
const spanId = await DdTrace.startSpan('fakeOperation');
|
|
91
|
+
await DdTrace.finishSpan(spanId);
|
|
92
|
+
(nowMock as any).mockReturnValue('bad_timestamp');
|
|
93
|
+
|
|
94
|
+
// Then
|
|
95
|
+
expect(NativeModules.DdRum.addAction).not.toHaveBeenCalled();
|
|
96
|
+
expect(NativeModules.DdTrace.startSpan).not.toHaveBeenCalled();
|
|
97
|
+
expect(NativeModules.DdTrace.finishSpan).not.toHaveBeenCalled();
|
|
98
|
+
|
|
99
|
+
// When initialization
|
|
100
|
+
renderWithProvider();
|
|
101
|
+
await flushPromises();
|
|
102
|
+
|
|
103
|
+
// Then
|
|
104
|
+
expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
|
|
105
|
+
expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
|
|
106
|
+
expect(NativeModules.DdTrace.startSpan).toHaveBeenCalledTimes(1);
|
|
107
|
+
expect(NativeModules.DdTrace.startSpan).toHaveBeenLastCalledWith(
|
|
108
|
+
'fakeOperation',
|
|
109
|
+
{},
|
|
110
|
+
'good_timestamp'
|
|
111
|
+
);
|
|
112
|
+
expect(NativeModules.DdTrace.finishSpan).toHaveBeenCalledTimes(1);
|
|
113
|
+
expect(NativeModules.DdTrace.finishSpan).toHaveBeenLastCalledWith(
|
|
114
|
+
'good_span_id',
|
|
115
|
+
{},
|
|
116
|
+
'good_timestamp'
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('onInitialization callback', () => {
|
|
122
|
+
it('runs after initialization when SYNC initialization', async () => {
|
|
123
|
+
const onInitialization = jest.fn();
|
|
124
|
+
const { getByText } = renderWithProvider({ onInitialization });
|
|
125
|
+
getByText('I am a test application');
|
|
126
|
+
expect(onInitialization).not.toHaveBeenCalled();
|
|
127
|
+
|
|
128
|
+
await flushPromises();
|
|
129
|
+
expect(onInitialization).toHaveBeenCalledTimes(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('runs after initialization when ASYNC initialization', async () => {
|
|
133
|
+
const onInitialization = jest.fn();
|
|
134
|
+
const { finishAnimation } = mockAnimation();
|
|
135
|
+
const configuration = getDefaultConfiguration();
|
|
136
|
+
configuration.initializationMode = InitializationMode.ASYNC;
|
|
137
|
+
const { getByText } = renderWithProvider({
|
|
138
|
+
onInitialization,
|
|
139
|
+
configuration
|
|
140
|
+
});
|
|
141
|
+
getByText('I am a test application');
|
|
142
|
+
await flushPromises();
|
|
143
|
+
expect(onInitialization).not.toHaveBeenCalled();
|
|
144
|
+
|
|
145
|
+
finishAnimation();
|
|
146
|
+
await flushPromises();
|
|
147
|
+
expect(onInitialization).toHaveBeenCalledTimes(1);
|
|
148
|
+
});
|
|
149
|
+
it('runs after initialization when partial initialization', async () => {
|
|
150
|
+
const onInitialization = jest.fn();
|
|
151
|
+
const { getByText } = renderWithProvider({
|
|
152
|
+
onInitialization,
|
|
153
|
+
configuration: {
|
|
154
|
+
trackErrors: true,
|
|
155
|
+
trackResources: true,
|
|
156
|
+
trackInteractions: true,
|
|
157
|
+
firstPartyHosts: ['api.com'],
|
|
158
|
+
resourceTracingSamplingRate: 100
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
getByText('I am a test application');
|
|
162
|
+
await flushPromises();
|
|
163
|
+
expect(onInitialization).not.toHaveBeenCalled();
|
|
164
|
+
|
|
165
|
+
await DatadogProvider.initialize({
|
|
166
|
+
applicationId: 'fake-application-id',
|
|
167
|
+
clientToken: 'fake-client-token',
|
|
168
|
+
env: 'fake-env'
|
|
169
|
+
});
|
|
170
|
+
await flushPromises();
|
|
171
|
+
expect(onInitialization).toHaveBeenCalledTimes(1);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|