Skip to content

Commit 5325fe3

Browse files
authored
fix: Extract transaction from nested express paths correctly (#2714)
* fix: Extract transaction from nested express paths correctly
1 parent b210907 commit 5325fe3

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

packages/node/src/handlers.ts

Lines changed: 11 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,24 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
9193
};
9294
};
9395

96+
let routePath;
97+
try {
98+
routePath = url.parse(request.originalUrl || request.url).pathname;
99+
} catch (_oO) {
100+
routePath = request.route.path;
101+
}
102+
94103
switch (type) {
95104
case 'path': {
96-
return request.route.path;
105+
return routePath;
97106
}
98107
case 'handler': {
99108
return request.route.stack[0].name;
100109
}
101110
case 'methodPath':
102111
default: {
103112
const method = request.method.toUpperCase();
104-
const path = request.route.path;
105-
return `${method}|${path}`;
113+
return `${method}|${routePath}`;
106114
}
107115
}
108116
} 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+
originalUrl: '/some/originalUrl?key=value',
18+
route: {
19+
path: '/path',
20+
stack: [
21+
{
22+
name: 'routeHandler',
23+
},
24+
],
25+
},
26+
url: '/some/url?key=value',
27+
user: {
28+
custom_property: 'foo',
29+
30+
id: 123,
31+
username: 'tobias',
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)