Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Extract transaction from nested express paths correctly #2714

Merged
merged 2 commits into from Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/node/src/handlers.ts
Expand Up @@ -80,6 +80,8 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
try {
// Express.js shape
const request = req as {
url: string;
originalUrl: string;
method: string;
route: {
path: string;
Expand All @@ -91,18 +93,28 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
};
};

let routePath;
try {
if (request.originalUrl) {
routePath = url.parse(request.originalUrl).pathname;
} else {
routePath = url.parse(request.url).pathname;
}
} catch (_oO) {
routePath = request.route.path;
}

switch (type) {
case 'path': {
return request.route.path;
return routePath;
}
case 'handler': {
return request.route.stack[0].name;
}
case 'methodPath':
default: {
const method = request.method.toUpperCase();
const path = request.route.path;
return `${method}|${path}`;
return `${method}|${routePath}`;
}
}
} catch (_oO) {
Expand Down
73 changes: 58 additions & 15 deletions packages/node/test/handlers.test.ts
Expand Up @@ -4,21 +4,34 @@ import { Event, Request, User } from '../src';
import { parseRequest } from '../src/handlers';

describe('parseRequest', () => {
const mockReq = {
body: 'foo',
cookies: { test: 'test' },
headers: {
host: 'mattrobenolt.com',
},
method: 'POST',
url: '/some/path?key=value',
user: {
custom_property: 'foo',
email: 'tobias@mail.com',
id: 123,
username: 'tobias',
},
};
let mockReq: { [key: string]: any };

beforeEach(() => {
mockReq = {
body: 'foo',
cookies: { test: 'test' },
headers: {
host: 'mattrobenolt.com',
},
method: 'POST',
url: '/some/url?key=value',
originalUrl: '/some/originalUrl?key=value',
user: {
custom_property: 'foo',
email: 'tobias@mail.com',
id: 123,
username: 'tobias',
},
route: {
path: '/path',
stack: [
{
name: 'routeHandler',
},
],
},
};
});

describe('parseRequest.contexts runtime', () => {
test('runtime name must contain node', () => {
Expand Down Expand Up @@ -121,4 +134,34 @@ describe('parseRequest', () => {
expect(parseRequest({}, { ...mockReq, method: 'HEAD' }, {}).request).not.toHaveProperty('data');
});
});

describe('parseRequest.transaction property', () => {
test('extracts method and full route path by default from `originalUrl`', () => {
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/some/originalUrl');
});

test('extracts method and full route path by default from `url` if `originalUrl` is not present', () => {
delete mockReq.originalUrl;
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/some/url');
});

test('fallback to method and `route.path` if previous attempts failed', () => {
delete mockReq.originalUrl;
delete mockReq.url;
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/path');
});

test('can extract path only instead if configured', () => {
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'path' });
expect(parsedRequest.transaction).toEqual('/some/originalUrl');
});

test('can extract handler name instead if configured', () => {
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'handler' });
expect(parsedRequest.transaction).toEqual('routeHandler');
});
});
});