@datadog/mobile-react-native 1.0.0-rc7 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +2 -0
  3. package/android/build.gradle +1 -1
  4. package/datadog-sourcemaps.gradle +137 -0
  5. package/ios/Sources/DdSdk.swift +6 -3
  6. package/lib/commonjs/DdSdkReactNative.js +7 -3
  7. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  8. package/lib/commonjs/DdSdkReactNativeConfiguration.js +2 -0
  9. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  10. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
  11. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  12. package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
  13. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  14. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +51 -0
  15. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  16. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
  17. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  18. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  19. package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  20. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
  21. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  22. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  26. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js +42 -0
  27. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  28. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
  29. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  30. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
  31. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  32. package/lib/commonjs/version.js +1 -1
  33. package/lib/commonjs/version.js.map +1 -1
  34. package/lib/module/DdSdkReactNative.js +6 -2
  35. package/lib/module/DdSdkReactNative.js.map +1 -1
  36. package/lib/module/DdSdkReactNativeConfiguration.js +2 -0
  37. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  38. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
  39. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  40. package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
  41. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  42. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +34 -0
  43. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  44. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
  45. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  46. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  47. package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  48. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
  49. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  50. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
  51. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  52. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
  53. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  54. package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js +30 -0
  55. package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  56. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
  57. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  58. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
  59. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  60. package/lib/module/version.js +1 -1
  61. package/lib/module/version.js.map +1 -1
  62. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +1 -0
  63. package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  64. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  65. package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  66. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  67. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
  68. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  69. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  70. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  71. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  72. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  73. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  74. package/lib/typescript/version.d.ts +1 -1
  75. package/package.json +6 -4
  76. package/src/DdSdkReactNative.tsx +8 -4
  77. package/src/DdSdkReactNativeConfiguration.tsx +1 -0
  78. package/src/__tests__/DdSdkReactNative.test.tsx +42 -24
  79. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +1 -1
  80. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
  81. package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
  82. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
  83. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
  84. package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
  85. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
  86. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
  87. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +52 -0
  88. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
  89. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +26 -0
  90. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
  91. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
  92. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
  93. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
  94. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
  95. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
  96. package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
  97. package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
  98. package/src/rum/instrumentation/resourceTracking/implementation/__tests__/URLHostParser.test.ts +40 -0
  99. package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +178 -69
  100. package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
  101. package/src/version.ts +1 -1
  102. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -298
  103. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  104. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
  105. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
  106. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -276
  107. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  108. package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
  109. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
  110. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -31
  111. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
  112. package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
  113. package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -361
  114. package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
  115. package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
@@ -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
+ });
@@ -1,4 +1,4 @@
1
- import { generateTraceId } from '../../../rum/instrumentation/TraceIdentifier';
1
+ import { generateTraceId } from '../distributedTracing';
2
2
 
3
3
  it('M return an unique identifier W toString', async () => {
4
4
  // GIVEN
@@ -0,0 +1,22 @@
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 { firstPartyHostsRegexBuilder } from '../firstPartyHosts';
8
+
9
+ describe('firstPartyHosts', () => {
10
+ describe('firstPartyHostsRegexBuilder', () => {
11
+ it('returns a RegExp that matches hosts', () => {
12
+ const regex = firstPartyHostsRegexBuilder(['api.example.com']);
13
+ expect(regex.test('api.example.com')).toBe(true);
14
+ expect(regex.test('api.myapi.com')).toBe(false);
15
+ });
16
+
17
+ it('escapes special characters in hosts', () => {
18
+ const regex = firstPartyHostsRegexBuilder(['api.example.com']);
19
+ expect(regex.test('apiiexample.com')).toBe(false);
20
+ });
21
+ });
22
+ });
@@ -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,52 @@
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
+ // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
16
+ const escapeRegExp = (string: string) => {
17
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
18
+ };
19
+
20
+ export const firstPartyHostsRegexBuilder = (
21
+ firstPartyHosts: string[]
22
+ ): RegExp => {
23
+ if (firstPartyHosts.length === 0) {
24
+ return NO_MATCH_REGEX;
25
+ }
26
+ try {
27
+ // A regexp for matching hosts, e.g. when `hosts` is "example.com", it will match
28
+ // "example.com", "api.example.com", but not "foo.com".
29
+ const firstPartyHostsRegex = new RegExp(
30
+ `^(.*\\.)*(${firstPartyHosts
31
+ .map(host => `${escapeRegExp(host)}$`)
32
+ .join('|')})`
33
+ );
34
+ firstPartyHostsRegex.test('test_the_regex_is_valid');
35
+ return firstPartyHostsRegex;
36
+ } catch (e) {
37
+ InternalLog.log(
38
+ `Invalid first party hosts list ${JSON.stringify(
39
+ firstPartyHosts
40
+ )}. Regular expressions are not allowed.`,
41
+ SdkVerbosity.WARN
42
+ );
43
+ return NO_MATCH_REGEX;
44
+ }
45
+ };
46
+
47
+ export const isHostFirstParty = (
48
+ hostname: Hostname,
49
+ firstPartyHostsRegex: RegExp
50
+ ): boolean => {
51
+ return firstPartyHostsRegex.test(hostname);
52
+ };
@@ -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
+ }
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { filterDevResource } from '../internalDevResourceBlocklist';
8
+
9
+ import { ResourceMockFactory } from './__utils__/ResourceMockFactory';
10
+
11
+ const resourceMockFactory = new ResourceMockFactory();
12
+
13
+ describe('internalDevResourceBlocklist', () => {
14
+ describe('filterDevResource', () => {
15
+ it.each(['192.168.1.20', '10.46.29.155', '172.28.1.20'])(
16
+ 'returns null when a expo logs call with ip %s is made',
17
+ ip => {
18
+ const resource = resourceMockFactory.getCustomResource({
19
+ request: {
20
+ method: 'GET',
21
+ url: `http://${ip}:8081/logs`,
22
+ kind: 'xhr'
23
+ }
24
+ });
25
+ expect(filterDevResource(resource)).toBeNull();
26
+ }
27
+ );
28
+ it('returns null when an expo logs call with custom port is made', () => {
29
+ const resource = resourceMockFactory.getCustomResource({
30
+ request: {
31
+ method: 'GET',
32
+ url: 'http://10.46.29.155:19000/logs',
33
+ kind: 'xhr'
34
+ }
35
+ });
36
+ expect(filterDevResource(resource)).toBeNull();
37
+ });
38
+ it('returns null when a rn symbolicate call is made', () => {
39
+ const resource = resourceMockFactory.getCustomResource({
40
+ request: {
41
+ method: 'GET',
42
+ url: 'http://localhost:8081/symbolicate',
43
+ kind: 'xhr'
44
+ }
45
+ });
46
+ expect(filterDevResource(resource)).toBeNull();
47
+ });
48
+ it('returns null when an rn symbolicate call with custom port is made', () => {
49
+ const resource = resourceMockFactory.getCustomResource({
50
+ request: {
51
+ method: 'GET',
52
+ url: 'http://localhost:40/symbolicate',
53
+ kind: 'xhr'
54
+ }
55
+ });
56
+ expect(filterDevResource(resource)).toBeNull();
57
+ });
58
+ it('returns the resource when the resource is not a dev resource', () => {
59
+ const resource = resourceMockFactory.getCustomResource({
60
+ request: {
61
+ method: 'GET',
62
+ url: 'http://11.22.33.22:443/logs',
63
+ kind: 'xhr'
64
+ }
65
+ });
66
+ expect(filterDevResource(resource)).not.toBeNull();
67
+ });
68
+ });
69
+ });
@@ -0,0 +1,50 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type { RUMResource } from '../../domain/interfaces/RumResource';
8
+
9
+ /**
10
+ * Expo sends all console.* calls to the packager. As we log all API calls
11
+ * when the SDKVerbosity is DEBUG, this would result in an infinite loop of
12
+ * `console.log` and API calls, creating 250 RUM resources per second.
13
+ *
14
+ * The hostname is always going to be localhost or a local IP.
15
+ *
16
+ * An example URL is http://192.168.1.20:8081/logs or http://10.46.29.155:19000/logs
17
+ */
18
+ const EXPO_DEV_LOGS_REGEX = new RegExp(
19
+ '^http://((10|172|192).[0-9]+.[0-9]+.[0-9]+|localhost):[0-9]+/logs$'
20
+ );
21
+
22
+ /**
23
+ * This call is made every time the RN packager reloads the js in dev mode.
24
+ */
25
+ const RN_PACKAGER_SYMBOLICATE_REGEX = new RegExp(
26
+ '^http://localhost:[0-9]+/symbolicate$'
27
+ );
28
+
29
+ const internalResourceBlocklist: RegExp[] = [
30
+ EXPO_DEV_LOGS_REGEX,
31
+ RN_PACKAGER_SYMBOLICATE_REGEX
32
+ ];
33
+
34
+ /**
35
+ * Filters RN symbolicate calls and Expo logs calls that happen only in dev.
36
+ * @param resource RUMResource
37
+ */
38
+ export const filterDevResource = (
39
+ resource: RUMResource
40
+ ): RUMResource | null => {
41
+ // TODO: if we get the confirmation by Expo that the
42
+ // logs call is only made when __DEV__ is true, add an
43
+ // early return for when it is false.
44
+ for (const resourceRegex of internalResourceBlocklist) {
45
+ if (resourceRegex.test(resource.request.url)) {
46
+ return null;
47
+ }
48
+ }
49
+ return resource;
50
+ };