Skip to content

Commit b8cb9e0

Browse files
committed
feat: Transactions handling for RequestHandler
1 parent 3d6ee8e commit b8cb9e0

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
## Unreleased
44

5-
- [hub]: Scope level overwrites level on the event
6-
- [core]: ref: Check for node-env first and return more accurate global object
5+
- [hub] Scope level overwrites level on the event
6+
- [core] ref: Check for node-env first and return more accurate global object
77
- [core] ref: Remove Repo interface and repos attribute from Event
8-
- [browser]: ref: Include md5 lib and transcript it to TypeScript
9-
- [browser]: feat: Better mechanism detection in TraceKit
8+
- [browser] ref: Include md5 lib and transcript it to TypeScript
9+
- [browser] feat: Better mechanism detection in TraceKit
10+
- [browser] ref: Remove default transaction from browser
11+
- [node] feat: Transactions handling for RequestHandler in Express/Hapi
12+
- [node] feat: Allow requestHandler to be configured
13+
- [node] feat: Make node transactions a pluggable integration with tests
1014

1115
## 4.0.6
1216

packages/node/src/handlers.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,43 @@ import { getCurrentHub } from './hub';
1313

1414
const DEFAULT_SHUTDOWN_TIMEOUT = 2000;
1515

16+
type TransactionTypes = 'path' | 'methodPath' | 'handler';
17+
18+
/** JSDoc */
19+
function extractTransaction(req: { [key: string]: any }, type: TransactionTypes): string | undefined {
20+
try {
21+
// Express.js shape
22+
const request = req as {
23+
method: string;
24+
route: {
25+
path: string;
26+
stack: [
27+
{
28+
name: string;
29+
}
30+
];
31+
};
32+
};
33+
34+
switch (type) {
35+
case 'path': {
36+
return request.route.path;
37+
}
38+
case 'handler': {
39+
return request.route.stack[0].name;
40+
}
41+
case 'methodPath':
42+
default: {
43+
const method = request.method.toUpperCase();
44+
const path = request.route.path;
45+
return `${method}|${path}`;
46+
}
47+
}
48+
} catch (_oO) {
49+
return undefined;
50+
}
51+
}
52+
1653
/** JSDoc */
1754
function extractRequestData(req: { [key: string]: any }): { [key: string]: string } {
1855
// headers:
@@ -112,16 +149,18 @@ function parseRequest(
112149
[key: string]: any;
113150
},
114151
options?: {
115-
version?: boolean;
116152
request?: boolean;
117153
serverName?: boolean;
154+
transaction?: boolean | TransactionTypes;
118155
user?: boolean;
156+
version?: boolean;
119157
},
120158
): SentryEvent {
121159
// tslint:disable-next-line:no-parameter-reassignment
122160
options = {
123161
request: true,
124162
serverName: true,
163+
transaction: true,
125164
user: true,
126165
version: true,
127166
...options,
@@ -152,15 +191,23 @@ function parseRequest(
152191
};
153192
}
154193

194+
if (options.transaction) {
195+
const transaction = extractTransaction(req, options.transaction);
196+
if (transaction) {
197+
event.transaction = transaction;
198+
}
199+
}
200+
155201
return event;
156202
}
157203

158204
/** JSDoc */
159205
export function requestHandler(options?: {
160-
version?: boolean;
161206
request?: boolean;
162207
serverName?: boolean;
208+
transaction?: boolean | string;
163209
user?: boolean;
210+
version?: boolean;
164211
}): (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => void {
165212
return function sentryRequestMiddleware(
166213
req: http.IncomingMessage,

0 commit comments

Comments
 (0)