Skip to content

Commit 0c92795

Browse files
committed
fix: Extract transaction from nested express paths correctly
1 parent 73182c5 commit 0c92795

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

packages/node/src/handlers.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
8080
try {
8181
// Express.js shape
8282
const request = req as {
83+
url: string;
84+
originalUrl: string;
8385
method: string;
8486
route: {
8587
path: string;
@@ -91,18 +93,28 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
9193
};
9294
};
9395

96+
let routePath;
97+
try {
98+
if (request.originalUrl) {
99+
routePath = url.parse(request.originalUrl).pathname;
100+
} else {
101+
routePath = url.parse(request.url).pathname;
102+
}
103+
} catch (_oO) {
104+
routePath = request.route.path;
105+
}
106+
94107
switch (type) {
95108
case 'path': {
96-
return request.route.path;
109+
return routePath;
97110
}
98111
case 'handler': {
99112
return request.route.stack[0].name;
100113
}
101114
case 'methodPath':
102115
default: {
103116
const method = request.method.toUpperCase();
104-
const path = request.route.path;
105-
return `${method}|${path}`;
117+
return `${method}|${routePath}`;
106118
}
107119
}
108120
} catch (_oO) {

packages/node/test/handlers.test.ts

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@ import { Event, Request, User } from '../src';
44
import { parseRequest } from '../src/handlers';
55

66
describe('parseRequest', () => {
7-
const mockReq = {
8-
body: 'foo',
9-
cookies: { test: 'test' },
10-
headers: {
11-
host: 'mattrobenolt.com',
12-
},
13-
method: 'POST',
14-
url: '/some/path?key=value',
15-
user: {
16-
custom_property: 'foo',
17-
18-
id: 123,
19-
username: 'tobias',
20-
},
21-
};
7+
let mockReq: { [key: string]: any };
8+
9+
beforeEach(() => {
10+
mockReq = {
11+
body: 'foo',
12+
cookies: { test: 'test' },
13+
headers: {
14+
host: 'mattrobenolt.com',
15+
},
16+
method: 'POST',
17+
url: '/some/url?key=value',
18+
originalUrl: '/some/originalUrl?key=value',
19+
user: {
20+
custom_property: 'foo',
21+
22+
id: 123,
23+
username: 'tobias',
24+
},
25+
route: {
26+
path: '/path',
27+
stack: [
28+
{
29+
name: 'routeHandler',
30+
},
31+
],
32+
},
33+
};
34+
});
2235

2336
describe('parseRequest.contexts runtime', () => {
2437
test('runtime name must contain node', () => {
@@ -121,4 +134,34 @@ describe('parseRequest', () => {
121134
expect(parseRequest({}, { ...mockReq, method: 'HEAD' }, {}).request).not.toHaveProperty('data');
122135
});
123136
});
137+
138+
describe('parseRequest.transaction property', () => {
139+
test('extracts method and full route path by default from `originalUrl`', () => {
140+
const parsedRequest: Event = parseRequest({}, mockReq);
141+
expect(parsedRequest.transaction).toEqual('POST|/some/originalUrl');
142+
});
143+
144+
test('extracts method and full route path by default from `url` if `originalUrl` is not present', () => {
145+
delete mockReq.originalUrl;
146+
const parsedRequest: Event = parseRequest({}, mockReq);
147+
expect(parsedRequest.transaction).toEqual('POST|/some/url');
148+
});
149+
150+
test('fallback to method and `route.path` if previous attempts failed', () => {
151+
delete mockReq.originalUrl;
152+
delete mockReq.url;
153+
const parsedRequest: Event = parseRequest({}, mockReq);
154+
expect(parsedRequest.transaction).toEqual('POST|/path');
155+
});
156+
157+
test('can extract path only instead if configured', () => {
158+
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'path' });
159+
expect(parsedRequest.transaction).toEqual('/some/originalUrl');
160+
});
161+
162+
test('can extract handler name instead if configured', () => {
163+
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'handler' });
164+
expect(parsedRequest.transaction).toEqual('routeHandler');
165+
});
166+
});
124167
});

0 commit comments

Comments
 (0)