@datadog/mobile-react-native 1.0.0-rc8 → 1.0.0-rc9

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.
Files changed (108) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/lib/commonjs/DdSdkReactNative.js +2 -2
  3. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  4. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +73 -0
  5. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  6. package/lib/commonjs/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +40 -1
  7. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  8. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +46 -0
  9. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  10. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +25 -0
  11. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  12. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  13. package/lib/commonjs/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  14. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +57 -0
  15. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  16. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +49 -0
  17. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  18. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +45 -0
  19. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  20. package/lib/commonjs/rum/instrumentation/{URLHostParser.js → resourceTracking/implementation/URLHostParser.js} +16 -7
  21. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  22. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +167 -0
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js +97 -0
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  26. package/lib/commonjs/version.js +1 -1
  27. package/lib/commonjs/version.js.map +1 -1
  28. package/lib/module/DdSdkReactNative.js +1 -1
  29. package/lib/module/DdSdkReactNative.js.map +1 -1
  30. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +61 -0
  31. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -0
  32. package/lib/module/rum/instrumentation/{TraceIdentifier.js → resourceTracking/domain/distributedTracing.js} +35 -1
  33. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -0
  34. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +29 -0
  35. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -0
  36. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js +16 -0
  37. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -0
  38. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.js +2 -0
  39. package/lib/module/rum/instrumentation/{DdRumXhr.js.map → resourceTracking/domain/interfaces/RumResource.js.map} +0 -0
  40. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +50 -0
  41. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -0
  42. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js +40 -0
  43. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.js.map +1 -0
  44. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js +36 -0
  45. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.js.map +1 -0
  46. package/{src/rum/instrumentation/URLHostParser.ts → lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js} +15 -8
  47. package/lib/module/rum/instrumentation/resourceTracking/implementation/URLHostParser.js.map +1 -0
  48. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +151 -0
  49. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -0
  50. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js +86 -0
  51. package/lib/module/rum/instrumentation/resourceTracking/implementation/responseSize.js.map +1 -0
  52. package/lib/module/version.js +1 -1
  53. package/lib/module/version.js.map +1 -1
  54. package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  55. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  56. package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  57. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  58. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
  59. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  60. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  61. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  62. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  63. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  64. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  65. package/lib/typescript/version.d.ts +1 -1
  66. package/package.json +4 -3
  67. package/src/DdSdkReactNative.tsx +1 -1
  68. package/src/__tests__/DdSdkReactNative.test.tsx +11 -8
  69. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +1 -1
  70. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +1 -1
  71. package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +67 -0
  72. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +79 -0
  73. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/__tests__}/__utils__/XMLHttpRequestMock.ts +0 -0
  74. package/src/{__tests__/rum/instrumentation/TraceIdentifier.test.tsx → rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts} +1 -1
  75. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +87 -0
  76. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +45 -0
  77. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +15 -0
  78. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +26 -0
  79. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +78 -0
  80. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +83 -0
  81. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/__utils__/ResourceMockFactory.ts +45 -0
  82. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/internalDevResourceBlocklist.test.ts +69 -0
  83. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.ts +50 -0
  84. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.ts +70 -0
  85. package/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.ts +30 -0
  86. package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +197 -0
  87. package/src/{__tests__/rum/instrumentation → rum/instrumentation/resourceTracking/implementation/__tests__}/URLHostParser.test.ts +3 -3
  88. package/src/{__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx → rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts} +83 -49
  89. package/src/rum/instrumentation/resourceTracking/implementation/responseSize.ts +83 -0
  90. package/src/version.ts +1 -1
  91. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +0 -356
  92. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  93. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -2
  94. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +0 -1
  95. package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +0 -1
  96. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +0 -331
  97. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +0 -1
  98. package/lib/module/rum/instrumentation/DdRumXhr.js +0 -2
  99. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +0 -1
  100. package/lib/module/rum/instrumentation/URLHostParser.js +0 -24
  101. package/lib/module/rum/instrumentation/URLHostParser.js.map +0 -1
  102. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +0 -40
  103. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +0 -23
  104. package/lib/typescript/rum/instrumentation/TraceIdentifier.d.ts +0 -1
  105. package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +0 -6
  106. package/src/rum/instrumentation/DdRumResourceTracking.tsx +0 -426
  107. package/src/rum/instrumentation/DdRumXhr.tsx +0 -33
  108. package/src/rum/instrumentation/TraceIdentifier.tsx +0 -34
@@ -1,426 +0,0 @@
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
- import { InternalLog } from '../../InternalLog';
10
- import { SdkVerbosity } from '../../SdkVerbosity';
11
- import Timer from '../../Timer';
12
- import { DdRum } from '../../foundation';
13
-
14
- import type { DdRumResourceTracingAttributes, DdRumXhr } from './DdRumXhr';
15
- import { generateTraceId } from './TraceIdentifier';
16
- import { URLHostParser } from './URLHostParser';
17
-
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: ";
25
-
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^');
31
-
32
- interface Timing {
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;
41
- }
42
-
43
- interface ResourceTimings {
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;
51
- }
52
-
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
- };
72
- }
73
-
74
- /**
75
- * @param origin Start time (absolute) of the request
76
- * @param start Start time (absolute) of the timing
77
- * @param end End time (absolute) of the timing
78
- */
79
- function formatTiming(origin: number, start: number, end: number): Timing {
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
- };
89
- }
90
-
91
- function timeToNanos(durationMs: number): number {
92
- return +(durationMs * 1e6).toFixed(0);
93
- }
94
-
95
- function byteLength(str: string): number {
96
- // This is a weird trick, but it works.
97
- // Output is the same as TextEncoder.encode(...).length
98
- return unescape(encodeURI(str)).length;
99
- }
100
-
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;
112
- }
113
-
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
- }
159
-
160
- if (typeof size !== 'number') {
161
- return MISSING_RESOURCE_SIZE;
162
- }
163
- return size;
164
- }
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
-
206
- /**
207
- * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
208
- */
209
- export class DdRumResourceTracking {
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
- });
231
- }
232
-
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
- }
251
-
252
- DdRumResourceTracking.tracingSamplingRate = tracingSamplingRate;
253
- DdRumResourceTracking.firstPartyHostsRegex = getFirstPartyHostsRegex(
254
- firstPartyHosts
255
- );
256
-
257
- DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
258
- DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
259
-
260
- DdRumResourceTracking.proxyXhr(xhrType);
261
- InternalLog.log(
262
- 'Datadog SDK is tracking XHR resources',
263
- SdkVerbosity.INFO
264
- );
265
- }
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;
274
- }
275
- }
276
-
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
- }
301
-
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
- }
307
-
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
- }
330
-
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
- }
362
-
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
- }
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
- }
426
- }
@@ -1,33 +0,0 @@
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 type Timer from '../../Timer';
8
-
9
- export interface DdRumXhr extends XMLHttpRequest {
10
- _datadog_xhr: DdRumXhrContext;
11
- }
12
-
13
- export interface DdRumXhrContext {
14
- method: string;
15
- url: string;
16
- reported: boolean;
17
- timer: Timer;
18
- tracingAttributes: DdRumResourceTracingAttributes;
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
- };
@@ -1,34 +0,0 @@
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
- /*
8
- * This code was inspired from browser-sdk at (https://github.com/DataDog/browser-sdk/blob/master/packages/rum-core/src/domain/tracing/tracer.ts#L107)
9
- */
10
-
11
- export function generateTraceId(): string {
12
- const radix = 10;
13
- const MAX_32_BITS_NUMBER = 4294967295; // 2^32-1
14
- let low: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
15
- let high: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
16
- let str = '';
17
-
18
- while (high > 0 && low > 0) {
19
- // Create an intermediate value with the same modulo as the combined high and low value
20
- // but requiring 36 bits max (32 for the low value + 4 for the high part)
21
- const modH = high % radix;
22
- const temp = (modH << 32) + low;
23
- const digit = temp % radix;
24
-
25
- // update the high value
26
- high = (high - modH) / radix; // we reuse the modH to avoid the need of a floor op
27
- // the low value reuses the previous temp value to account for the "missing mod" in the high update
28
- low = (temp - digit) / radix; // we reuse the digit to avoid the need of a floor op
29
-
30
- // update the string from right to left
31
- str = digit.toString() + str;
32
- }
33
- return str;
34
- }