@datadog/mobile-react-native 1.0.0-rc7 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DatadogSDKReactNative.podspec +1 -1
- package/README.md +2 -0
- package/android/build.gradle +1 -1
- package/datadog-sourcemaps.gradle +137 -0
- package/ios/Sources/DdSdk.swift +6 -3
- package/lib/commonjs/DdSdkReactNative.js +7 -3
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js +2 -0
- package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +51 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
- package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js +42 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +6 -2
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/DdSdkReactNativeConfiguration.js +2 -0
- package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
- package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
- package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
- package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +34 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
- package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js +30 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
- package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +6 -4
- package/src/DdSdkReactNative.tsx +8 -4
- package/src/DdSdkReactNativeConfiguration.tsx +1 -0
- package/src/__tests__/DdSdkReactNative.test.tsx +42 -24
- package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +1 -1
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
- package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
- package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
- package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
- package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
- package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
- package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
- package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +52 -0
- package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
- package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +26 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
- package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
- package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
- package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
- package/src/rum/instrumentation/resourceTracking/implementation/__tests__/URLHostParser.test.ts +40 -0
- package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +178 -69
- package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
- package/src/version.ts +1 -1
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -298
- package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
- package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
- package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -276
- package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
- package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
- package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
- package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -31
- package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
- package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
- package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -361
- package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
- package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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 { Platform } from 'react-native';
|
|
8
|
+
|
|
9
|
+
interface Timing {
|
|
10
|
+
/**
|
|
11
|
+
* Time relative (absolute in case of iOS) to some point, in ns.
|
|
12
|
+
*/
|
|
13
|
+
startTime: number;
|
|
14
|
+
/**
|
|
15
|
+
* Duration in ns.
|
|
16
|
+
*/
|
|
17
|
+
duration: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ResourceTimings {
|
|
21
|
+
// unlike in Performance API it is not the time until request
|
|
22
|
+
// starts (requestStart, before it can be connect, SSL, DNS),
|
|
23
|
+
// but the time until the response is first seen
|
|
24
|
+
firstByte: Timing;
|
|
25
|
+
download: Timing;
|
|
26
|
+
// required by iOS, total timing from the beginning to the end
|
|
27
|
+
fetch: Timing;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createTimings(
|
|
31
|
+
startTime: number,
|
|
32
|
+
responseStartTime: number,
|
|
33
|
+
responseEndTime: number
|
|
34
|
+
): ResourceTimings {
|
|
35
|
+
const firstByte = formatTiming(startTime, startTime, responseStartTime);
|
|
36
|
+
const download = formatTiming(
|
|
37
|
+
startTime,
|
|
38
|
+
responseStartTime,
|
|
39
|
+
responseEndTime
|
|
40
|
+
);
|
|
41
|
+
// needed for iOS, simply total duration from start to end
|
|
42
|
+
const fetch = formatTiming(startTime, startTime, responseEndTime);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
firstByte,
|
|
46
|
+
download,
|
|
47
|
+
fetch
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param origin Start time (absolute) of the request
|
|
53
|
+
* @param start Start time (absolute) of the timing
|
|
54
|
+
* @param end End time (absolute) of the timing
|
|
55
|
+
*/
|
|
56
|
+
function formatTiming(origin: number, start: number, end: number): Timing {
|
|
57
|
+
return {
|
|
58
|
+
duration: timeToNanos(end - start),
|
|
59
|
+
// if it is Android, startTime should be relative to the origin,
|
|
60
|
+
// if it is iOS - absolute (unix timestamp)
|
|
61
|
+
startTime:
|
|
62
|
+
Platform.OS === 'ios'
|
|
63
|
+
? timeToNanos(start)
|
|
64
|
+
: timeToNanos(start - origin)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function timeToNanos(durationMs: number): number {
|
|
69
|
+
return +(durationMs * 1e6).toFixed(0);
|
|
70
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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 { InternalLog } from '../../../../InternalLog';
|
|
8
|
+
import { SdkVerbosity } from '../../../../SdkVerbosity';
|
|
9
|
+
import type { Hostname } from '../domain/firstPartyHosts';
|
|
10
|
+
|
|
11
|
+
// matches what is between the first "://" and the next "/", ":" or whitespace
|
|
12
|
+
const hostRegex = '^.+://([^:/\\s]+)';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns the host from an URL.
|
|
16
|
+
* @returns the host (without the port) or null if could not cast the URL as host
|
|
17
|
+
*/
|
|
18
|
+
export const URLHostParser = (url: string): Hostname | null => {
|
|
19
|
+
try {
|
|
20
|
+
const matchedHost = url.match(hostRegex);
|
|
21
|
+
if (matchedHost === null) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
// [0] is the input, [1] is the captured group
|
|
25
|
+
return matchedHost[1] as Hostname;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
InternalLog.log(`Impossible to cast ${url} as URL`, SdkVerbosity.WARN);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
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 Timer from '../../../../Timer';
|
|
8
|
+
import type { DdRumResourceTracingAttributes } from '../domain/distributedTracing';
|
|
9
|
+
import { getTracingAttributes } from '../domain/distributedTracing';
|
|
10
|
+
import type { RequestProxyOptions } from '../domain/interfaces/RequestProxy';
|
|
11
|
+
import { RequestProxy } from '../domain/interfaces/RequestProxy';
|
|
12
|
+
|
|
13
|
+
import type { ResourceReporter } from './DatadogRumResource/ResourceReporter';
|
|
14
|
+
import { URLHostParser } from './URLHostParser';
|
|
15
|
+
import { calculateResponseSize } from './responseSize';
|
|
16
|
+
|
|
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
|
+
const RESPONSE_START_LABEL = 'response_start';
|
|
24
|
+
|
|
25
|
+
interface DdRumXhr extends XMLHttpRequest {
|
|
26
|
+
_datadog_xhr: DdRumXhrContext;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface DdRumXhrContext {
|
|
30
|
+
method: string;
|
|
31
|
+
url: string;
|
|
32
|
+
reported: boolean;
|
|
33
|
+
timer: Timer;
|
|
34
|
+
tracingAttributes: DdRumResourceTracingAttributes;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface XHRProxyProviders {
|
|
38
|
+
xhrType: typeof XMLHttpRequest;
|
|
39
|
+
resourceReporter: ResourceReporter;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Proxies XMLHTTPRequest to track resources.
|
|
44
|
+
*/
|
|
45
|
+
export class XHRProxy extends RequestProxy {
|
|
46
|
+
private providers: XHRProxyProviders;
|
|
47
|
+
private static originalXhrOpen: typeof XMLHttpRequest.prototype.open;
|
|
48
|
+
private static originalXhrSend: typeof XMLHttpRequest.prototype.send;
|
|
49
|
+
|
|
50
|
+
constructor(providers: XHRProxyProviders) {
|
|
51
|
+
super();
|
|
52
|
+
this.providers = providers;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
onTrackingStart = (context: RequestProxyOptions) => {
|
|
56
|
+
XHRProxy.originalXhrOpen = this.providers.xhrType.prototype.open;
|
|
57
|
+
XHRProxy.originalXhrSend = this.providers.xhrType.prototype.send;
|
|
58
|
+
proxyRequests(this.providers, context);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
onTrackingStop = () => {
|
|
62
|
+
this.providers.xhrType.prototype.open = XHRProxy.originalXhrOpen;
|
|
63
|
+
this.providers.xhrType.prototype.send = XHRProxy.originalXhrSend;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const proxyRequests = (
|
|
68
|
+
providers: XHRProxyProviders,
|
|
69
|
+
context: RequestProxyOptions
|
|
70
|
+
): void => {
|
|
71
|
+
proxyOpen(providers, context);
|
|
72
|
+
proxySend(providers);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const proxyOpen = (
|
|
76
|
+
{ xhrType }: XHRProxyProviders,
|
|
77
|
+
context: RequestProxyOptions
|
|
78
|
+
): void => {
|
|
79
|
+
const originalXhrOpen = xhrType.prototype.open;
|
|
80
|
+
const firstPartyHostsRegex = context.firstPartyHostsRegex;
|
|
81
|
+
const tracingSamplingRate = context.tracingSamplingRate;
|
|
82
|
+
|
|
83
|
+
xhrType.prototype.open = function (
|
|
84
|
+
this: DdRumXhr,
|
|
85
|
+
method: string,
|
|
86
|
+
url: string
|
|
87
|
+
) {
|
|
88
|
+
const hostname = URLHostParser(url);
|
|
89
|
+
// Keep track of the method and url
|
|
90
|
+
// start time is tracked by the `send` method
|
|
91
|
+
this._datadog_xhr = {
|
|
92
|
+
method,
|
|
93
|
+
url,
|
|
94
|
+
reported: false,
|
|
95
|
+
timer: new Timer(),
|
|
96
|
+
tracingAttributes: getTracingAttributes({
|
|
97
|
+
hostname,
|
|
98
|
+
firstPartyHostsRegex,
|
|
99
|
+
tracingSamplingRate
|
|
100
|
+
})
|
|
101
|
+
};
|
|
102
|
+
// eslint-disable-next-line prefer-rest-params
|
|
103
|
+
return originalXhrOpen.apply(this, arguments as any);
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const proxySend = (providers: XHRProxyProviders): void => {
|
|
108
|
+
const xhrType = providers.xhrType;
|
|
109
|
+
const originalXhrSend = xhrType.prototype.send;
|
|
110
|
+
|
|
111
|
+
xhrType.prototype.send = function (this: DdRumXhr) {
|
|
112
|
+
if (this._datadog_xhr) {
|
|
113
|
+
// keep track of start time
|
|
114
|
+
this._datadog_xhr.timer.start();
|
|
115
|
+
this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM);
|
|
116
|
+
|
|
117
|
+
const tracingAttributes = this._datadog_xhr.tracingAttributes;
|
|
118
|
+
this.setRequestHeader(
|
|
119
|
+
SAMPLING_PRIORITY_HEADER_KEY,
|
|
120
|
+
tracingAttributes.samplingPriorityHeader
|
|
121
|
+
);
|
|
122
|
+
if (tracingAttributes.tracingStrategy !== 'DISCARD') {
|
|
123
|
+
this.setRequestHeader(
|
|
124
|
+
TRACE_ID_HEADER_KEY,
|
|
125
|
+
tracingAttributes.traceId
|
|
126
|
+
);
|
|
127
|
+
this.setRequestHeader(
|
|
128
|
+
PARENT_ID_HEADER_KEY,
|
|
129
|
+
tracingAttributes.spanId
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
proxyOnReadyStateChange(this, providers);
|
|
135
|
+
|
|
136
|
+
// eslint-disable-next-line prefer-rest-params
|
|
137
|
+
return originalXhrSend.apply(this, arguments as any);
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const proxyOnReadyStateChange = (
|
|
142
|
+
xhrProxy: DdRumXhr,
|
|
143
|
+
providers: XHRProxyProviders
|
|
144
|
+
): void => {
|
|
145
|
+
const xhrType = providers.xhrType;
|
|
146
|
+
const originalOnreadystatechange = xhrProxy.onreadystatechange;
|
|
147
|
+
|
|
148
|
+
xhrProxy.onreadystatechange = function () {
|
|
149
|
+
if (xhrProxy.readyState === xhrType.DONE) {
|
|
150
|
+
if (!xhrProxy._datadog_xhr.reported) {
|
|
151
|
+
reportXhr(xhrProxy, providers.resourceReporter);
|
|
152
|
+
xhrProxy._datadog_xhr.reported = true;
|
|
153
|
+
}
|
|
154
|
+
} else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
|
|
155
|
+
xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (originalOnreadystatechange) {
|
|
159
|
+
// eslint-disable-next-line prefer-rest-params
|
|
160
|
+
originalOnreadystatechange.apply(xhrProxy, arguments as any);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const reportXhr = async (
|
|
166
|
+
xhrProxy: DdRumXhr,
|
|
167
|
+
resourceReporter: ResourceReporter
|
|
168
|
+
): Promise<void> => {
|
|
169
|
+
const responseSize = calculateResponseSize(xhrProxy);
|
|
170
|
+
|
|
171
|
+
const context = xhrProxy._datadog_xhr;
|
|
172
|
+
|
|
173
|
+
const key = `${context.timer.startTime}/${context.method}`;
|
|
174
|
+
|
|
175
|
+
context.timer.stop();
|
|
176
|
+
|
|
177
|
+
resourceReporter.reportResource({
|
|
178
|
+
key,
|
|
179
|
+
request: {
|
|
180
|
+
method: context.method,
|
|
181
|
+
url: context.url,
|
|
182
|
+
kind: 'xhr'
|
|
183
|
+
},
|
|
184
|
+
tracingAttributes: context.tracingAttributes,
|
|
185
|
+
response: {
|
|
186
|
+
statusCode: xhrProxy.status,
|
|
187
|
+
size: responseSize
|
|
188
|
+
},
|
|
189
|
+
timings: {
|
|
190
|
+
startTime: context.timer.startTime,
|
|
191
|
+
stopTime: context.timer.stopTime,
|
|
192
|
+
responseStartTime: context.timer.hasTickFor(RESPONSE_START_LABEL)
|
|
193
|
+
? context.timer.timeAt(RESPONSE_START_LABEL)
|
|
194
|
+
: undefined
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
package/src/rum/instrumentation/resourceTracking/implementation/__tests__/URLHostParser.test.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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 { URLHostParser } from '../URLHostParser';
|
|
8
|
+
|
|
9
|
+
describe('URLHostParser', () => {
|
|
10
|
+
it('returns the host when a classic URL is provided', () => {
|
|
11
|
+
const url = 'https://api.example.com/api.json';
|
|
12
|
+
expect(URLHostParser(url)).toBe('api.example.com');
|
|
13
|
+
});
|
|
14
|
+
it('returns the host when a port is provided', () => {
|
|
15
|
+
const url = 'https://api.example.com:443/api.json';
|
|
16
|
+
expect(URLHostParser(url)).toBe('api.example.com');
|
|
17
|
+
});
|
|
18
|
+
it('returns the host when no slash is present', () => {
|
|
19
|
+
const url = 'https://api.example.com';
|
|
20
|
+
expect(URLHostParser(url)).toBe('api.example.com');
|
|
21
|
+
});
|
|
22
|
+
it('strips whitespaces from host when there is a whitespace', () => {
|
|
23
|
+
const url = 'https://api.example.com ';
|
|
24
|
+
expect(URLHostParser(url)).toBe('api.example.com');
|
|
25
|
+
});
|
|
26
|
+
it('returns the host when a URL contains non ASCII characters', () => {
|
|
27
|
+
const chineseURL = 'https://api.example.中国/api.json';
|
|
28
|
+
expect(URLHostParser(chineseURL)).toBe('api.example.中国');
|
|
29
|
+
|
|
30
|
+
const russianURL = 'https://api.example.рф/api.json';
|
|
31
|
+
expect(URLHostParser(russianURL)).toBe('api.example.рф');
|
|
32
|
+
|
|
33
|
+
const egyptianURL = 'https://api.example.مصر./api.json';
|
|
34
|
+
expect(URLHostParser(egyptianURL)).toBe('api.example.مصر.');
|
|
35
|
+
});
|
|
36
|
+
it('returns null if not given an URL', () => {
|
|
37
|
+
const url = 'crash';
|
|
38
|
+
expect(URLHostParser(url)).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
});
|