@datadog/mobile-react-native 1.3.0 → 1.4.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.
- package/DatadogSDKReactNative.podspec +2 -2
- package/android/build.gradle +58 -64
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +36 -2
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +14 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +162 -16
- package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +7 -0
- package/ios/Sources/DdSdk.swift +2 -2
- package/ios/Sources/RNDdSdkConfiguration.swift +42 -0
- package/lib/commonjs/DdSdkReactNative.js +2 -2
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js +26 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +9 -0
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js +59 -41
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js +136 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +39 -6
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +3 -3
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +12 -21
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -1
- package/lib/commonjs/rum/types.js +19 -1
- package/lib/commonjs/rum/types.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +3 -3
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/DdSdkReactNativeConfiguration.js +22 -0
- package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/module/index.js +3 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +9 -0
- package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +2 -2
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js +57 -35
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js +115 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +37 -3
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +3 -3
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +10 -15
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -1
- package/lib/module/rum/types.js +16 -0
- package/lib/module/rum/types.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +11 -3
- package/lib/typescript/index.d.ts +3 -3
- package/lib/typescript/rum/eventMappers/resourceEventMapper.d.ts +0 -1
- package/lib/typescript/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +2 -1
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +32 -6
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.d.ts +21 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +4 -2
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +6 -1
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +0 -1
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +0 -6
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +0 -1
- package/lib/typescript/rum/types.d.ts +17 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/DdSdkReactNative.tsx +8 -4
- package/src/DdSdkReactNativeConfiguration.tsx +36 -3
- package/src/__tests__/DdSdkReactNative.test.tsx +16 -4
- package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +7 -6
- package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +2 -1
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +130 -114
- package/src/index.tsx +6 -3
- package/src/rum/__tests__/DdRum.test.ts +17 -1
- package/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +8 -0
- package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +6 -3
- package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +15 -3
- package/src/rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts +64 -24
- package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +19 -7
- package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +82 -40
- package/src/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.ts +141 -0
- package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +43 -7
- package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +8 -1
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +4 -4
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +2 -1
- package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +8 -23
- package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +257 -74
- package/src/rum/types.ts +19 -0
- package/src/sdk/DatadogProvider/Buffer/__tests__/BufferSingleton.test.ts +2 -1
- package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +5 -4
- package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +6 -2
- package/src/version.ts +1 -1
|
@@ -26,6 +26,7 @@ export class DdRumUserInteractionTracking {
|
|
|
26
26
|
private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor();
|
|
27
27
|
private static originalCreateElement = React.createElement;
|
|
28
28
|
private static originalMemo = React.memo;
|
|
29
|
+
private static originalJsx = null;
|
|
29
30
|
|
|
30
31
|
private static patchCreateElementFunction = (
|
|
31
32
|
originalFunction: typeof React.createElement,
|
|
@@ -78,6 +79,7 @@ export class DdRumUserInteractionTracking {
|
|
|
78
79
|
try {
|
|
79
80
|
const jsxRuntime = getJsxRuntime();
|
|
80
81
|
const originaljsx = jsxRuntime.jsx;
|
|
82
|
+
this.originalJsx = originaljsx;
|
|
81
83
|
jsxRuntime.jsx = (
|
|
82
84
|
...args: Parameters<typeof React.createElement>
|
|
83
85
|
): ReturnType<typeof React.createElement> => {
|
|
@@ -88,6 +90,7 @@ export class DdRumUserInteractionTracking {
|
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
const originalMemo = React.memo;
|
|
93
|
+
|
|
91
94
|
React.memo = (
|
|
92
95
|
component: any,
|
|
93
96
|
propsAreEqual?: (prevProps: any, newProps: any) => boolean
|
|
@@ -129,5 +132,10 @@ export class DdRumUserInteractionTracking {
|
|
|
129
132
|
React.createElement = this.originalCreateElement;
|
|
130
133
|
React.memo = this.originalMemo;
|
|
131
134
|
DdRumUserInteractionTracking.isTracking = false;
|
|
135
|
+
if (this.originalJsx) {
|
|
136
|
+
const jsxRuntime = getJsxRuntime();
|
|
137
|
+
jsxRuntime.jsx = this.originalJsx;
|
|
138
|
+
this.originalJsx = null;
|
|
139
|
+
}
|
|
132
140
|
}
|
|
133
141
|
}
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
import { InternalLog } from '../../../InternalLog';
|
|
8
8
|
import { SdkVerbosity } from '../../../SdkVerbosity';
|
|
9
|
+
import type { FirstPartyHost } from '../../types';
|
|
9
10
|
|
|
10
|
-
import {
|
|
11
|
+
import { firstPartyHostsRegexMapBuilder } from './domain/firstPartyHosts';
|
|
11
12
|
import type { RequestProxy } from './domain/interfaces/RequestProxy';
|
|
12
13
|
import { ResourceReporter } from './implementation/DatadogRumResource/ResourceReporter';
|
|
13
14
|
import { filterDevResource } from './implementation/DatadogRumResource/internalDevResourceBlocklist';
|
|
@@ -28,7 +29,7 @@ export class DdRumResourceTracking {
|
|
|
28
29
|
firstPartyHosts
|
|
29
30
|
}: {
|
|
30
31
|
tracingSamplingRate: number;
|
|
31
|
-
firstPartyHosts:
|
|
32
|
+
firstPartyHosts: FirstPartyHost[];
|
|
32
33
|
}): void {
|
|
33
34
|
// extra safety to avoid proxying the XHR class twice
|
|
34
35
|
if (DdRumResourceTracking.isTracking) {
|
|
@@ -45,7 +46,9 @@ export class DdRumResourceTracking {
|
|
|
45
46
|
});
|
|
46
47
|
this.requestProxy.onTrackingStart({
|
|
47
48
|
tracingSamplingRate,
|
|
48
|
-
|
|
49
|
+
firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder(
|
|
50
|
+
firstPartyHosts
|
|
51
|
+
)
|
|
49
52
|
});
|
|
50
53
|
|
|
51
54
|
InternalLog.log(
|
|
@@ -7,13 +7,15 @@
|
|
|
7
7
|
import { NativeModules } from 'react-native';
|
|
8
8
|
|
|
9
9
|
import { BufferSingleton } from '../../../../sdk/DatadogProvider/Buffer/BufferSingleton';
|
|
10
|
+
import { PropagatorType } from '../../../types';
|
|
10
11
|
import { DdRumResourceTracking } from '../DdRumResourceTracking';
|
|
11
12
|
|
|
12
13
|
import { XMLHttpRequestMock } from './__utils__/XMLHttpRequestMock';
|
|
13
14
|
|
|
14
15
|
const DdRum = NativeModules.DdRum;
|
|
15
16
|
|
|
16
|
-
const flushPromises = () =>
|
|
17
|
+
const flushPromises = () =>
|
|
18
|
+
new Promise(jest.requireActual('timers').setImmediate);
|
|
17
19
|
|
|
18
20
|
beforeEach(() => {
|
|
19
21
|
DdRum.startResource.mockClear();
|
|
@@ -40,7 +42,12 @@ describe('DdRumResourceTracking', () => {
|
|
|
40
42
|
global.XMLHttpRequest = XMLHttpRequestMock;
|
|
41
43
|
DdRumResourceTracking.startTracking({
|
|
42
44
|
tracingSamplingRate: 100,
|
|
43
|
-
firstPartyHosts: [
|
|
45
|
+
firstPartyHosts: [
|
|
46
|
+
{
|
|
47
|
+
match: 'example.com',
|
|
48
|
+
propagatorTypes: [PropagatorType.DATADOG]
|
|
49
|
+
}
|
|
50
|
+
]
|
|
44
51
|
});
|
|
45
52
|
|
|
46
53
|
// WHEN
|
|
@@ -67,7 +74,12 @@ describe('DdRumResourceTracking', () => {
|
|
|
67
74
|
global.XMLHttpRequest = XMLHttpRequestMock;
|
|
68
75
|
DdRumResourceTracking.startTracking({
|
|
69
76
|
tracingSamplingRate: 100,
|
|
70
|
-
firstPartyHosts: [
|
|
77
|
+
firstPartyHosts: [
|
|
78
|
+
{
|
|
79
|
+
match: 'example.com',
|
|
80
|
+
propagatorTypes: [PropagatorType.DATADOG]
|
|
81
|
+
}
|
|
82
|
+
]
|
|
71
83
|
});
|
|
72
84
|
|
|
73
85
|
// WHEN
|
package/src/rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts
CHANGED
|
@@ -1,31 +1,71 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TraceIdentifier } from '../distributedTracing';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
describe('TraceIdentifier', () => {
|
|
4
|
+
it('M return an unique identifier W toString', async () => {
|
|
5
|
+
// GIVEN
|
|
6
|
+
const generatedIds = new Set<string>();
|
|
7
|
+
const iterations = 100;
|
|
8
|
+
let counter = iterations;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
// WHEN
|
|
11
|
+
while (counter-- > 0) {
|
|
12
|
+
generatedIds.add(new TraceIdentifier().toString(10));
|
|
13
|
+
}
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
15
|
+
// THEN
|
|
16
|
+
expect(generatedIds.size).toBe(iterations);
|
|
17
|
+
});
|
|
17
18
|
|
|
18
|
-
it('M return an 64 bits positive integer W toString', async () => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
it('M return an 64 bits positive integer W toString', async () => {
|
|
20
|
+
let iterations = 100;
|
|
21
|
+
while (iterations-- > 0) {
|
|
22
|
+
// GIVEN
|
|
23
|
+
const id = new TraceIdentifier().toString(10);
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
// THEN
|
|
26
|
+
expect(id).toMatch(/[1-9]{1,19}/);
|
|
27
|
+
// should be less than the max 64 bits integer
|
|
28
|
+
if (id.length === 19) {
|
|
29
|
+
expect(id < '9223372036854775807').toBeTruthy();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('M return an 64 bits positive hex W toString(16)', async () => {
|
|
35
|
+
let iterations = 100;
|
|
36
|
+
while (iterations-- > 0) {
|
|
37
|
+
// GIVEN
|
|
38
|
+
const trace = new TraceIdentifier();
|
|
39
|
+
const id = trace.toString(16);
|
|
40
|
+
const paddedId = trace.toPaddedString(16, 16);
|
|
41
|
+
|
|
42
|
+
// THEN
|
|
43
|
+
expect(id).toMatch(/[1-9a-f]{1,16}/);
|
|
44
|
+
expect(paddedId).toMatch(/[0-9a-f]{16}/);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('M return an 64 bits positive padded hex W toPaddedString(16, 32)', async () => {
|
|
49
|
+
let iterations = 100;
|
|
50
|
+
while (iterations-- > 0) {
|
|
51
|
+
// GIVEN
|
|
52
|
+
const id = new TraceIdentifier().toPaddedString(16, 32);
|
|
53
|
+
|
|
54
|
+
// THEN
|
|
55
|
+
expect(id).not.toMatch(/[0]{32}/);
|
|
56
|
+
expect(id).toMatch(/[0]{16}[0-9a-f]{16}/);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('M return original string hex W toPaddedString(16, 10)', async () => {
|
|
61
|
+
let iterations = 100;
|
|
62
|
+
while (iterations-- > 0) {
|
|
63
|
+
// GIVEN
|
|
64
|
+
const id = new TraceIdentifier().toPaddedString(16, 10);
|
|
65
|
+
|
|
66
|
+
// THEN
|
|
67
|
+
expect(id).not.toMatch(/[0]{10}/);
|
|
68
|
+
expect(id).toMatch(/[0-9a-f]{10}/);
|
|
29
69
|
}
|
|
30
|
-
}
|
|
70
|
+
});
|
|
31
71
|
});
|
|
@@ -4,19 +4,31 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { PropagatorType } from '../../../../types';
|
|
8
|
+
import { firstPartyHostsRegexMapBuilder } from '../firstPartyHosts';
|
|
8
9
|
|
|
9
10
|
describe('firstPartyHosts', () => {
|
|
10
|
-
describe('
|
|
11
|
+
describe('firstPartyHostsRegexMapBuilder', () => {
|
|
11
12
|
it('returns a RegExp that matches hosts', () => {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
const regexMap = firstPartyHostsRegexMapBuilder([
|
|
14
|
+
{
|
|
15
|
+
match: 'api.example.com',
|
|
16
|
+
propagatorTypes: [PropagatorType.DATADOG]
|
|
17
|
+
}
|
|
18
|
+
]);
|
|
19
|
+
expect(regexMap[0].propagatorType).toBe('datadog');
|
|
20
|
+
expect(regexMap[0].regex.test('api.example.com')).toBe(true);
|
|
21
|
+
expect(regexMap[0].regex.test('api.myapi.com')).toBe(false);
|
|
15
22
|
});
|
|
16
23
|
|
|
17
24
|
it('escapes special characters in hosts', () => {
|
|
18
|
-
const
|
|
19
|
-
|
|
25
|
+
const regexMap = firstPartyHostsRegexMapBuilder([
|
|
26
|
+
{
|
|
27
|
+
match: 'api.example.com',
|
|
28
|
+
propagatorTypes: [PropagatorType.DATADOG]
|
|
29
|
+
}
|
|
30
|
+
]);
|
|
31
|
+
expect(regexMap[0].regex.test('apiiexample.com')).toBe(false);
|
|
20
32
|
});
|
|
21
33
|
});
|
|
22
34
|
});
|
|
@@ -4,16 +4,20 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import type { PropagatorType } from '../../../types';
|
|
8
|
+
|
|
7
9
|
import type { Hostname } from './firstPartyHosts';
|
|
8
|
-
import {
|
|
10
|
+
import { getPropagatorsForHost } from './firstPartyHosts';
|
|
11
|
+
import type { RegexMap } from './interfaces/RequestProxy';
|
|
9
12
|
|
|
10
13
|
export type DdRumResourceTracingAttributes =
|
|
11
14
|
| {
|
|
12
15
|
tracingStrategy: 'KEEP';
|
|
13
|
-
traceId:
|
|
14
|
-
spanId:
|
|
15
|
-
samplingPriorityHeader: '1';
|
|
16
|
+
traceId: TraceId;
|
|
17
|
+
spanId: SpanId;
|
|
18
|
+
samplingPriorityHeader: '1' | '0';
|
|
16
19
|
rulePsr: number;
|
|
20
|
+
propagatorTypes: PropagatorType[];
|
|
17
21
|
}
|
|
18
22
|
| {
|
|
19
23
|
tracingStrategy: 'DISCARD';
|
|
@@ -29,61 +33,99 @@ const DISCARDED_TRACE_ATTRIBUTES: DdRumResourceTracingAttributes = {
|
|
|
29
33
|
|
|
30
34
|
export const getTracingAttributes = ({
|
|
31
35
|
hostname,
|
|
32
|
-
|
|
36
|
+
firstPartyHostsRegexMap,
|
|
33
37
|
tracingSamplingRate
|
|
34
38
|
}: {
|
|
35
39
|
hostname: Hostname | null;
|
|
36
|
-
|
|
40
|
+
firstPartyHostsRegexMap: RegexMap;
|
|
37
41
|
tracingSamplingRate: number;
|
|
38
42
|
}): DdRumResourceTracingAttributes => {
|
|
39
43
|
if (hostname === null) {
|
|
40
44
|
return DISCARDED_TRACE_ATTRIBUTES;
|
|
41
45
|
}
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
const propagatorsForHost = getPropagatorsForHost(
|
|
47
|
+
hostname,
|
|
48
|
+
firstPartyHostsRegexMap
|
|
49
|
+
);
|
|
50
|
+
if (propagatorsForHost) {
|
|
51
|
+
return generateTracingAttributesWithSampling(
|
|
52
|
+
tracingSamplingRate,
|
|
53
|
+
propagatorsForHost
|
|
54
|
+
);
|
|
44
55
|
}
|
|
45
56
|
return DISCARDED_TRACE_ATTRIBUTES;
|
|
46
57
|
};
|
|
47
58
|
|
|
48
59
|
const generateTracingAttributesWithSampling = (
|
|
49
|
-
tracingSamplingRate: number
|
|
60
|
+
tracingSamplingRate: number,
|
|
61
|
+
propagatorTypes: PropagatorType[]
|
|
50
62
|
): DdRumResourceTracingAttributes => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
63
|
+
const isSampled = Math.random() * 100 <= tracingSamplingRate;
|
|
64
|
+
const tracingAttributes: DdRumResourceTracingAttributes = {
|
|
65
|
+
traceId: new TraceIdentifier() as TraceId,
|
|
66
|
+
spanId: new TraceIdentifier() as SpanId,
|
|
67
|
+
samplingPriorityHeader: isSampled ? '1' : '0',
|
|
68
|
+
tracingStrategy: 'KEEP',
|
|
69
|
+
rulePsr: tracingSamplingRate / 100,
|
|
70
|
+
propagatorTypes
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return tracingAttributes;
|
|
61
74
|
};
|
|
62
75
|
|
|
63
|
-
|
|
64
|
-
*
|
|
76
|
+
/**
|
|
77
|
+
* Using branded types will ensure we don't accidentally use
|
|
78
|
+
* traceId for spanId when generating headers.
|
|
65
79
|
*/
|
|
66
|
-
export
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
let low: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
|
|
70
|
-
let high: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
|
|
71
|
-
let str = '';
|
|
80
|
+
export type TraceId = TraceIdentifier & {
|
|
81
|
+
_brand: 'traceId';
|
|
82
|
+
};
|
|
72
83
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const modH = high % radix;
|
|
77
|
-
const temp = (modH << 32) + low;
|
|
78
|
-
const digit = temp % radix;
|
|
84
|
+
export type SpanId = TraceIdentifier & {
|
|
85
|
+
_brand: 'spanId';
|
|
86
|
+
};
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
88
|
+
/*
|
|
89
|
+
* This code was inspired from browser-sdk at (https://github.com/DataDog/browser-sdk/blob/0e9722d5b06f6d49264bc82cd254a207d647d66c/packages/rum-core/src/domain/tracing/tracer.ts#L190)
|
|
90
|
+
*/
|
|
91
|
+
const MAX_32_BITS_NUMBER = 4294967295; // 2^32-1
|
|
92
|
+
const MAX_31_BITS_NUMBER = 2147483647; // 2^31-1
|
|
93
|
+
export class TraceIdentifier {
|
|
94
|
+
private low: number;
|
|
95
|
+
private high: number;
|
|
84
96
|
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
constructor() {
|
|
98
|
+
// We need to have a 63 bits number max
|
|
99
|
+
this.high = Math.floor(Math.random() * MAX_31_BITS_NUMBER);
|
|
100
|
+
this.low = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
|
|
87
101
|
}
|
|
88
|
-
|
|
102
|
+
|
|
103
|
+
toString = (radix: number) => {
|
|
104
|
+
let low = this.low;
|
|
105
|
+
let high = this.high;
|
|
106
|
+
let str = '';
|
|
107
|
+
|
|
108
|
+
while (high > 0 || low > 0) {
|
|
109
|
+
const mod = (high % radix) * (MAX_32_BITS_NUMBER + 1) + low;
|
|
110
|
+
high = Math.floor(high / radix);
|
|
111
|
+
low = Math.floor(mod / radix);
|
|
112
|
+
str = (mod % radix).toString(radix) + str;
|
|
113
|
+
}
|
|
114
|
+
return str;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* This function pads the trace with `0`.
|
|
119
|
+
* It should not be used with a `length` lower than the trace, as we return the full trace in this case.
|
|
120
|
+
* @param radix radix for the trace
|
|
121
|
+
* @param length minimum length
|
|
122
|
+
* @returns padded string
|
|
123
|
+
*/
|
|
124
|
+
toPaddedString = (radix: number, length: number) => {
|
|
125
|
+
const traceId = this.toString(radix);
|
|
126
|
+
if (traceId.length > length) {
|
|
127
|
+
return traceId;
|
|
128
|
+
}
|
|
129
|
+
return Array(length - traceId.length + 1).join('0') + traceId;
|
|
130
|
+
};
|
|
89
131
|
}
|
|
@@ -0,0 +1,141 @@
|
|
|
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 { PropagatorType } from '../../../types';
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
DdRumResourceTracingAttributes,
|
|
11
|
+
SpanId,
|
|
12
|
+
TraceId
|
|
13
|
+
} from './distributedTracing';
|
|
14
|
+
|
|
15
|
+
export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
|
|
16
|
+
/**
|
|
17
|
+
* Datadog headers
|
|
18
|
+
*/
|
|
19
|
+
export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
|
|
20
|
+
export const ORIGIN_RUM = 'rum';
|
|
21
|
+
export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
|
|
22
|
+
export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
|
|
23
|
+
/**
|
|
24
|
+
* OTel headers
|
|
25
|
+
*/
|
|
26
|
+
export const TRACECONTEXT_HEADER_KEY = 'traceparent';
|
|
27
|
+
export const B3_HEADER_KEY = 'b3';
|
|
28
|
+
export const B3_MULTI_TRACE_ID_HEADER_KEY = 'X-B3-TraceId';
|
|
29
|
+
export const B3_MULTI_SPAN_ID_HEADER_KEY = 'X-B3-SpanId';
|
|
30
|
+
export const B3_MULTI_SAMPLED_HEADER_KEY = 'X-B3-Sampled';
|
|
31
|
+
|
|
32
|
+
export const getTracingHeaders = (
|
|
33
|
+
tracingAttributes: DdRumResourceTracingAttributes
|
|
34
|
+
): { header: string; value: string }[] => {
|
|
35
|
+
const headers: { header: string; value: string }[] = [];
|
|
36
|
+
if (tracingAttributes.tracingStrategy === 'DISCARD') {
|
|
37
|
+
return headers;
|
|
38
|
+
}
|
|
39
|
+
tracingAttributes.propagatorTypes.forEach(propagator => {
|
|
40
|
+
switch (propagator) {
|
|
41
|
+
case PropagatorType.DATADOG: {
|
|
42
|
+
headers.push(
|
|
43
|
+
{
|
|
44
|
+
header: ORIGIN_HEADER_KEY,
|
|
45
|
+
value: ORIGIN_RUM
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
header: SAMPLING_PRIORITY_HEADER_KEY,
|
|
49
|
+
value: tracingAttributes.samplingPriorityHeader
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
header: TRACE_ID_HEADER_KEY,
|
|
53
|
+
value: tracingAttributes.traceId.toString(10)
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
header: PARENT_ID_HEADER_KEY,
|
|
57
|
+
value: tracingAttributes.spanId.toString(10)
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case PropagatorType.TRACECONTEXT: {
|
|
63
|
+
headers.push({
|
|
64
|
+
header: TRACECONTEXT_HEADER_KEY,
|
|
65
|
+
value: generateTraceContextHeader({
|
|
66
|
+
version: '00',
|
|
67
|
+
traceId: tracingAttributes.traceId,
|
|
68
|
+
parentId: tracingAttributes.spanId,
|
|
69
|
+
isSampled:
|
|
70
|
+
tracingAttributes.samplingPriorityHeader === '1'
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case PropagatorType.B3: {
|
|
76
|
+
headers.push({
|
|
77
|
+
header: B3_HEADER_KEY,
|
|
78
|
+
value: generateB3Header({
|
|
79
|
+
traceId: tracingAttributes.traceId,
|
|
80
|
+
spanId: tracingAttributes.spanId,
|
|
81
|
+
isSampled:
|
|
82
|
+
tracingAttributes.samplingPriorityHeader === '1'
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case PropagatorType.B3MULTI: {
|
|
88
|
+
headers.push(
|
|
89
|
+
{
|
|
90
|
+
header: B3_MULTI_TRACE_ID_HEADER_KEY,
|
|
91
|
+
value: tracingAttributes.traceId.toPaddedString(16, 32)
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
header: B3_MULTI_SPAN_ID_HEADER_KEY,
|
|
95
|
+
value: tracingAttributes.spanId.toPaddedString(16, 16)
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
header: B3_MULTI_SAMPLED_HEADER_KEY,
|
|
99
|
+
value: tracingAttributes.samplingPriorityHeader
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return headers;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const generateTraceContextHeader = ({
|
|
110
|
+
version,
|
|
111
|
+
traceId,
|
|
112
|
+
parentId,
|
|
113
|
+
isSampled
|
|
114
|
+
}: {
|
|
115
|
+
version: string;
|
|
116
|
+
traceId: TraceId;
|
|
117
|
+
parentId: SpanId;
|
|
118
|
+
isSampled: boolean;
|
|
119
|
+
}) => {
|
|
120
|
+
const flags = isSampled ? '01' : '00';
|
|
121
|
+
return `${version}-${traceId.toPaddedString(
|
|
122
|
+
16,
|
|
123
|
+
32
|
|
124
|
+
)}-${parentId.toPaddedString(16, 16)}-${flags}`;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const generateB3Header = ({
|
|
128
|
+
traceId,
|
|
129
|
+
spanId,
|
|
130
|
+
isSampled
|
|
131
|
+
}: {
|
|
132
|
+
traceId: TraceId;
|
|
133
|
+
spanId: SpanId;
|
|
134
|
+
isSampled: boolean;
|
|
135
|
+
}) => {
|
|
136
|
+
const flags = isSampled ? '1' : '0';
|
|
137
|
+
return `${traceId.toPaddedString(16, 32)}-${spanId.toPaddedString(
|
|
138
|
+
16,
|
|
139
|
+
16
|
|
140
|
+
)}-${flags}`;
|
|
141
|
+
};
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
import { InternalLog } from '../../../../InternalLog';
|
|
8
8
|
import { SdkVerbosity } from '../../../../SdkVerbosity';
|
|
9
|
+
import type { FirstPartyHost, PropagatorType } from '../../../types';
|
|
10
|
+
|
|
11
|
+
import type { RegexMap } from './interfaces/RequestProxy';
|
|
9
12
|
|
|
10
13
|
export type Hostname = { _type: 'Hostname' } & string;
|
|
11
14
|
|
|
@@ -17,9 +20,36 @@ const escapeRegExp = (string: string) => {
|
|
|
17
20
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
18
21
|
};
|
|
19
22
|
|
|
20
|
-
export const
|
|
21
|
-
firstPartyHosts:
|
|
22
|
-
):
|
|
23
|
+
export const firstPartyHostsRegexMapBuilder = (
|
|
24
|
+
firstPartyHosts: FirstPartyHost[]
|
|
25
|
+
): RegexMap => {
|
|
26
|
+
const hostsMap: Record<PropagatorType, string[]> = {
|
|
27
|
+
datadog: [],
|
|
28
|
+
tracecontext: [],
|
|
29
|
+
b3: [],
|
|
30
|
+
b3multi: []
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
firstPartyHosts.forEach(host => {
|
|
34
|
+
host.propagatorTypes.forEach(propagatorType => {
|
|
35
|
+
hostsMap[propagatorType].push(host.match);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const regexMap: { regex: RegExp; propagatorType: PropagatorType }[] = [];
|
|
40
|
+
Object.entries(hostsMap).forEach(([propagatorType, hosts]) => {
|
|
41
|
+
if (hosts.length > 0) {
|
|
42
|
+
regexMap.push({
|
|
43
|
+
propagatorType: propagatorType as PropagatorType,
|
|
44
|
+
regex: firstPartyHostsRegexBuilder(hosts)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return regexMap;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const firstPartyHostsRegexBuilder = (firstPartyHosts: string[]): RegExp => {
|
|
23
53
|
if (firstPartyHosts.length === 0) {
|
|
24
54
|
return NO_MATCH_REGEX;
|
|
25
55
|
}
|
|
@@ -44,9 +74,15 @@ export const firstPartyHostsRegexBuilder = (
|
|
|
44
74
|
}
|
|
45
75
|
};
|
|
46
76
|
|
|
47
|
-
export const
|
|
77
|
+
export const getPropagatorsForHost = (
|
|
48
78
|
hostname: Hostname,
|
|
49
|
-
|
|
50
|
-
):
|
|
51
|
-
|
|
79
|
+
firstPartyHostsRegexMap: RegexMap
|
|
80
|
+
): PropagatorType[] | null => {
|
|
81
|
+
const matchedPropagatorTypes: PropagatorType[] = [];
|
|
82
|
+
firstPartyHostsRegexMap.forEach(({ regex, propagatorType }) => {
|
|
83
|
+
if (regex.test(hostname)) {
|
|
84
|
+
matchedPropagatorTypes.push(propagatorType);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return matchedPropagatorTypes.length > 0 ? matchedPropagatorTypes : null;
|
|
52
88
|
};
|
|
@@ -4,11 +4,18 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import type { PropagatorType } from '../../../../types';
|
|
8
|
+
|
|
7
9
|
export interface RequestProxyOptions {
|
|
8
10
|
tracingSamplingRate: number;
|
|
9
|
-
|
|
11
|
+
firstPartyHostsRegexMap: RegexMap;
|
|
10
12
|
}
|
|
11
13
|
|
|
14
|
+
export type RegexMap = {
|
|
15
|
+
regex: RegExp;
|
|
16
|
+
propagatorType: PropagatorType;
|
|
17
|
+
}[];
|
|
18
|
+
|
|
12
19
|
export abstract class RequestProxy {
|
|
13
20
|
abstract onTrackingStart: (context: RequestProxyOptions) => void;
|
|
14
21
|
abstract onTrackingStop: () => void;
|
|
@@ -34,12 +34,12 @@ export class ResourceReporter {
|
|
|
34
34
|
|
|
35
35
|
const formatResourceStartContext = (
|
|
36
36
|
tracingAttributes: RUMResource['tracingAttributes']
|
|
37
|
-
): Record<string,
|
|
38
|
-
return tracingAttributes.
|
|
37
|
+
): Record<string, string | number> | undefined => {
|
|
38
|
+
return tracingAttributes.samplingPriorityHeader === '0'
|
|
39
39
|
? undefined
|
|
40
40
|
: {
|
|
41
|
-
'_dd.span_id': tracingAttributes.spanId,
|
|
42
|
-
'_dd.trace_id': tracingAttributes.traceId,
|
|
41
|
+
'_dd.span_id': tracingAttributes.spanId.toString(10),
|
|
42
|
+
'_dd.trace_id': tracingAttributes.traceId.toString(10),
|
|
43
43
|
'_dd.rule_psr': tracingAttributes.rulePsr
|
|
44
44
|
};
|
|
45
45
|
};
|
|
@@ -14,7 +14,8 @@ import { ResourceMockFactory } from './__utils__/ResourceMockFactory';
|
|
|
14
14
|
|
|
15
15
|
const resourceMockFactory = new ResourceMockFactory();
|
|
16
16
|
const DdRum = NativeModules.DdRum;
|
|
17
|
-
const flushPromises = () =>
|
|
17
|
+
const flushPromises = () =>
|
|
18
|
+
new Promise(jest.requireActual('timers').setImmediate);
|
|
18
19
|
|
|
19
20
|
beforeEach(() => {
|
|
20
21
|
DdRum.startResource.mockClear();
|