@datadog/mobile-react-native 1.3.1 → 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/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 +145 -5
- 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/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/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/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/implementation/XHRProxy.d.ts +0 -5
- 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/index.tsx +6 -3
- package/src/rum/__tests__/DdRum.test.ts +17 -1
- package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +6 -3
- package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +13 -2
- 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/XHRProxy.ts +8 -23
- package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +251 -73
- package/src/rum/types.ts +19 -0
- package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +1 -1
- package/src/version.ts +1 -1
|
@@ -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
|
};
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import Timer from '../../../../Timer';
|
|
8
|
+
import { getTracingHeaders } from '../domain/distributedTracingHeaders';
|
|
8
9
|
import type { DdRumResourceTracingAttributes } from '../domain/distributedTracing';
|
|
9
10
|
import { getTracingAttributes } from '../domain/distributedTracing';
|
|
10
11
|
import type { RequestProxyOptions } from '../domain/interfaces/RequestProxy';
|
|
@@ -14,12 +15,6 @@ import type { ResourceReporter } from './DatadogRumResource/ResourceReporter';
|
|
|
14
15
|
import { URLHostParser } from './URLHostParser';
|
|
15
16
|
import { calculateResponseSize } from './responseSize';
|
|
16
17
|
|
|
17
|
-
export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
|
|
18
|
-
export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
|
|
19
|
-
export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
|
|
20
|
-
export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
|
|
21
|
-
export const ORIGIN_RUM = 'rum';
|
|
22
|
-
|
|
23
18
|
const RESPONSE_START_LABEL = 'response_start';
|
|
24
19
|
|
|
25
20
|
interface DdRumXhr extends XMLHttpRequest {
|
|
@@ -77,7 +72,7 @@ const proxyOpen = (
|
|
|
77
72
|
context: RequestProxyOptions
|
|
78
73
|
): void => {
|
|
79
74
|
const originalXhrOpen = xhrType.prototype.open;
|
|
80
|
-
const
|
|
75
|
+
const firstPartyHostsRegexMap = context.firstPartyHostsRegexMap;
|
|
81
76
|
const tracingSamplingRate = context.tracingSamplingRate;
|
|
82
77
|
|
|
83
78
|
xhrType.prototype.open = function (
|
|
@@ -95,7 +90,7 @@ const proxyOpen = (
|
|
|
95
90
|
timer: new Timer(),
|
|
96
91
|
tracingAttributes: getTracingAttributes({
|
|
97
92
|
hostname,
|
|
98
|
-
|
|
93
|
+
firstPartyHostsRegexMap,
|
|
99
94
|
tracingSamplingRate
|
|
100
95
|
})
|
|
101
96
|
};
|
|
@@ -112,23 +107,13 @@ const proxySend = (providers: XHRProxyProviders): void => {
|
|
|
112
107
|
if (this._datadog_xhr) {
|
|
113
108
|
// keep track of start time
|
|
114
109
|
this._datadog_xhr.timer.start();
|
|
115
|
-
this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM);
|
|
116
110
|
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
SAMPLING_PRIORITY_HEADER_KEY,
|
|
120
|
-
tracingAttributes.samplingPriorityHeader
|
|
111
|
+
const tracingHeaders = getTracingHeaders(
|
|
112
|
+
this._datadog_xhr.tracingAttributes
|
|
121
113
|
);
|
|
122
|
-
|
|
123
|
-
this.setRequestHeader(
|
|
124
|
-
|
|
125
|
-
tracingAttributes.traceId
|
|
126
|
-
);
|
|
127
|
-
this.setRequestHeader(
|
|
128
|
-
PARENT_ID_HEADER_KEY,
|
|
129
|
-
tracingAttributes.spanId
|
|
130
|
-
);
|
|
131
|
-
}
|
|
114
|
+
tracingHeaders.forEach(({ header, value }) => {
|
|
115
|
+
this.setRequestHeader(header, value);
|
|
116
|
+
});
|
|
132
117
|
}
|
|
133
118
|
|
|
134
119
|
proxyOnReadyStateChange(this, providers);
|