@datadog/mobile-react-native 1.0.0-rc5 → 1.0.0-rc8
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/README.md +48 -17
- package/android/build.gradle +1 -1
- package/android/gradle.properties +4 -4
- package/ios/Sources/DdSdk.swift +6 -3
- package/lib/commonjs/DdSdkReactNative.js +35 -31
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js +20 -7
- package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/commonjs/InternalLog.js +10 -5
- package/lib/commonjs/InternalLog.js.map +1 -1
- package/lib/commonjs/ProxyConfiguration.js.map +1 -1
- package/lib/commonjs/SdkVerbosity.js.map +1 -1
- package/lib/commonjs/TimeProvider.js +72 -0
- package/lib/commonjs/TimeProvider.js.map +1 -0
- package/lib/commonjs/Timer.js +10 -23
- package/lib/commonjs/Timer.js.map +1 -1
- package/lib/commonjs/TrackingConsent.js.map +1 -1
- package/lib/commonjs/foundation.js +19 -54
- package/lib/commonjs/foundation.js.map +1 -1
- package/lib/commonjs/index.js +27 -19
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +16 -11
- package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +19 -23
- package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +118 -42
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +46 -19
- package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -4
- package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
- package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js +34 -0
- package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/URLHostParser.js +33 -0
- package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +1 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +33 -29
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/DdSdkReactNativeConfiguration.js +21 -8
- package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/module/InternalLog.js +11 -5
- package/lib/module/InternalLog.js.map +1 -1
- package/lib/module/ProxyConfiguration.js.map +1 -1
- package/lib/module/SdkVerbosity.js.map +1 -1
- package/lib/module/TimeProvider.js +63 -0
- package/lib/module/TimeProvider.js.map +1 -0
- package/lib/module/Timer.js +14 -23
- package/lib/module/Timer.js.map +1 -1
- package/lib/module/TrackingConsent.js.map +1 -1
- package/lib/module/foundation.js +16 -39
- package/lib/module/foundation.js.map +1 -1
- package/lib/module/index.js +4 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/rum/instrumentation/DdEventsInterceptor.js +12 -10
- package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumErrorTracking.js +19 -23
- package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js +113 -39
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +44 -17
- package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumXhr.js +1 -1
- package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
- package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
- package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js +25 -0
- package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
- package/lib/module/rum/instrumentation/TraceIdentifier.js.map +1 -1
- package/lib/module/rum/instrumentation/URLHostParser.js +24 -0
- package/lib/module/rum/instrumentation/URLHostParser.js.map +1 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DdSdkReactNative.d.ts +6 -6
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +15 -3
- package/lib/typescript/InternalLog.d.ts +5 -0
- package/lib/typescript/TimeProvider.d.ts +20 -0
- package/lib/typescript/Timer.d.ts +3 -2
- package/lib/typescript/index.d.ts +4 -3
- package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -2
- package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +19 -9
- package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
- package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +13 -3
- package/lib/typescript/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
- package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +6 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +6 -3
- package/src/DdSdkReactNative.tsx +116 -70
- package/src/DdSdkReactNativeConfiguration.tsx +22 -13
- package/src/InternalLog.tsx +18 -16
- package/src/ProxyConfiguration.tsx +5 -5
- package/src/SdkVerbosity.tsx +4 -4
- package/src/TimeProvider.tsx +64 -0
- package/src/Timer.ts +20 -35
- package/src/TrackingConsent.tsx +3 -3
- package/src/__mocks__/InternalLog.tsx +9 -0
- package/src/__tests__/DdRumWrapper.test.tsx +40 -33
- package/src/__tests__/DdSdkReactNative.test.tsx +712 -513
- package/src/__tests__/InternalLog.test.tsx +114 -117
- package/src/__tests__/TimeProvider.test.tsx +75 -0
- package/src/__tests__/Timer.test.ts +148 -57
- package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +151 -92
- package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +168 -116
- package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +789 -644
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +239 -85
- package/src/__tests__/rum/instrumentation/ShallowObjectEqualityChecker.test.ts +31 -0
- package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +13 -15
- package/src/__tests__/rum/instrumentation/URLHostParser.test.ts +40 -0
- package/src/__tests__/rum/instrumentation/__utils__/XMLHttpRequestMock.ts +97 -0
- package/src/foundation.tsx +13 -10
- package/src/index.tsx +15 -4
- package/src/rum/instrumentation/DdEventsInterceptor.tsx +51 -35
- package/src/rum/instrumentation/DdRumErrorTracking.tsx +49 -44
- package/src/rum/instrumentation/DdRumResourceTracking.tsx +369 -234
- package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +72 -29
- package/src/rum/instrumentation/DdRumXhr.tsx +21 -11
- package/src/rum/instrumentation/EventsInterceptor.tsx +3 -3
- package/src/rum/instrumentation/NoOpEventsInterceptor.tsx +2 -2
- package/src/rum/instrumentation/ShallowObjectEqualityChecker.ts +24 -0
- package/src/rum/instrumentation/TraceIdentifier.tsx +19 -19
- package/src/rum/instrumentation/URLHostParser.ts +23 -0
- package/src/types.tsx +199 -158
- package/src/version.ts +1 -1
|
@@ -5,57 +5,70 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { Platform } from 'react-native';
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../SdkVerbosity';
|
|
9
11
|
import Timer from '../../Timer';
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
12
|
+
import { DdRum } from '../../foundation';
|
|
13
|
+
|
|
14
|
+
import type { DdRumResourceTracingAttributes, DdRumXhr } from './DdRumXhr';
|
|
15
|
+
import { generateTraceId } from './TraceIdentifier';
|
|
16
|
+
import { URLHostParser } from './URLHostParser';
|
|
14
17
|
|
|
15
|
-
export const TRACE_ID_HEADER_KEY =
|
|
16
|
-
export const PARENT_ID_HEADER_KEY =
|
|
17
|
-
export const ORIGIN_HEADER_KEY =
|
|
18
|
-
export const SAMPLING_PRIORITY_HEADER_KEY =
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
18
|
+
export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
|
|
19
|
+
export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
|
|
20
|
+
export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
|
|
21
|
+
export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
|
|
22
|
+
export const ORIGIN_RUM = 'rum';
|
|
23
|
+
export const RESOURCE_SIZE_ERROR_MESSAGE =
|
|
24
|
+
"Couldn't get resource size, because an error occured: ";
|
|
21
25
|
|
|
22
|
-
const RESPONSE_START_LABEL =
|
|
23
|
-
const MISSING_RESOURCE_SIZE = -1
|
|
26
|
+
const RESPONSE_START_LABEL = 'response_start';
|
|
27
|
+
const MISSING_RESOURCE_SIZE = -1;
|
|
28
|
+
|
|
29
|
+
// This regex does not match anything
|
|
30
|
+
const NO_MATCH_REGEX = new RegExp('a^');
|
|
24
31
|
|
|
25
32
|
interface Timing {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Time relative (absolute in case of iOS) to some point, in ns.
|
|
35
|
+
*/
|
|
36
|
+
startTime: number;
|
|
37
|
+
/**
|
|
38
|
+
* Duration in ns.
|
|
39
|
+
*/
|
|
40
|
+
duration: number;
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
interface ResourceTimings {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
// unlike in Performance API it is not the time until request
|
|
45
|
+
// starts (requestStart, before it can be connect, SSL, DNS),
|
|
46
|
+
// but the time until the response is first seen
|
|
47
|
+
firstByte: Timing;
|
|
48
|
+
download: Timing;
|
|
49
|
+
// required by iOS, total timing from the beginning to the end
|
|
50
|
+
fetch: Timing;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
function createTimings(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
function createTimings(
|
|
54
|
+
startTime: number,
|
|
55
|
+
responseStartTime: number,
|
|
56
|
+
responseEndTime: number
|
|
57
|
+
): ResourceTimings {
|
|
58
|
+
const firstByte = formatTiming(startTime, startTime, responseStartTime);
|
|
59
|
+
const download = formatTiming(
|
|
60
|
+
startTime,
|
|
61
|
+
responseStartTime,
|
|
62
|
+
responseEndTime
|
|
63
|
+
);
|
|
64
|
+
// needed for iOS, simply total duration from start to end
|
|
65
|
+
const fetch = formatTiming(startTime, startTime, responseEndTime);
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
firstByte,
|
|
69
|
+
download,
|
|
70
|
+
fetch
|
|
71
|
+
};
|
|
59
72
|
}
|
|
60
73
|
|
|
61
74
|
/**
|
|
@@ -64,228 +77,350 @@ function createTimings(startTime: number,
|
|
|
64
77
|
* @param end End time (absolute) of the timing
|
|
65
78
|
*/
|
|
66
79
|
function formatTiming(origin: number, start: number, end: number): Timing {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
80
|
+
return {
|
|
81
|
+
duration: timeToNanos(end - start),
|
|
82
|
+
// if it is Android, startTime should be relative to the origin,
|
|
83
|
+
// if it is iOS - absolute (unix timestamp)
|
|
84
|
+
startTime:
|
|
85
|
+
Platform.OS === 'ios'
|
|
86
|
+
? timeToNanos(start)
|
|
87
|
+
: timeToNanos(start - origin)
|
|
88
|
+
};
|
|
73
89
|
}
|
|
74
90
|
|
|
75
91
|
function timeToNanos(durationMs: number): number {
|
|
76
|
-
|
|
92
|
+
return +(durationMs * 1e6).toFixed(0);
|
|
77
93
|
}
|
|
78
94
|
|
|
79
95
|
function byteLength(str: string): number {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
96
|
+
// This is a weird trick, but it works.
|
|
97
|
+
// Output is the same as TextEncoder.encode(...).length
|
|
98
|
+
return unescape(encodeURI(str)).length;
|
|
83
99
|
}
|
|
84
100
|
|
|
85
|
-
function getResponseContentLengthFromHeader(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
101
|
+
function getResponseContentLengthFromHeader(
|
|
102
|
+
xhr: XMLHttpRequest
|
|
103
|
+
): number | null {
|
|
104
|
+
const contentLengthHeader = parseInt(
|
|
105
|
+
xhr.getResponseHeader('Content-Length') ?? '',
|
|
106
|
+
10
|
|
107
|
+
);
|
|
108
|
+
if (!isNaN(contentLengthHeader)) {
|
|
109
|
+
return contentLengthHeader;
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
91
112
|
}
|
|
92
113
|
|
|
93
114
|
export function calculateResponseSize(xhr: XMLHttpRequest): number {
|
|
115
|
+
const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
|
|
116
|
+
if (contentLengthHeader != null) {
|
|
117
|
+
return contentLengthHeader as number;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const response = xhr.response;
|
|
121
|
+
if (!response) {
|
|
122
|
+
return MISSING_RESOURCE_SIZE;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let size;
|
|
126
|
+
try {
|
|
127
|
+
switch (xhr.responseType) {
|
|
128
|
+
case '':
|
|
129
|
+
case 'text':
|
|
130
|
+
// String
|
|
131
|
+
size = byteLength(response);
|
|
132
|
+
break;
|
|
133
|
+
case 'blob':
|
|
134
|
+
size = response.size;
|
|
135
|
+
break;
|
|
136
|
+
case 'arraybuffer':
|
|
137
|
+
size = response.byteLength;
|
|
138
|
+
break;
|
|
139
|
+
case 'document':
|
|
140
|
+
// currently not supported by RN as of 0.66
|
|
141
|
+
// HTML Document or XML Document
|
|
142
|
+
break;
|
|
143
|
+
case 'json':
|
|
144
|
+
// plain JS object
|
|
145
|
+
// original size was lost, because this is the object which was parsed.
|
|
146
|
+
// We can only convert back to the string and calculate the size,
|
|
147
|
+
// which will roughly match original.
|
|
148
|
+
size = byteLength(JSON.stringify(response));
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
} catch (e) {
|
|
154
|
+
InternalLog.log(
|
|
155
|
+
`${RESOURCE_SIZE_ERROR_MESSAGE}${e}`,
|
|
156
|
+
SdkVerbosity.ERROR
|
|
157
|
+
);
|
|
158
|
+
}
|
|
94
159
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return contentLengthHeader as number;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const response = xhr.response
|
|
101
|
-
if (!response) {
|
|
102
|
-
return MISSING_RESOURCE_SIZE;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
let size;
|
|
106
|
-
try {
|
|
107
|
-
switch (xhr.responseType) {
|
|
108
|
-
case '':
|
|
109
|
-
case 'text':
|
|
110
|
-
// String
|
|
111
|
-
size = byteLength(response);
|
|
112
|
-
break;
|
|
113
|
-
case 'blob':
|
|
114
|
-
size = response.size;
|
|
115
|
-
break;
|
|
116
|
-
case 'arraybuffer':
|
|
117
|
-
size = response.byteLength;
|
|
118
|
-
break;
|
|
119
|
-
case 'document':
|
|
120
|
-
// currently not supported by RN as of 0.66
|
|
121
|
-
// HTML Document or XML Document
|
|
122
|
-
break;
|
|
123
|
-
case 'json':
|
|
124
|
-
// plain JS object
|
|
125
|
-
// original size was lost, because this is the object which was parsed.
|
|
126
|
-
// We can only convert back to the string and calculate the size,
|
|
127
|
-
// which will roughly match original.
|
|
128
|
-
size = byteLength(JSON.stringify(response))
|
|
129
|
-
break;
|
|
130
|
-
default:
|
|
131
|
-
break;
|
|
160
|
+
if (typeof size !== 'number') {
|
|
161
|
+
return MISSING_RESOURCE_SIZE;
|
|
132
162
|
}
|
|
133
|
-
|
|
134
|
-
console.error("Couldn't get resource size, because of the error", e);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (typeof size !== 'number') {
|
|
138
|
-
return MISSING_RESOURCE_SIZE;
|
|
139
|
-
}
|
|
140
|
-
return size;
|
|
163
|
+
return size;
|
|
141
164
|
}
|
|
142
165
|
|
|
166
|
+
const generateTracingAttributesWithSampling = (
|
|
167
|
+
tracingSamplingRate: number
|
|
168
|
+
): DdRumResourceTracingAttributes => {
|
|
169
|
+
if (Math.random() * 100 <= tracingSamplingRate) {
|
|
170
|
+
return {
|
|
171
|
+
traceId: generateTraceId(),
|
|
172
|
+
spanId: generateTraceId(),
|
|
173
|
+
samplingPriorityHeader: '1',
|
|
174
|
+
tracingStrategy: 'KEEP'
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
samplingPriorityHeader: '0',
|
|
179
|
+
tracingStrategy: 'DISCARD'
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const getFirstPartyHostsRegex = (firstPartyHosts: string[]): RegExp => {
|
|
184
|
+
if (firstPartyHosts.length === 0) {
|
|
185
|
+
return NO_MATCH_REGEX;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
// A regexp for matching hosts, e.g. when `hosts` is "example.com", it will match
|
|
189
|
+
// "example.com", "api.example.com", but not "foo.com".
|
|
190
|
+
const firstPartyHostsRegex = new RegExp(
|
|
191
|
+
`^(.*\\.)*(${firstPartyHosts.map(host => `${host}$`).join('|')})`
|
|
192
|
+
);
|
|
193
|
+
firstPartyHostsRegex.test('test_the_regex_is_valid');
|
|
194
|
+
return firstPartyHostsRegex;
|
|
195
|
+
} catch (e) {
|
|
196
|
+
InternalLog.log(
|
|
197
|
+
`Invalid first party hosts list ${JSON.stringify(
|
|
198
|
+
firstPartyHosts
|
|
199
|
+
)}. Regular expressions are not allowed.`,
|
|
200
|
+
SdkVerbosity.WARN
|
|
201
|
+
);
|
|
202
|
+
return NO_MATCH_REGEX;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
143
206
|
/**
|
|
144
|
-
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
|
|
145
|
-
*/
|
|
207
|
+
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
|
|
208
|
+
*/
|
|
146
209
|
export class DdRumResourceTracking {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return
|
|
210
|
+
private static isTracking = false;
|
|
211
|
+
private static tracingSamplingRate: number;
|
|
212
|
+
private static firstPartyHostsRegex: RegExp = NO_MATCH_REGEX; // matches nothing by default
|
|
213
|
+
|
|
214
|
+
private static originalXhrOpen: any;
|
|
215
|
+
private static originalXhrSend: any;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Starts tracking resources and sends a RUM Resource event every time a network request is detected.
|
|
219
|
+
*/
|
|
220
|
+
static startTracking({
|
|
221
|
+
tracingSamplingRate,
|
|
222
|
+
firstPartyHosts
|
|
223
|
+
}: {
|
|
224
|
+
tracingSamplingRate: number;
|
|
225
|
+
firstPartyHosts: string[];
|
|
226
|
+
}): void {
|
|
227
|
+
DdRumResourceTracking.startTrackingInternal(XMLHttpRequest, {
|
|
228
|
+
tracingSamplingRate,
|
|
229
|
+
firstPartyHosts
|
|
230
|
+
});
|
|
169
231
|
}
|
|
170
232
|
|
|
171
|
-
|
|
172
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
|
|
235
|
+
*/
|
|
236
|
+
static startTrackingInternal(
|
|
237
|
+
xhrType: typeof XMLHttpRequest,
|
|
238
|
+
{
|
|
239
|
+
tracingSamplingRate,
|
|
240
|
+
firstPartyHosts
|
|
241
|
+
}: { tracingSamplingRate: number; firstPartyHosts: string[] }
|
|
242
|
+
): void {
|
|
243
|
+
// extra safety to avoid proxying the XHR class twice
|
|
244
|
+
if (DdRumResourceTracking.isTracking) {
|
|
245
|
+
InternalLog.log(
|
|
246
|
+
'Datadog SDK is already tracking XHR resources',
|
|
247
|
+
SdkVerbosity.WARN
|
|
248
|
+
);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
173
251
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
252
|
+
DdRumResourceTracking.tracingSamplingRate = tracingSamplingRate;
|
|
253
|
+
DdRumResourceTracking.firstPartyHostsRegex = getFirstPartyHostsRegex(
|
|
254
|
+
firstPartyHosts
|
|
255
|
+
);
|
|
177
256
|
|
|
257
|
+
DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
|
|
258
|
+
DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
|
|
178
259
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
260
|
+
DdRumResourceTracking.proxyXhr(xhrType);
|
|
261
|
+
InternalLog.log(
|
|
262
|
+
'Datadog SDK is tracking XHR resources',
|
|
263
|
+
SdkVerbosity.INFO
|
|
264
|
+
);
|
|
184
265
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
private static proxyOpen(xhrType: any): void {
|
|
194
|
-
const originalXhrOpen = this.originalXhrOpen;
|
|
195
|
-
xhrType.prototype.open = function (this: DdRumXhr, method: string, url: string) {
|
|
196
|
-
// Keep track of the method and url
|
|
197
|
-
// start time is tracked by the `send` method
|
|
198
|
-
const spanId = generateTraceId()
|
|
199
|
-
const traceId = generateTraceId()
|
|
200
|
-
this._datadog_xhr = {
|
|
201
|
-
method,
|
|
202
|
-
url: url,
|
|
203
|
-
reported: false,
|
|
204
|
-
timer: new Timer(),
|
|
205
|
-
spanId: spanId,
|
|
206
|
-
traceId: traceId
|
|
207
|
-
}
|
|
208
|
-
// eslint-disable-next-line prefer-rest-params
|
|
209
|
-
return originalXhrOpen.apply(this, arguments as any)
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private static proxySend(xhrType: any): void {
|
|
214
|
-
const originalXhrSend = this.originalXhrSend;
|
|
215
|
-
xhrType.prototype.send = function (this: DdRumXhr) {
|
|
216
|
-
|
|
217
|
-
if (this._datadog_xhr) {
|
|
218
|
-
// keep track of start time
|
|
219
|
-
this._datadog_xhr.timer.start()
|
|
220
|
-
this.setRequestHeader(TRACE_ID_HEADER_KEY, this._datadog_xhr.traceId)
|
|
221
|
-
this.setRequestHeader(PARENT_ID_HEADER_KEY, this._datadog_xhr.spanId)
|
|
222
|
-
this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM)
|
|
223
|
-
this.setRequestHeader(SAMPLING_PRIORITY_HEADER_KEY, "1")
|
|
224
|
-
this.setRequestHeader(SAMPLED_HEADER_KEY, "1")
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
|
|
228
|
-
|
|
229
|
-
// eslint-disable-next-line prefer-rest-params
|
|
230
|
-
return originalXhrSend.apply(this, arguments as any);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private static proxyOnReadyStateChange(xhrProxy: DdRumXhr, xhrType: any): void {
|
|
235
|
-
const originalOnreadystatechange = xhrProxy.onreadystatechange
|
|
236
|
-
xhrProxy.onreadystatechange = function () {
|
|
237
|
-
if (xhrProxy.readyState === xhrType.DONE) {
|
|
238
|
-
if (!xhrProxy._datadog_xhr.reported) {
|
|
239
|
-
DdRumResourceTracking.reportXhr(xhrProxy);
|
|
240
|
-
xhrProxy._datadog_xhr.reported = true;
|
|
266
|
+
|
|
267
|
+
static stopTracking(): void {
|
|
268
|
+
if (DdRumResourceTracking.isTracking) {
|
|
269
|
+
DdRumResourceTracking.isTracking = false;
|
|
270
|
+
XMLHttpRequest.prototype.open =
|
|
271
|
+
DdRumResourceTracking.originalXhrOpen;
|
|
272
|
+
XMLHttpRequest.prototype.send =
|
|
273
|
+
DdRumResourceTracking.originalXhrSend;
|
|
241
274
|
}
|
|
242
|
-
} else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
|
|
243
|
-
xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL)
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (originalOnreadystatechange) {
|
|
247
|
-
// eslint-disable-next-line prefer-rest-params
|
|
248
|
-
originalOnreadystatechange.apply(xhrProxy, arguments as any)
|
|
249
|
-
}
|
|
250
275
|
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
private static reportXhr(xhrProxy: DdRumXhr): void {
|
|
254
276
|
|
|
255
|
-
|
|
277
|
+
static getTracingAttributes(url: string): DdRumResourceTracingAttributes {
|
|
278
|
+
try {
|
|
279
|
+
const hostname = URLHostParser(url);
|
|
280
|
+
|
|
281
|
+
if (DdRumResourceTracking.firstPartyHostsRegex.test(hostname)) {
|
|
282
|
+
return generateTracingAttributesWithSampling(
|
|
283
|
+
DdRumResourceTracking.tracingSamplingRate
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
samplingPriorityHeader: '0',
|
|
288
|
+
tracingStrategy: 'DISCARD'
|
|
289
|
+
};
|
|
290
|
+
} catch (e) {
|
|
291
|
+
InternalLog.log(
|
|
292
|
+
`Impossible to cast ${url} as URL`,
|
|
293
|
+
SdkVerbosity.WARN
|
|
294
|
+
);
|
|
295
|
+
return {
|
|
296
|
+
samplingPriorityHeader: '0',
|
|
297
|
+
tracingStrategy: 'DISCARD'
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
256
301
|
|
|
257
|
-
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
303
|
+
static proxyXhr(xhrType: any): void {
|
|
304
|
+
this.proxyOpen(xhrType);
|
|
305
|
+
this.proxySend(xhrType);
|
|
306
|
+
}
|
|
258
307
|
|
|
259
|
-
|
|
308
|
+
private static proxyOpen(xhrType: any): void {
|
|
309
|
+
const originalXhrOpen = this.originalXhrOpen;
|
|
310
|
+
xhrType.prototype.open = function (
|
|
311
|
+
this: DdRumXhr,
|
|
312
|
+
method: string,
|
|
313
|
+
url: string
|
|
314
|
+
) {
|
|
315
|
+
// Keep track of the method and url
|
|
316
|
+
// start time is tracked by the `send` method
|
|
317
|
+
this._datadog_xhr = {
|
|
318
|
+
method,
|
|
319
|
+
url,
|
|
320
|
+
reported: false,
|
|
321
|
+
timer: new Timer(),
|
|
322
|
+
tracingAttributes: DdRumResourceTracking.getTracingAttributes(
|
|
323
|
+
url
|
|
324
|
+
)
|
|
325
|
+
};
|
|
326
|
+
// eslint-disable-next-line prefer-rest-params
|
|
327
|
+
return originalXhrOpen.apply(this, arguments as any);
|
|
328
|
+
};
|
|
329
|
+
}
|
|
260
330
|
|
|
261
|
-
|
|
331
|
+
private static proxySend(xhrType: any): void {
|
|
332
|
+
const originalXhrSend = this.originalXhrSend;
|
|
333
|
+
xhrType.prototype.send = function (this: DdRumXhr) {
|
|
334
|
+
if (this._datadog_xhr) {
|
|
335
|
+
// keep track of start time
|
|
336
|
+
this._datadog_xhr.timer.start();
|
|
337
|
+
this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM);
|
|
338
|
+
|
|
339
|
+
const tracingAttributes = this._datadog_xhr.tracingAttributes;
|
|
340
|
+
this.setRequestHeader(
|
|
341
|
+
SAMPLING_PRIORITY_HEADER_KEY,
|
|
342
|
+
tracingAttributes.samplingPriorityHeader
|
|
343
|
+
);
|
|
344
|
+
if (tracingAttributes.tracingStrategy !== 'DISCARD') {
|
|
345
|
+
this.setRequestHeader(
|
|
346
|
+
TRACE_ID_HEADER_KEY,
|
|
347
|
+
tracingAttributes.traceId
|
|
348
|
+
);
|
|
349
|
+
this.setRequestHeader(
|
|
350
|
+
PARENT_ID_HEADER_KEY,
|
|
351
|
+
tracingAttributes.spanId
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
|
|
357
|
+
|
|
358
|
+
// eslint-disable-next-line prefer-rest-params
|
|
359
|
+
return originalXhrSend.apply(this, arguments as any);
|
|
360
|
+
};
|
|
361
|
+
}
|
|
262
362
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
) : null
|
|
285
|
-
},
|
|
286
|
-
context.timer.stopTime
|
|
287
|
-
);
|
|
288
|
-
})
|
|
289
|
-
}
|
|
363
|
+
private static proxyOnReadyStateChange(
|
|
364
|
+
xhrProxy: DdRumXhr,
|
|
365
|
+
xhrType: any
|
|
366
|
+
): void {
|
|
367
|
+
const originalOnreadystatechange = xhrProxy.onreadystatechange;
|
|
368
|
+
xhrProxy.onreadystatechange = function () {
|
|
369
|
+
if (xhrProxy.readyState === xhrType.DONE) {
|
|
370
|
+
if (!xhrProxy._datadog_xhr.reported) {
|
|
371
|
+
DdRumResourceTracking.reportXhr(xhrProxy);
|
|
372
|
+
xhrProxy._datadog_xhr.reported = true;
|
|
373
|
+
}
|
|
374
|
+
} else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
|
|
375
|
+
xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (originalOnreadystatechange) {
|
|
379
|
+
// eslint-disable-next-line prefer-rest-params
|
|
380
|
+
originalOnreadystatechange.apply(xhrProxy, arguments as any);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
290
384
|
|
|
385
|
+
private static reportXhr(xhrProxy: DdRumXhr): void {
|
|
386
|
+
const responseSize = calculateResponseSize(xhrProxy);
|
|
387
|
+
|
|
388
|
+
const context = xhrProxy._datadog_xhr;
|
|
389
|
+
|
|
390
|
+
const key = `${context.timer.startTime}/${context.method}`;
|
|
391
|
+
|
|
392
|
+
context.timer.stop();
|
|
393
|
+
|
|
394
|
+
DdRum.startResource(
|
|
395
|
+
key,
|
|
396
|
+
context.method,
|
|
397
|
+
context.url,
|
|
398
|
+
context.tracingAttributes.tracingStrategy === 'DISCARD'
|
|
399
|
+
? undefined
|
|
400
|
+
: {
|
|
401
|
+
'_dd.span_id': context.tracingAttributes.spanId,
|
|
402
|
+
'_dd.trace_id': context.tracingAttributes.traceId
|
|
403
|
+
},
|
|
404
|
+
context.timer.startTime
|
|
405
|
+
).then(() => {
|
|
406
|
+
DdRum.stopResource(
|
|
407
|
+
key,
|
|
408
|
+
xhrProxy.status,
|
|
409
|
+
'xhr',
|
|
410
|
+
responseSize,
|
|
411
|
+
{
|
|
412
|
+
'_dd.resource_timings': context.timer.hasTickFor(
|
|
413
|
+
RESPONSE_START_LABEL
|
|
414
|
+
)
|
|
415
|
+
? createTimings(
|
|
416
|
+
context.timer.startTime,
|
|
417
|
+
context.timer.timeAt(RESPONSE_START_LABEL),
|
|
418
|
+
context.timer.stopTime
|
|
419
|
+
)
|
|
420
|
+
: null
|
|
421
|
+
},
|
|
422
|
+
context.timer.stopTime
|
|
423
|
+
);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
291
426
|
}
|