@datadog/mobile-react-native 1.0.0-rc3 → 1.0.0-rc6
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 +2 -3
- package/android/gradle.properties +4 -4
- package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
- package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
- package/ios/Sources/DdRum.m +10 -9
- package/ios/Sources/DdRum.swift +24 -24
- package/ios/Sources/DdTrace.m +2 -2
- package/ios/Sources/DdTrace.swift +6 -6
- package/lib/commonjs/DdSdkReactNative.js +42 -22
- 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 +51 -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 +62 -40
- package/lib/commonjs/foundation.js.map +1 -1
- package/lib/commonjs/index.js +26 -18
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal.js +14 -0
- package/lib/commonjs/internal.js.map +1 -0
- 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 +51 -20
- package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +141 -30
- 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 +10 -0
- package/lib/commonjs/version.js.map +1 -0
- package/lib/module/DdSdkReactNative.js +34 -22
- 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 +46 -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 +42 -40
- package/lib/module/foundation.js.map +1 -1
- package/lib/module/index.js +3 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal.js +2 -0
- package/lib/module/internal.js.map +1 -0
- 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 +46 -20
- package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js +133 -29
- 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 +3 -0
- package/lib/module/version.js.map +1 -0
- package/lib/typescript/DdSdkReactNative.d.ts +7 -6
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +15 -4
- package/lib/typescript/InternalLog.d.ts +6 -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 +3 -2
- package/lib/typescript/internal.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +3 -2
- package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +13 -8
- 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/types.d.ts +12 -11
- package/lib/typescript/version.d.ts +1 -0
- package/package.json +7 -4
- package/src/DdSdkReactNative.tsx +112 -58
- package/src/DdSdkReactNativeConfiguration.tsx +22 -14
- package/src/InternalLog.tsx +45 -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 +98 -0
- package/src/__tests__/DdSdkReactNative.test.tsx +699 -500
- 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 +243 -76
- package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +723 -403
- 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 +55 -42
- package/src/index.tsx +13 -3
- package/src/internal.tsx +1 -0
- package/src/rum/instrumentation/DdEventsInterceptor.tsx +51 -35
- package/src/rum/instrumentation/DdRumErrorTracking.tsx +75 -42
- package/src/rum/instrumentation/DdRumResourceTracking.tsx +312 -166
- 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 -157
- package/src/version.ts +2 -0
- 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,52 +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
|
|
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: ";
|
|
17
24
|
|
|
18
|
-
const RESPONSE_START_LABEL =
|
|
25
|
+
const RESPONSE_START_LABEL = 'response_start';
|
|
26
|
+
const MISSING_RESOURCE_SIZE = -1;
|
|
19
27
|
|
|
20
28
|
interface Timing {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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;
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
interface ResourceTimings {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
39
47
|
}
|
|
40
48
|
|
|
41
|
-
function createTimings(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
};
|
|
54
68
|
}
|
|
55
69
|
|
|
56
70
|
/**
|
|
@@ -59,157 +73,289 @@ function createTimings(startTime: number,
|
|
|
59
73
|
* @param end End time (absolute) of the timing
|
|
60
74
|
*/
|
|
61
75
|
function formatTiming(origin: number, start: number, end: number): Timing {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
};
|
|
68
85
|
}
|
|
69
86
|
|
|
70
87
|
function timeToNanos(durationMs: number): number {
|
|
71
|
-
|
|
88
|
+
return +(durationMs * 1e6).toFixed(0);
|
|
72
89
|
}
|
|
73
90
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
91
|
+
function byteLength(str: string): number {
|
|
92
|
+
// This is a weird trick, but it works.
|
|
93
|
+
// Output is the same as TextEncoder.encode(...).length
|
|
94
|
+
return unescape(encodeURI(str)).length;
|
|
95
|
+
}
|
|
78
96
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
|
|
93
|
-
*/
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
95
|
-
static startTrackingInternal(xhrType: any): void {
|
|
96
|
-
// extra safety to avoid proxying the XHR class twice
|
|
97
|
-
if (DdRumResourceTracking.isTracking) {
|
|
98
|
-
return
|
|
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;
|
|
99
106
|
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
100
109
|
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
export function calculateResponseSize(xhr: XMLHttpRequest): number {
|
|
111
|
+
const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
|
|
112
|
+
if (contentLengthHeader != null) {
|
|
113
|
+
return contentLengthHeader as number;
|
|
114
|
+
}
|
|
103
115
|
|
|
104
|
-
|
|
105
|
-
|
|
116
|
+
const response = xhr.response;
|
|
117
|
+
if (!response) {
|
|
118
|
+
return MISSING_RESOURCE_SIZE;
|
|
119
|
+
}
|
|
106
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
|
+
}
|
|
107
155
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
DdRumResourceTracking.isTracking = false;
|
|
111
|
-
XMLHttpRequest.prototype.open = DdRumResourceTracking.originalXhrOpen;
|
|
112
|
-
XMLHttpRequest.prototype.send = DdRumResourceTracking.originalXhrSend;
|
|
156
|
+
if (typeof size !== 'number') {
|
|
157
|
+
return MISSING_RESOURCE_SIZE;
|
|
113
158
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const spanId = generateTraceId()
|
|
128
|
-
const traceId = generateTraceId()
|
|
129
|
-
this._datadog_xhr = {
|
|
130
|
-
method,
|
|
131
|
-
url: url,
|
|
132
|
-
reported: false,
|
|
133
|
-
timer: new Timer(),
|
|
134
|
-
spanId: spanId,
|
|
135
|
-
traceId: traceId
|
|
136
|
-
}
|
|
137
|
-
// eslint-disable-next-line prefer-rest-params
|
|
138
|
-
return originalXhrOpen.apply(this, arguments as any)
|
|
159
|
+
return size;
|
|
160
|
+
}
|
|
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
|
+
};
|
|
139
172
|
}
|
|
140
|
-
|
|
173
|
+
return {
|
|
174
|
+
samplingPriorityHeader: '0',
|
|
175
|
+
tracingStrategy: 'DISCARD'
|
|
176
|
+
};
|
|
177
|
+
};
|
|
141
178
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
|
|
181
|
+
*/
|
|
182
|
+
export class DdRumResourceTracking {
|
|
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
|
+
);
|
|
197
|
+
}
|
|
145
198
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
}
|
|
152
214
|
|
|
153
|
-
|
|
215
|
+
DdRumResourceTracking.tracingSamplingRate = tracingSamplingRate;
|
|
154
216
|
|
|
155
|
-
|
|
217
|
+
DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
|
|
218
|
+
DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
|
|
156
219
|
|
|
157
|
-
|
|
158
|
-
|
|
220
|
+
DdRumResourceTracking.proxyXhr(xhrType);
|
|
221
|
+
InternalLog.log(
|
|
222
|
+
'Datadog SDK is tracking XHR resources',
|
|
223
|
+
SdkVerbosity.INFO
|
|
224
|
+
);
|
|
159
225
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
xhrProxy._datadog_xhr.reported = true;
|
|
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;
|
|
169
234
|
}
|
|
170
|
-
} else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
|
|
171
|
-
xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (originalOnreadystatechange) {
|
|
175
|
-
// eslint-disable-next-line prefer-rest-params
|
|
176
|
-
originalOnreadystatechange.apply(xhrProxy, arguments as any)
|
|
177
|
-
}
|
|
178
235
|
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private static reportXhr(xhrProxy: DdRumXhr): void {
|
|
182
|
-
|
|
183
|
-
const context = xhrProxy._datadog_xhr
|
|
184
|
-
|
|
185
|
-
const key = context.timer.startTime + "/" + context.method
|
|
186
|
-
|
|
187
|
-
context.timer.stop()
|
|
188
|
-
|
|
189
|
-
DdRum.startResource(
|
|
190
|
-
key,
|
|
191
|
-
context.method,
|
|
192
|
-
context.url,
|
|
193
|
-
{
|
|
194
|
-
"_dd.span_id": context.spanId,
|
|
195
|
-
"_dd.trace_id": context.traceId
|
|
196
|
-
},
|
|
197
|
-
context.timer.startTime
|
|
198
|
-
).then(() => {
|
|
199
|
-
DdRum.stopResource(
|
|
200
|
-
key,
|
|
201
|
-
xhrProxy.status,
|
|
202
|
-
"xhr",
|
|
203
|
-
{
|
|
204
|
-
"_dd.resource_timings": context.timer.hasTickFor(RESPONSE_START_LABEL) ?
|
|
205
|
-
createTimings(
|
|
206
|
-
context.timer.startTime,
|
|
207
|
-
context.timer.timeAt(RESPONSE_START_LABEL),
|
|
208
|
-
context.timer.stopTime
|
|
209
|
-
) : null
|
|
210
|
-
},
|
|
211
|
-
context.timer.stopTime);
|
|
212
|
-
})
|
|
213
|
-
}
|
|
214
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);
|
|
241
|
+
}
|
|
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
|
+
};
|
|
296
|
+
}
|
|
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
|
+
}
|
|
215
361
|
}
|
|
@@ -4,22 +4,23 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import React from 'react'
|
|
8
|
-
import { DdEventsInterceptor } from './DdEventsInterceptor'
|
|
9
|
-
import NoOpEventsInterceptor from './NoOpEventsInterceptor'
|
|
10
|
-
import type EventsInterceptor from './EventsInterceptor'
|
|
7
|
+
import React from 'react';
|
|
11
8
|
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../SdkVerbosity';
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
import { DdEventsInterceptor } from './DdEventsInterceptor';
|
|
13
|
+
import type EventsInterceptor from './EventsInterceptor';
|
|
14
|
+
import NoOpEventsInterceptor from './NoOpEventsInterceptor';
|
|
15
|
+
import { areObjectShallowEqual } from './ShallowObjectEqualityChecker';
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
|
-
* Provides RUM auto-instrumentation feature to track user interaction as RUM events.
|
|
17
|
-
* For now we are only covering the "onPress" events.
|
|
18
|
-
*/
|
|
18
|
+
* Provides RUM auto-instrumentation feature to track user interaction as RUM events.
|
|
19
|
+
* For now we are only covering the "onPress" events.
|
|
20
|
+
*/
|
|
19
21
|
export class DdRumUserInteractionTracking {
|
|
20
|
-
|
|
21
|
-
private static
|
|
22
|
-
private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor()
|
|
22
|
+
private static isTracking = false;
|
|
23
|
+
private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor();
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Starts tracking user interactions and sends a RUM Action event every time a new interaction was detected.
|
|
@@ -29,23 +30,69 @@ export class DdRumUserInteractionTracking {
|
|
|
29
30
|
static startTracking(): void {
|
|
30
31
|
// extra safety to avoid wrapping more than 1 time this function
|
|
31
32
|
if (DdRumUserInteractionTracking.isTracking) {
|
|
32
|
-
|
|
33
|
+
InternalLog.log(
|
|
34
|
+
'Datadog SDK is already tracking interactions',
|
|
35
|
+
SdkVerbosity.WARN
|
|
36
|
+
);
|
|
37
|
+
return;
|
|
33
38
|
}
|
|
34
|
-
DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor()
|
|
35
|
-
const original = React.createElement
|
|
36
|
-
React.createElement = (
|
|
39
|
+
DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor();
|
|
40
|
+
const original = React.createElement;
|
|
41
|
+
React.createElement = (
|
|
42
|
+
element: any,
|
|
43
|
+
props: any,
|
|
44
|
+
...children: any
|
|
45
|
+
): any => {
|
|
37
46
|
// check if we have an 'onPress' property and that this is really a function
|
|
38
|
-
if (props && typeof props.onPress ===
|
|
39
|
-
const originalOnPress = props.onPress
|
|
47
|
+
if (props && typeof props.onPress === 'function') {
|
|
48
|
+
const originalOnPress = props.onPress;
|
|
40
49
|
props.onPress = (...args: any[]) => {
|
|
41
|
-
DdRumUserInteractionTracking.eventsInterceptor.interceptOnPress(
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
DdRumUserInteractionTracking.eventsInterceptor.interceptOnPress(
|
|
51
|
+
...args
|
|
52
|
+
);
|
|
53
|
+
return originalOnPress(...args);
|
|
54
|
+
};
|
|
55
|
+
// we store the original onPress prop so we can keep memoization working
|
|
56
|
+
props.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__ = originalOnPress;
|
|
44
57
|
}
|
|
45
|
-
return original(element, props, ...children)
|
|
46
|
-
}
|
|
47
|
-
DdRumUserInteractionTracking.isTracking = true
|
|
48
|
-
}
|
|
58
|
+
return original(element, props, ...children);
|
|
59
|
+
};
|
|
49
60
|
|
|
50
|
-
|
|
61
|
+
const originalMemo = React.memo;
|
|
62
|
+
React.memo = (
|
|
63
|
+
component: any,
|
|
64
|
+
propsAreEqual?: (prevProps: any, newProps: any) => boolean
|
|
65
|
+
) => {
|
|
66
|
+
return originalMemo(component, (prev, next) => {
|
|
67
|
+
if (!next.onPress || !prev.onPress) {
|
|
68
|
+
return propsAreEqual
|
|
69
|
+
? propsAreEqual(prev, next)
|
|
70
|
+
: areObjectShallowEqual(prev, next);
|
|
71
|
+
}
|
|
72
|
+
// we replace "our" onPress from the props by the original for comparison
|
|
73
|
+
const { onPress: _prevOnPress, ...partialPrevProps } = prev;
|
|
74
|
+
const prevProps = {
|
|
75
|
+
...partialPrevProps,
|
|
76
|
+
onPress: prev.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const { onPress: _nextOnPress, ...partialNextProps } = next;
|
|
80
|
+
const nextProps = {
|
|
81
|
+
...partialNextProps,
|
|
82
|
+
onPress: next.__DATADOG_INTERNAL_ORIGINAL_ON_PRESS__
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// if no comparison function is provided we do shallow comparison
|
|
86
|
+
return propsAreEqual
|
|
87
|
+
? propsAreEqual(prevProps, nextProps)
|
|
88
|
+
: areObjectShallowEqual(nextProps, prevProps);
|
|
89
|
+
});
|
|
90
|
+
};
|
|
51
91
|
|
|
92
|
+
DdRumUserInteractionTracking.isTracking = true;
|
|
93
|
+
InternalLog.log(
|
|
94
|
+
'Datadog SDK is tracking interactions',
|
|
95
|
+
SdkVerbosity.INFO
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -4,20 +4,30 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type Timer from
|
|
8
|
-
|
|
7
|
+
import type Timer from '../../Timer';
|
|
9
8
|
|
|
10
9
|
export interface DdRumXhr extends XMLHttpRequest {
|
|
11
|
-
|
|
12
|
-
_datadog_xhr: DdRumXhrContext
|
|
13
|
-
|
|
10
|
+
_datadog_xhr: DdRumXhrContext;
|
|
14
11
|
}
|
|
15
12
|
|
|
16
13
|
export interface DdRumXhrContext {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
traceId: string
|
|
14
|
+
method: string;
|
|
15
|
+
url: string;
|
|
16
|
+
reported: boolean;
|
|
17
|
+
timer: Timer;
|
|
18
|
+
tracingAttributes: DdRumResourceTracingAttributes;
|
|
23
19
|
}
|
|
20
|
+
|
|
21
|
+
export type DdRumResourceTracingAttributes =
|
|
22
|
+
| {
|
|
23
|
+
tracingStrategy: 'KEEP';
|
|
24
|
+
traceId: string;
|
|
25
|
+
spanId: string;
|
|
26
|
+
samplingPriorityHeader: '1';
|
|
27
|
+
}
|
|
28
|
+
| {
|
|
29
|
+
tracingStrategy: 'DISCARD';
|
|
30
|
+
traceId?: void;
|
|
31
|
+
spanId?: void;
|
|
32
|
+
samplingPriorityHeader: '0';
|
|
33
|
+
};
|