@datadog/mobile-react-native 1.0.0-rc8 → 1.0.0-rc9
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/DatadogSDKReactNative.podspec +1 -1
- package/lib/commonjs/DdSdkReactNative.js +2 -2
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +46 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
- package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/{URLHostParser.js → resourceTracking/implementation/URLHostParser.js} +16 -7
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +1 -1
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
- package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +29 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
- package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
- package/{src/rum/instrumentation/URLHostParser.ts → lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js} +15 -8
- package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +4 -3
- package/src/DdSdkReactNative.tsx +1 -1
- package/src/__tests__/DdSdkReactNative.test.tsx +11 -8
- package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +1 -1
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
- package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
- package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
- package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
- package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
- package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
- package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +45 -0
- package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
- package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +26 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
- package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
- package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
- package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/implementation/__tests__}/URLHostParser.test.ts +3 -3
- package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +83 -49
- package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
- package/src/version.ts +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -356
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
- package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
- package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
- package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +0 -1
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -331
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
- package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
- package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
- package/lib/module/rum/instrumentation/URLHostParser.js +0 -24
- package/lib/module/rum/instrumentation/URLHostParser.js.map +0 -1
- package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -40
- package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
- package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
- package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +0 -6
- package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -426
- package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
- package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datadog/mobile-react-native",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc9",
|
|
4
4
|
"description": "A client-side React Native module to interact with Datadog",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datadog",
|
|
@@ -53,7 +53,8 @@
|
|
|
53
53
|
"<rootDir>/lib/"
|
|
54
54
|
],
|
|
55
55
|
"setupFiles": [
|
|
56
|
-
"./../../node_modules/react-native-gesture-handler/jestSetup.js"
|
|
56
|
+
"./../../node_modules/react-native-gesture-handler/jestSetup.js",
|
|
57
|
+
"./../../jest.setup.js"
|
|
57
58
|
],
|
|
58
59
|
"transformIgnorePatterns": []
|
|
59
60
|
},
|
|
@@ -71,5 +72,5 @@
|
|
|
71
72
|
]
|
|
72
73
|
]
|
|
73
74
|
},
|
|
74
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "552457ced3fc826cb42120402eb9e98f80228890"
|
|
75
76
|
}
|
package/src/DdSdkReactNative.tsx
CHANGED
|
@@ -11,8 +11,8 @@ import { SdkVerbosity } from './SdkVerbosity';
|
|
|
11
11
|
import type { TrackingConsent } from './TrackingConsent';
|
|
12
12
|
import { DdSdk } from './foundation';
|
|
13
13
|
import { DdRumErrorTracking } from './rum/instrumentation/DdRumErrorTracking';
|
|
14
|
-
import { DdRumResourceTracking } from './rum/instrumentation/DdRumResourceTracking';
|
|
15
14
|
import { DdRumUserInteractionTracking } from './rum/instrumentation/DdRumUserInteractionTracking';
|
|
15
|
+
import { DdRumResourceTracking } from './rum/instrumentation/resourceTracking/DdRumResourceTracking';
|
|
16
16
|
import { DdSdkConfiguration } from './types';
|
|
17
17
|
import { version as sdkVersion } from './version';
|
|
18
18
|
|
|
@@ -13,8 +13,8 @@ import { SdkVerbosity } from '../SdkVerbosity';
|
|
|
13
13
|
import { TrackingConsent } from '../TrackingConsent';
|
|
14
14
|
import { DdSdk } from '../foundation';
|
|
15
15
|
import { DdRumErrorTracking } from '../rum/instrumentation/DdRumErrorTracking';
|
|
16
|
-
import { DdRumResourceTracking } from '../rum/instrumentation/DdRumResourceTracking';
|
|
17
16
|
import { DdRumUserInteractionTracking } from '../rum/instrumentation/DdRumUserInteractionTracking';
|
|
17
|
+
import { DdRumResourceTracking } from '../rum/instrumentation/resourceTracking/DdRumResourceTracking';
|
|
18
18
|
import type { DdSdkConfiguration } from '../types';
|
|
19
19
|
import { version as sdkVersion } from '../version';
|
|
20
20
|
|
|
@@ -28,13 +28,16 @@ jest.mock('../rum/instrumentation/DdRumUserInteractionTracking', () => {
|
|
|
28
28
|
};
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
jest.mock(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
31
|
+
jest.mock(
|
|
32
|
+
'../rum/instrumentation/resourceTracking/DdRumResourceTracking',
|
|
33
|
+
() => {
|
|
34
|
+
return {
|
|
35
|
+
DdRumResourceTracking: {
|
|
36
|
+
startTracking: jest.fn().mockImplementation(() => {})
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
);
|
|
38
41
|
|
|
39
42
|
jest.mock('../rum/instrumentation/DdRumErrorTracking', () => {
|
|
40
43
|
return {
|
|
@@ -31,7 +31,7 @@ jest.mock('../../../InternalLog', () => {
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
|
|
34
|
-
jest.mock('react-native/Libraries/Animated/
|
|
34
|
+
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
|
|
35
35
|
jest.useFakeTimers();
|
|
36
36
|
|
|
37
37
|
let testedEventsInterceptor: DdEventsInterceptor;
|
|
@@ -31,7 +31,7 @@ const styles = StyleSheet.create({
|
|
|
31
31
|
jest.mock('../../../rum/instrumentation/DdEventsInterceptor');
|
|
32
32
|
|
|
33
33
|
// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
|
|
34
|
-
jest.mock('react-native/Libraries/Animated/
|
|
34
|
+
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
|
|
35
35
|
jest.useFakeTimers();
|
|
36
36
|
|
|
37
37
|
let originalMethod: typeof React.createElement;
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
|
|
10
|
+
import { firstPartyHostsRegexBuilder } from './domain/firstPartyHosts';
|
|
11
|
+
import type { RequestProxy } from './domain/interfaces/RequestProxy';
|
|
12
|
+
import { ResourceReporter } from './implementation/DatadogRumResource/ResourceReporter';
|
|
13
|
+
import { filterDevResource } from './implementation/DatadogRumResource/internalDevResourceBlocklist';
|
|
14
|
+
import { XHRProxy } from './implementation/XHRProxy';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
|
|
18
|
+
*/
|
|
19
|
+
export class DdRumResourceTracking {
|
|
20
|
+
private static isTracking = false;
|
|
21
|
+
private static requestProxy: RequestProxy | null;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Starts tracking resources and sends a RUM Resource event every time a network request is detected.
|
|
25
|
+
*/
|
|
26
|
+
static startTracking({
|
|
27
|
+
tracingSamplingRate,
|
|
28
|
+
firstPartyHosts
|
|
29
|
+
}: {
|
|
30
|
+
tracingSamplingRate: number;
|
|
31
|
+
firstPartyHosts: string[];
|
|
32
|
+
}): void {
|
|
33
|
+
// extra safety to avoid proxying the XHR class twice
|
|
34
|
+
if (DdRumResourceTracking.isTracking) {
|
|
35
|
+
InternalLog.log(
|
|
36
|
+
'Datadog SDK is already tracking XHR resources',
|
|
37
|
+
SdkVerbosity.WARN
|
|
38
|
+
);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this.requestProxy = new XHRProxy({
|
|
43
|
+
xhrType: XMLHttpRequest,
|
|
44
|
+
resourceReporter: new ResourceReporter([filterDevResource])
|
|
45
|
+
});
|
|
46
|
+
this.requestProxy.onTrackingStart({
|
|
47
|
+
tracingSamplingRate,
|
|
48
|
+
firstPartyHostsRegex: firstPartyHostsRegexBuilder(firstPartyHosts)
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
InternalLog.log(
|
|
52
|
+
'Datadog SDK is tracking XHR resources',
|
|
53
|
+
SdkVerbosity.INFO
|
|
54
|
+
);
|
|
55
|
+
DdRumResourceTracking.isTracking = true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static stopTracking(): void {
|
|
59
|
+
if (DdRumResourceTracking.isTracking) {
|
|
60
|
+
DdRumResourceTracking.isTracking = false;
|
|
61
|
+
if (this.requestProxy) {
|
|
62
|
+
this.requestProxy.onTrackingStop();
|
|
63
|
+
}
|
|
64
|
+
this.requestProxy = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
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 { DdRumResourceTracking } from '../DdRumResourceTracking';
|
|
10
|
+
|
|
11
|
+
import { XMLHttpRequestMock } from './__utils__/XMLHttpRequestMock';
|
|
12
|
+
|
|
13
|
+
const DdRum = NativeModules.DdRum;
|
|
14
|
+
|
|
15
|
+
const flushPromises = () => new Promise(setImmediate);
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
DdRum.startResource.mockClear();
|
|
19
|
+
DdRum.stopResource.mockClear();
|
|
20
|
+
global.XMLHttpRequest = XMLHttpRequestMock;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
global.XMLHttpRequest = undefined;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const executeRequest = (url: string = 'https://api.example.com/v2/user') => {
|
|
28
|
+
const xhr = new XMLHttpRequestMock();
|
|
29
|
+
xhr.open('GET', url);
|
|
30
|
+
xhr.send();
|
|
31
|
+
xhr.notifyResponseArrived();
|
|
32
|
+
xhr.complete(200, 'ok');
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('DdRumResourceTracking', () => {
|
|
36
|
+
it('removes all side effects when tracking is stopped', async () => {
|
|
37
|
+
// GIVEN
|
|
38
|
+
global.XMLHttpRequest = XMLHttpRequestMock;
|
|
39
|
+
DdRumResourceTracking.startTracking({
|
|
40
|
+
tracingSamplingRate: 100,
|
|
41
|
+
firstPartyHosts: ['example.com']
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// WHEN
|
|
45
|
+
executeRequest();
|
|
46
|
+
await flushPromises();
|
|
47
|
+
|
|
48
|
+
// THEN
|
|
49
|
+
expect(DdRum.startResource).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(DdRum.stopResource).toHaveBeenCalledTimes(1);
|
|
51
|
+
|
|
52
|
+
// WHEN
|
|
53
|
+
DdRum.startResource.mockClear();
|
|
54
|
+
DdRum.stopResource.mockClear();
|
|
55
|
+
DdRumResourceTracking.stopTracking();
|
|
56
|
+
executeRequest();
|
|
57
|
+
|
|
58
|
+
// THEN
|
|
59
|
+
expect(DdRum.startResource).toHaveBeenCalledTimes(0);
|
|
60
|
+
expect(DdRum.stopResource).toHaveBeenCalledTimes(0);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('does not report the resource when it is an internal resource', async () => {
|
|
64
|
+
// GIVEN
|
|
65
|
+
global.XMLHttpRequest = XMLHttpRequestMock;
|
|
66
|
+
DdRumResourceTracking.startTracking({
|
|
67
|
+
tracingSamplingRate: 100,
|
|
68
|
+
firstPartyHosts: ['example.com']
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// WHEN
|
|
72
|
+
executeRequest('http://192.168.1.20:8081/logs');
|
|
73
|
+
await flushPromises();
|
|
74
|
+
|
|
75
|
+
// THEN
|
|
76
|
+
expect(DdRum.startResource).not.toHaveBeenCalled();
|
|
77
|
+
expect(DdRum.stopResource).not.toHaveBeenCalled();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,87 @@
|
|
|
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 { Hostname } from './firstPartyHosts';
|
|
8
|
+
import { isHostFirstParty } from './firstPartyHosts';
|
|
9
|
+
|
|
10
|
+
export type DdRumResourceTracingAttributes =
|
|
11
|
+
| {
|
|
12
|
+
tracingStrategy: 'KEEP';
|
|
13
|
+
traceId: string;
|
|
14
|
+
spanId: string;
|
|
15
|
+
samplingPriorityHeader: '1';
|
|
16
|
+
}
|
|
17
|
+
| {
|
|
18
|
+
tracingStrategy: 'DISCARD';
|
|
19
|
+
traceId?: void;
|
|
20
|
+
spanId?: void;
|
|
21
|
+
samplingPriorityHeader: '0';
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const DISCARDED_TRACE_ATTRIBUTES: DdRumResourceTracingAttributes = {
|
|
25
|
+
samplingPriorityHeader: '0',
|
|
26
|
+
tracingStrategy: 'DISCARD'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const getTracingAttributes = ({
|
|
30
|
+
hostname,
|
|
31
|
+
firstPartyHostsRegex,
|
|
32
|
+
tracingSamplingRate
|
|
33
|
+
}: {
|
|
34
|
+
hostname: Hostname | null;
|
|
35
|
+
firstPartyHostsRegex: RegExp;
|
|
36
|
+
tracingSamplingRate: number;
|
|
37
|
+
}): DdRumResourceTracingAttributes => {
|
|
38
|
+
if (hostname === null) {
|
|
39
|
+
return DISCARDED_TRACE_ATTRIBUTES;
|
|
40
|
+
}
|
|
41
|
+
if (isHostFirstParty(hostname, firstPartyHostsRegex)) {
|
|
42
|
+
return generateTracingAttributesWithSampling(tracingSamplingRate);
|
|
43
|
+
}
|
|
44
|
+
return DISCARDED_TRACE_ATTRIBUTES;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const generateTracingAttributesWithSampling = (
|
|
48
|
+
tracingSamplingRate: number
|
|
49
|
+
): DdRumResourceTracingAttributes => {
|
|
50
|
+
if (Math.random() * 100 <= tracingSamplingRate) {
|
|
51
|
+
return {
|
|
52
|
+
traceId: generateTraceId(),
|
|
53
|
+
spanId: generateTraceId(),
|
|
54
|
+
samplingPriorityHeader: '1',
|
|
55
|
+
tracingStrategy: 'KEEP'
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return DISCARDED_TRACE_ATTRIBUTES;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/*
|
|
62
|
+
* This code was inspired from browser-sdk at (https://github.com/DataDog/browser-sdk/blob/master/packages/rum-core/src/domain/tracing/tracer.ts#L107)
|
|
63
|
+
*/
|
|
64
|
+
export function generateTraceId(): string {
|
|
65
|
+
const radix = 10;
|
|
66
|
+
const MAX_32_BITS_NUMBER = 4294967295; // 2^32-1
|
|
67
|
+
let low: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
|
|
68
|
+
let high: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
|
|
69
|
+
let str = '';
|
|
70
|
+
|
|
71
|
+
while (high > 0 && low > 0) {
|
|
72
|
+
// Create an intermediate value with the same modulo as the combined high and low value
|
|
73
|
+
// but requiring 36 bits max (32 for the low value + 4 for the high part)
|
|
74
|
+
const modH = high % radix;
|
|
75
|
+
const temp = (modH << 32) + low;
|
|
76
|
+
const digit = temp % radix;
|
|
77
|
+
|
|
78
|
+
// update the high value
|
|
79
|
+
high = (high - modH) / radix; // we reuse the modH to avoid the need of a floor op
|
|
80
|
+
// the low value reuses the previous temp value to account for the "missing mod" in the high update
|
|
81
|
+
low = (temp - digit) / radix; // we reuse the digit to avoid the need of a floor op
|
|
82
|
+
|
|
83
|
+
// update the string from right to left
|
|
84
|
+
str = digit.toString() + str;
|
|
85
|
+
}
|
|
86
|
+
return str;
|
|
87
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
|
|
10
|
+
export type Hostname = { _type: 'Hostname' } & string;
|
|
11
|
+
|
|
12
|
+
// This regex does not match anything
|
|
13
|
+
export const NO_MATCH_REGEX = new RegExp('a^');
|
|
14
|
+
|
|
15
|
+
export const firstPartyHostsRegexBuilder = (
|
|
16
|
+
firstPartyHosts: string[]
|
|
17
|
+
): RegExp => {
|
|
18
|
+
if (firstPartyHosts.length === 0) {
|
|
19
|
+
return NO_MATCH_REGEX;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
// A regexp for matching hosts, e.g. when `hosts` is "example.com", it will match
|
|
23
|
+
// "example.com", "api.example.com", but not "foo.com".
|
|
24
|
+
const firstPartyHostsRegex = new RegExp(
|
|
25
|
+
`^(.*\\.)*(${firstPartyHosts.map(host => `${host}$`).join('|')})`
|
|
26
|
+
);
|
|
27
|
+
firstPartyHostsRegex.test('test_the_regex_is_valid');
|
|
28
|
+
return firstPartyHostsRegex;
|
|
29
|
+
} catch (e) {
|
|
30
|
+
InternalLog.log(
|
|
31
|
+
`Invalid first party hosts list ${JSON.stringify(
|
|
32
|
+
firstPartyHosts
|
|
33
|
+
)}. Regular expressions are not allowed.`,
|
|
34
|
+
SdkVerbosity.WARN
|
|
35
|
+
);
|
|
36
|
+
return NO_MATCH_REGEX;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const isHostFirstParty = (
|
|
41
|
+
hostname: Hostname,
|
|
42
|
+
firstPartyHostsRegex: RegExp
|
|
43
|
+
): boolean => {
|
|
44
|
+
return firstPartyHostsRegex.test(hostname);
|
|
45
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
export interface RequestProxyOptions {
|
|
8
|
+
tracingSamplingRate: number;
|
|
9
|
+
firstPartyHostsRegex: RegExp;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export abstract class RequestProxy {
|
|
13
|
+
abstract onTrackingStart: (context: RequestProxyOptions) => void;
|
|
14
|
+
abstract onTrackingStop: () => void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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 { DdRumResourceTracingAttributes } from '../distributedTracing';
|
|
8
|
+
|
|
9
|
+
export interface RUMResource {
|
|
10
|
+
key: string;
|
|
11
|
+
request: {
|
|
12
|
+
method: string;
|
|
13
|
+
url: string;
|
|
14
|
+
kind: string;
|
|
15
|
+
};
|
|
16
|
+
tracingAttributes: DdRumResourceTracingAttributes;
|
|
17
|
+
response: {
|
|
18
|
+
statusCode: number;
|
|
19
|
+
size: number;
|
|
20
|
+
};
|
|
21
|
+
timings: {
|
|
22
|
+
startTime: number;
|
|
23
|
+
stopTime: number;
|
|
24
|
+
responseStartTime?: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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 { DdRum } from '../../../../../foundation';
|
|
8
|
+
import type { RUMResource } from '../../domain/interfaces/RumResource';
|
|
9
|
+
|
|
10
|
+
import { createTimings } from './resourceTiming';
|
|
11
|
+
|
|
12
|
+
type ResourceMapper = (resource: RUMResource) => RUMResource | null;
|
|
13
|
+
|
|
14
|
+
export class ResourceReporter {
|
|
15
|
+
private mappers: ResourceMapper[];
|
|
16
|
+
|
|
17
|
+
constructor(resourceMappers: ResourceMapper[]) {
|
|
18
|
+
this.mappers = resourceMappers;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
reportResource = (resource: RUMResource) => {
|
|
22
|
+
let modifiedResource: RUMResource | null = resource;
|
|
23
|
+
|
|
24
|
+
for (const mapper of this.mappers) {
|
|
25
|
+
modifiedResource = mapper(resource);
|
|
26
|
+
if (modifiedResource === null) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
reportResource(modifiedResource);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const formatResourceStartContext = (
|
|
36
|
+
tracingAttributes: RUMResource['tracingAttributes']
|
|
37
|
+
): Record<string, unknown> | undefined => {
|
|
38
|
+
return tracingAttributes.tracingStrategy === 'DISCARD'
|
|
39
|
+
? undefined
|
|
40
|
+
: {
|
|
41
|
+
'_dd.span_id': tracingAttributes.spanId,
|
|
42
|
+
'_dd.trace_id': tracingAttributes.traceId
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const formatResourceStopContext = (
|
|
47
|
+
timings: RUMResource['timings']
|
|
48
|
+
): Record<string, unknown> => {
|
|
49
|
+
return {
|
|
50
|
+
'_dd.resource_timings':
|
|
51
|
+
timings.responseStartTime !== undefined
|
|
52
|
+
? createTimings(
|
|
53
|
+
timings.startTime,
|
|
54
|
+
timings.responseStartTime,
|
|
55
|
+
timings.stopTime
|
|
56
|
+
)
|
|
57
|
+
: null
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const reportResource = async (resource: RUMResource) => {
|
|
62
|
+
await DdRum.startResource(
|
|
63
|
+
resource.key,
|
|
64
|
+
resource.request.method,
|
|
65
|
+
resource.request.url,
|
|
66
|
+
formatResourceStartContext(resource.tracingAttributes),
|
|
67
|
+
resource.timings.startTime
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
DdRum.stopResource(
|
|
71
|
+
resource.key,
|
|
72
|
+
resource.response.statusCode,
|
|
73
|
+
resource.request.kind,
|
|
74
|
+
resource.response.size,
|
|
75
|
+
formatResourceStopContext(resource.timings),
|
|
76
|
+
resource.timings.stopTime
|
|
77
|
+
);
|
|
78
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
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 type { RUMResource } from '../../../domain/interfaces/RumResource';
|
|
10
|
+
import { ResourceReporter } from '../ResourceReporter';
|
|
11
|
+
|
|
12
|
+
import { ResourceMockFactory } from './__utils__/ResourceMockFactory';
|
|
13
|
+
|
|
14
|
+
const resourceMockFactory = new ResourceMockFactory();
|
|
15
|
+
const DdRum = NativeModules.DdRum;
|
|
16
|
+
const flushPromises = () => new Promise(setImmediate);
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
DdRum.startResource.mockClear();
|
|
20
|
+
DdRum.stopResource.mockClear();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('Resource reporter', () => {
|
|
24
|
+
it('reports resource when no mapper is passed', async () => {
|
|
25
|
+
// GIVEN
|
|
26
|
+
const resourceReporter = new ResourceReporter([]);
|
|
27
|
+
const resource = resourceMockFactory.getBasicResource();
|
|
28
|
+
|
|
29
|
+
// WHEN
|
|
30
|
+
resourceReporter.reportResource(resource);
|
|
31
|
+
await flushPromises();
|
|
32
|
+
// THEN
|
|
33
|
+
expect(DdRum.startResource).toHaveBeenCalledTimes(1);
|
|
34
|
+
expect(DdRum.stopResource).toHaveBeenCalledTimes(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('applies mappers when report resource is called', async () => {
|
|
38
|
+
// GIVEN
|
|
39
|
+
const setURLToGoogle = (resource: RUMResource) => {
|
|
40
|
+
resource.request.url = 'https://google.com/';
|
|
41
|
+
return resource;
|
|
42
|
+
};
|
|
43
|
+
const resourceReporter = new ResourceReporter([setURLToGoogle]);
|
|
44
|
+
const resource = resourceMockFactory.getCustomResource({
|
|
45
|
+
request: {
|
|
46
|
+
method: 'GET',
|
|
47
|
+
url: 'https://blabla.com',
|
|
48
|
+
kind: 'xhr'
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// WHEN
|
|
53
|
+
resourceReporter.reportResource(resource);
|
|
54
|
+
await flushPromises();
|
|
55
|
+
|
|
56
|
+
// THEN
|
|
57
|
+
expect(DdRum.startResource).toHaveBeenCalledWith(
|
|
58
|
+
expect.anything(),
|
|
59
|
+
expect.anything(),
|
|
60
|
+
'https://google.com/',
|
|
61
|
+
expect.anything(),
|
|
62
|
+
expect.anything()
|
|
63
|
+
);
|
|
64
|
+
expect(DdRum.stopResource).toHaveBeenCalledTimes(1);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('drops the resource when a mapper returns null', async () => {
|
|
68
|
+
// GIVEN
|
|
69
|
+
const discardResource = (resource: RUMResource) => {
|
|
70
|
+
return null;
|
|
71
|
+
};
|
|
72
|
+
const resourceReporter = new ResourceReporter([discardResource]);
|
|
73
|
+
const resource = resourceMockFactory.getBasicResource();
|
|
74
|
+
|
|
75
|
+
// WHEN
|
|
76
|
+
resourceReporter.reportResource(resource);
|
|
77
|
+
await flushPromises();
|
|
78
|
+
|
|
79
|
+
// THEN
|
|
80
|
+
expect(DdRum.startResource).not.toHaveBeenCalled();
|
|
81
|
+
expect(DdRum.stopResource).not.toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
* This method returns a new object each time to avoid overriding the same object
|
|
11
|
+
*/
|
|
12
|
+
const getBasicResource = (): RUMResource => ({
|
|
13
|
+
key: 'baseResourceKey',
|
|
14
|
+
request: {
|
|
15
|
+
method: 'GET',
|
|
16
|
+
url: 'https://api.example.com/api/test',
|
|
17
|
+
kind: 'xhr'
|
|
18
|
+
},
|
|
19
|
+
tracingAttributes: {
|
|
20
|
+
tracingStrategy: 'DISCARD',
|
|
21
|
+
samplingPriorityHeader: '0'
|
|
22
|
+
},
|
|
23
|
+
response: {
|
|
24
|
+
statusCode: 200,
|
|
25
|
+
size: 483
|
|
26
|
+
},
|
|
27
|
+
timings: {
|
|
28
|
+
startTime: 1000,
|
|
29
|
+
stopTime: 2000,
|
|
30
|
+
responseStartTime: 1500
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export class ResourceMockFactory {
|
|
35
|
+
getBasicResource = () => {
|
|
36
|
+
return getBasicResource();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
getCustomResource = (
|
|
40
|
+
partialResource: Partial<RUMResource>
|
|
41
|
+
): RUMResource => {
|
|
42
|
+
const baseResource = getBasicResource();
|
|
43
|
+
return Object.assign(baseResource, partialResource);
|
|
44
|
+
};
|
|
45
|
+
}
|