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