Skip to content

Commit fb1f758

Browse files
authored
feat: Add URL to http events and trace segments. (#190)
1 parent b8886f2 commit fb1f758

File tree

5 files changed

+90
-41
lines changed

5 files changed

+90
-41
lines changed

src/plugins/event-plugins/FetchPlugin.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
isUrlAllowed,
1616
createXRaySubsegment,
1717
requestInfoToHostname,
18-
addAmznTraceIdHeaderToHeaders
18+
addAmznTraceIdHeaderToHeaders,
19+
resourceToUrlString
1920
} from '../utils/http-utils';
2021
import { HTTP_EVENT_TYPE, XRAY_TRACE_EVENT_TYPE } from '../utils/constant';
2122
import {
@@ -83,7 +84,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
8384
argsArray: IArguments
8485
): XRayTraceEvent => {
8586
const startTime = epochTime();
86-
const http: Http = createXRayTraceEventHttp(init, true);
87+
const http: Http = createXRayTraceEventHttp(input, init, true);
8788
const xRayTraceEvent: XRayTraceEvent = createXRayTraceEvent(
8889
this.config.logicalServiceName,
8990
startTime
@@ -198,6 +199,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
198199
return {
199200
version: '1.0.0',
200201
request: {
202+
url: resourceToUrlString(input),
201203
method: init?.method ? init.method : 'GET'
202204
}
203205
};
@@ -240,14 +242,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
240242
const httpEvent: HttpEvent = this.createHttpEvent(input, init);
241243
let trace: XRayTraceEvent | undefined;
242244

243-
let url: string;
244-
if (typeof input === 'string') {
245-
url = input;
246-
} else {
247-
url = input.url;
248-
}
249-
250-
if (!isUrlAllowed(url, this.config)) {
245+
if (!isUrlAllowed(resourceToUrlString(input), this.config)) {
251246
return original.apply(thisArg, argsArray);
252247
}
253248

src/plugins/event-plugins/XhrPlugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
224224
if (this.config.recordAllRequests || !this.statusOk(xhr.status)) {
225225
this.context.record(HTTP_EVENT_TYPE, {
226226
version: '1.0.0',
227-
request: { method: xhrDetails.method },
227+
request: { method: xhrDetails.method, url: xhrDetails.url },
228228
response: { status: xhr.status, statusText: xhr.statusText }
229229
});
230230
}
@@ -236,7 +236,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
236236
) {
237237
const httpEvent: HttpEvent = {
238238
version: '1.0.0',
239-
request: { method: xhrDetails.method }
239+
request: { method: xhrDetails.method, url: xhrDetails.url }
240240
};
241241
httpEvent.error = errorEventToJsErrorEvent(
242242
{
@@ -267,6 +267,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
267267
{
268268
request: {
269269
method: xhrDetails.method,
270+
url: xhrDetails.url,
270271
traced: true
271272
}
272273
}

src/plugins/event-plugins/__tests__/FetchPlugin.test.ts

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { HttpEvent } from '../../../events/http-event';
2424
// Mock getRandomValues -- since it does nothing, the 'random' number will be 0.
2525
jest.mock('../../../utils/random');
2626

27+
const URL = 'https://aws.amazon.com';
28+
2729
const TRACE_ID =
2830
'Root=1-0-000000000000000000000000;Parent=0000000000000000;Sampled=1';
2931

@@ -83,7 +85,7 @@ describe('FetchPlugin tests', () => {
8385
plugin.load(xRayOffContext);
8486

8587
// Run
86-
await fetch('https://aws.amazon.com');
88+
await fetch(URL);
8789
plugin.disable();
8890

8991
// Assert
@@ -92,7 +94,8 @@ describe('FetchPlugin tests', () => {
9294
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
9395
expect(record.mock.calls[0][1]).toMatchObject({
9496
request: {
95-
method: 'GET'
97+
method: 'GET',
98+
url: URL
9699
},
97100
response: {
98101
status: 200,
@@ -113,7 +116,7 @@ describe('FetchPlugin tests', () => {
113116
plugin.load(xRayOffContext);
114117

115118
// Run
116-
await fetch('https://aws.amazon.com').catch((error) => {
119+
await fetch(URL).catch((error) => {
117120
// Expected
118121
});
119122
plugin.disable();
@@ -124,7 +127,8 @@ describe('FetchPlugin tests', () => {
124127
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
125128
expect(record.mock.calls[0][1]).toMatchObject({
126129
request: {
127-
method: 'GET'
130+
method: 'GET',
131+
url: URL
128132
},
129133
error: {
130134
version: '1.0.0',
@@ -145,7 +149,7 @@ describe('FetchPlugin tests', () => {
145149
plugin.load(xRayOffContext);
146150

147151
// Run
148-
await fetch('https://aws.amazon.com').catch((error) => {
152+
await fetch(URL).catch((error) => {
149153
// Expected
150154
});
151155
plugin.disable();
@@ -156,7 +160,8 @@ describe('FetchPlugin tests', () => {
156160
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
157161
expect(record.mock.calls[0][1]).toMatchObject({
158162
request: {
159-
method: 'GET'
163+
method: 'GET',
164+
url: URL
160165
},
161166
error: {
162167
version: '1.0.0',
@@ -178,7 +183,7 @@ describe('FetchPlugin tests', () => {
178183
plugin.load(xRayOnContext);
179184

180185
// Run
181-
await fetch('https://aws.amazon.com');
186+
await fetch(URL);
182187
plugin.disable();
183188

184189
// Assert
@@ -201,6 +206,7 @@ describe('FetchPlugin tests', () => {
201206
http: {
202207
request: {
203208
method: 'GET',
209+
url: URL,
204210
traced: true
205211
},
206212
response: { status: 200, content_length: 125 }
@@ -222,7 +228,7 @@ describe('FetchPlugin tests', () => {
222228
plugin.load(xRayOnContext);
223229

224230
// Run
225-
await fetch('https://aws.amazon.com').catch((error) => {
231+
await fetch(URL).catch((error) => {
226232
// Expected
227233
});
228234
plugin.disable();
@@ -246,6 +252,7 @@ describe('FetchPlugin tests', () => {
246252
http: {
247253
request: {
248254
method: 'GET',
255+
url: URL,
249256
traced: true
250257
}
251258
},
@@ -274,7 +281,7 @@ describe('FetchPlugin tests', () => {
274281
plugin.disable();
275282

276283
// Run
277-
await fetch('https://aws.amazon.com');
284+
await fetch(URL);
278285

279286
// Assert
280287
expect(mockFetch).toHaveBeenCalledTimes(1);
@@ -294,7 +301,7 @@ describe('FetchPlugin tests', () => {
294301
plugin.enable();
295302

296303
// Run
297-
await fetch('https://aws.amazon.com');
304+
await fetch(URL);
298305
plugin.disable();
299306

300307
// Assert
@@ -312,7 +319,7 @@ describe('FetchPlugin tests', () => {
312319
const init: RequestInit = {};
313320

314321
// Run
315-
await fetch('https://aws.amazon.com', init);
322+
await fetch(URL, init);
316323
plugin.disable();
317324

318325
// Assert
@@ -327,7 +334,7 @@ describe('FetchPlugin tests', () => {
327334
plugin.load(xRayOnContext);
328335

329336
// Run
330-
await fetch('https://aws.amazon.com');
337+
await fetch(URL);
331338
plugin.disable();
332339

333340
// Assert
@@ -346,7 +353,7 @@ describe('FetchPlugin tests', () => {
346353
const init: RequestInit = {};
347354

348355
// Run
349-
await fetch('https://aws.amazon.com', init);
356+
await fetch(URL, init);
350357
plugin.disable();
351358

352359
// Assert
@@ -364,7 +371,7 @@ describe('FetchPlugin tests', () => {
364371
plugin.load(xRayOnContext);
365372

366373
// Run
367-
await fetch('https://aws.amazon.com');
374+
await fetch(URL);
368375
plugin.disable();
369376

370377
// Assert
@@ -385,7 +392,7 @@ describe('FetchPlugin tests', () => {
385392
plugin.load(xRayOffContext);
386393

387394
// Run
388-
await fetch('https://aws.amazon.com');
395+
await fetch(URL);
389396
plugin.disable();
390397

391398
// Assert
@@ -416,7 +423,7 @@ describe('FetchPlugin tests', () => {
416423
plugin.load(xRayOnContext);
417424

418425
// Run
419-
await fetch('https://aws.amazon.com');
426+
await fetch(URL);
420427
plugin.disable();
421428

422429
// Assert
@@ -445,7 +452,7 @@ describe('FetchPlugin tests', () => {
445452
plugin.load(xRayOnContext);
446453

447454
// Run
448-
await fetch('https://aws.amazon.com');
455+
await fetch(URL);
449456
plugin.disable();
450457

451458
// Assert
@@ -465,7 +472,7 @@ describe('FetchPlugin tests', () => {
465472
plugin.load(xRayOffContext);
466473

467474
// Run
468-
await fetch('https://aws.amazon.com').catch((error) => {
475+
await fetch(URL).catch((error) => {
469476
// Expected
470477
});
471478
plugin.disable();
@@ -500,7 +507,7 @@ describe('FetchPlugin tests', () => {
500507
plugin.load(xRayOffContext);
501508

502509
// Run
503-
await fetch('https://aws.amazon.com').catch((error) => {
510+
await fetch(URL).catch((error) => {
504511
// Expected
505512
});
506513
plugin.disable();
@@ -535,7 +542,7 @@ describe('FetchPlugin tests', () => {
535542
plugin.load(xRayOffContext);
536543

537544
// Run
538-
await fetch('https://aws.amazon.com');
545+
await fetch(URL);
539546
plugin.disable();
540547

541548
// Assert
@@ -554,7 +561,7 @@ describe('FetchPlugin tests', () => {
554561
plugin.load(xRayOffContext);
555562

556563
// Run
557-
await fetch('https://aws.amazon.com');
564+
await fetch(URL);
558565
plugin.disable();
559566

560567
// Assert
@@ -574,7 +581,7 @@ describe('FetchPlugin tests', () => {
574581
plugin.load(xRayOffContext);
575582

576583
// Run
577-
await fetch('https://aws.amazon.com');
584+
await fetch(URL);
578585
plugin.disable();
579586
global.fetch = mockFetch;
580587

@@ -595,7 +602,7 @@ describe('FetchPlugin tests', () => {
595602
plugin.load(xRayOffContext);
596603

597604
// Run
598-
await fetch('https://aws.amazon.com');
605+
await fetch(URL);
599606
plugin.disable();
600607

601608
// Assert
@@ -615,7 +622,7 @@ describe('FetchPlugin tests', () => {
615622
plugin.load(xRayOffContext);
616623

617624
// Run
618-
await fetch({ url: 'https://aws.amazon.com' } as Request);
625+
await fetch({ url: URL } as Request);
619626
plugin.disable();
620627

621628
// Assert
@@ -633,7 +640,7 @@ describe('FetchPlugin tests', () => {
633640
plugin.load(xRayOffContext);
634641

635642
// Run
636-
await fetch('https://aws.amazon.com');
643+
await fetch(URL);
637644
plugin.disable();
638645

639646
// Assert
@@ -720,7 +727,7 @@ describe('FetchPlugin tests', () => {
720727
}
721728
};
722729

723-
const request: Request = new Request('https://aws.amazon.com', init);
730+
const request: Request = new Request(URL, init);
724731

725732
// Run
726733
await fetch(request);
@@ -730,4 +737,36 @@ describe('FetchPlugin tests', () => {
730737
expect(request.headers.get(X_AMZN_TRACE_ID)).toEqual(TRACE_ID);
731738
expect(request.headers.get(SIGN_HEADER_KEY)).toEqual(SIGN_HEADER_VAL);
732739
});
740+
741+
test('when fetch uses request object then the URL is added to the event', async () => {
742+
// Init
743+
const config: PartialHttpPluginConfig = {
744+
urlsToInclude: [/aws\.amazon\.com/],
745+
recordAllRequests: true
746+
};
747+
748+
const plugin: FetchPlugin = new FetchPlugin(config);
749+
plugin.load(xRayOffContext);
750+
751+
const request: Request = new Request(URL);
752+
753+
// Run
754+
await fetch(request);
755+
plugin.disable();
756+
757+
// Assert
758+
expect(mockFetch).toHaveBeenCalledTimes(1);
759+
expect(record).toHaveBeenCalledTimes(1);
760+
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
761+
expect(record.mock.calls[0][1]).toMatchObject({
762+
request: {
763+
method: 'GET',
764+
url: URL
765+
},
766+
response: {
767+
status: 200,
768+
statusText: 'OK'
769+
}
770+
});
771+
});
733772
});

src/plugins/event-plugins/__tests__/XhrPlugin.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe('XhrPlugin tests', () => {
5353
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
5454
expect(record.mock.calls[0][1]).toMatchObject({
5555
request: {
56-
method: 'GET'
56+
method: 'GET',
57+
url: './response.json'
5758
},
5859
response: {
5960
status: 200,
@@ -106,7 +107,8 @@ describe('XhrPlugin tests', () => {
106107
http: {
107108
request: {
108109
method: 'GET',
109-
traced: true
110+
traced: true,
111+
url: './response.json'
110112
},
111113
response: { status: 200, content_length: 125 }
112114
}
@@ -256,7 +258,8 @@ describe('XhrPlugin tests', () => {
256258
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
257259
expect(record.mock.calls[0][1]).toMatchObject({
258260
request: {
259-
method: 'GET'
261+
method: 'GET',
262+
url: './response.json'
260263
},
261264
error: {
262265
version: '1.0.0',

0 commit comments

Comments
 (0)