Skip to content

Commit

Permalink
feat: Add URL to http events and trace segments. (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
qhanam committed Aug 3, 2022
1 parent b8886f2 commit fb1f758
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 41 deletions.
15 changes: 5 additions & 10 deletions src/plugins/event-plugins/FetchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
isUrlAllowed,
createXRaySubsegment,
requestInfoToHostname,
addAmznTraceIdHeaderToHeaders
addAmznTraceIdHeaderToHeaders,
resourceToUrlString
} from '../utils/http-utils';
import { HTTP_EVENT_TYPE, XRAY_TRACE_EVENT_TYPE } from '../utils/constant';
import {
Expand Down Expand Up @@ -83,7 +84,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
argsArray: IArguments
): XRayTraceEvent => {
const startTime = epochTime();
const http: Http = createXRayTraceEventHttp(init, true);
const http: Http = createXRayTraceEventHttp(input, init, true);
const xRayTraceEvent: XRayTraceEvent = createXRayTraceEvent(
this.config.logicalServiceName,
startTime
Expand Down Expand Up @@ -198,6 +199,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
return {
version: '1.0.0',
request: {
url: resourceToUrlString(input),
method: init?.method ? init.method : 'GET'
}
};
Expand Down Expand Up @@ -240,14 +242,7 @@ export class FetchPlugin extends MonkeyPatched<Window, 'fetch'> {
const httpEvent: HttpEvent = this.createHttpEvent(input, init);
let trace: XRayTraceEvent | undefined;

let url: string;
if (typeof input === 'string') {
url = input;
} else {
url = input.url;
}

if (!isUrlAllowed(url, this.config)) {
if (!isUrlAllowed(resourceToUrlString(input), this.config)) {
return original.apply(thisArg, argsArray);
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/event-plugins/XhrPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
if (this.config.recordAllRequests || !this.statusOk(xhr.status)) {
this.context.record(HTTP_EVENT_TYPE, {
version: '1.0.0',
request: { method: xhrDetails.method },
request: { method: xhrDetails.method, url: xhrDetails.url },
response: { status: xhr.status, statusText: xhr.statusText }
});
}
Expand All @@ -236,7 +236,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
) {
const httpEvent: HttpEvent = {
version: '1.0.0',
request: { method: xhrDetails.method }
request: { method: xhrDetails.method, url: xhrDetails.url }
};
httpEvent.error = errorEventToJsErrorEvent(
{
Expand Down Expand Up @@ -267,6 +267,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
{
request: {
method: xhrDetails.method,
url: xhrDetails.url,
traced: true
}
}
Expand Down
91 changes: 65 additions & 26 deletions src/plugins/event-plugins/__tests__/FetchPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { HttpEvent } from '../../../events/http-event';
// Mock getRandomValues -- since it does nothing, the 'random' number will be 0.
jest.mock('../../../utils/random');

const URL = 'https://aws.amazon.com';

const TRACE_ID =
'Root=1-0-000000000000000000000000;Parent=0000000000000000;Sampled=1';

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

// Assert
Expand All @@ -92,7 +94,8 @@ describe('FetchPlugin tests', () => {
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET'
method: 'GET',
url: URL
},
response: {
status: 200,
Expand All @@ -113,7 +116,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOffContext);

// Run
await fetch('https://aws.amazon.com').catch((error) => {
await fetch(URL).catch((error) => {
// Expected
});
plugin.disable();
Expand All @@ -124,7 +127,8 @@ describe('FetchPlugin tests', () => {
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET'
method: 'GET',
url: URL
},
error: {
version: '1.0.0',
Expand All @@ -145,7 +149,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOffContext);

// Run
await fetch('https://aws.amazon.com').catch((error) => {
await fetch(URL).catch((error) => {
// Expected
});
plugin.disable();
Expand All @@ -156,7 +160,8 @@ describe('FetchPlugin tests', () => {
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET'
method: 'GET',
url: URL
},
error: {
version: '1.0.0',
Expand All @@ -178,7 +183,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOnContext);

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

// Assert
Expand All @@ -201,6 +206,7 @@ describe('FetchPlugin tests', () => {
http: {
request: {
method: 'GET',
url: URL,
traced: true
},
response: { status: 200, content_length: 125 }
Expand All @@ -222,7 +228,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOnContext);

// Run
await fetch('https://aws.amazon.com').catch((error) => {
await fetch(URL).catch((error) => {
// Expected
});
plugin.disable();
Expand All @@ -246,6 +252,7 @@ describe('FetchPlugin tests', () => {
http: {
request: {
method: 'GET',
url: URL,
traced: true
}
},
Expand Down Expand Up @@ -274,7 +281,7 @@ describe('FetchPlugin tests', () => {
plugin.disable();

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com', init);
await fetch(URL, init);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com', init);
await fetch(URL, init);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

// Assert
Expand Down Expand Up @@ -416,7 +423,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOnContext);

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

// Assert
Expand Down Expand Up @@ -445,7 +452,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOnContext);

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com').catch((error) => {
await fetch(URL).catch((error) => {
// Expected
});
plugin.disable();
Expand Down Expand Up @@ -500,7 +507,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOffContext);

// Run
await fetch('https://aws.amazon.com').catch((error) => {
await fetch(URL).catch((error) => {
// Expected
});
plugin.disable();
Expand Down Expand Up @@ -535,7 +542,7 @@ describe('FetchPlugin tests', () => {
plugin.load(xRayOffContext);

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();
global.fetch = mockFetch;

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

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

// Run
await fetch({ url: 'https://aws.amazon.com' } as Request);
await fetch({ url: URL } as Request);
plugin.disable();

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

// Run
await fetch('https://aws.amazon.com');
await fetch(URL);
plugin.disable();

// Assert
Expand Down Expand Up @@ -720,7 +727,7 @@ describe('FetchPlugin tests', () => {
}
};

const request: Request = new Request('https://aws.amazon.com', init);
const request: Request = new Request(URL, init);

// Run
await fetch(request);
Expand All @@ -730,4 +737,36 @@ describe('FetchPlugin tests', () => {
expect(request.headers.get(X_AMZN_TRACE_ID)).toEqual(TRACE_ID);
expect(request.headers.get(SIGN_HEADER_KEY)).toEqual(SIGN_HEADER_VAL);
});

test('when fetch uses request object then the URL is added to the event', async () => {
// Init
const config: PartialHttpPluginConfig = {
urlsToInclude: [/aws\.amazon\.com/],
recordAllRequests: true
};

const plugin: FetchPlugin = new FetchPlugin(config);
plugin.load(xRayOffContext);

const request: Request = new Request(URL);

// Run
await fetch(request);
plugin.disable();

// Assert
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(record).toHaveBeenCalledTimes(1);
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET',
url: URL
},
response: {
status: 200,
statusText: 'OK'
}
});
});
});
9 changes: 6 additions & 3 deletions src/plugins/event-plugins/__tests__/XhrPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('XhrPlugin tests', () => {
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET'
method: 'GET',
url: './response.json'
},
response: {
status: 200,
Expand Down Expand Up @@ -106,7 +107,8 @@ describe('XhrPlugin tests', () => {
http: {
request: {
method: 'GET',
traced: true
traced: true,
url: './response.json'
},
response: { status: 200, content_length: 125 }
}
Expand Down Expand Up @@ -256,7 +258,8 @@ describe('XhrPlugin tests', () => {
expect(record.mock.calls[0][0]).toEqual(HTTP_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject({
request: {
method: 'GET'
method: 'GET',
url: './response.json'
},
error: {
version: '1.0.0',
Expand Down

0 comments on commit fb1f758

Please sign in to comment.