Skip to content

Commit 4b80692

Browse files
authored
fix: Express transaction name (#3048)
1 parent e9bad47 commit 4b80692

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

packages/tracing/src/integrations/express.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
111111

112112
switch (arity) {
113113
case 2: {
114-
return function(this: NodeJS.Global, _req: Request, res: Response & SentryTracingResponse): any {
114+
return function(this: NodeJS.Global, req: Request, res: Response & SentryTracingResponse): any {
115115
const transaction = res.__sentry_transaction;
116+
addExpressReqToTransaction(transaction, req);
116117
if (transaction) {
117118
const span = transaction.startChild({
118119
description: fn.name,
@@ -134,6 +135,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
134135
next: NextFunction,
135136
): any {
136137
const transaction = res.__sentry_transaction;
138+
addExpressReqToTransaction(transaction, req);
137139
const span =
138140
transaction &&
139141
transaction.startChild({
@@ -158,6 +160,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
158160
next: NextFunction,
159161
): any {
160162
const transaction = res.__sentry_transaction;
163+
addExpressReqToTransaction(transaction, req);
161164
const span =
162165
transaction &&
163166
transaction.startChild({
@@ -179,6 +182,23 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
179182
}
180183
}
181184

185+
/**
186+
* Set parameterized as transaction name e.g.: `GET /users/:id`
187+
* Also adds more context data on the transaction from the request
188+
*/
189+
function addExpressReqToTransaction(transaction: Transaction | undefined, req: any): void {
190+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
191+
if (transaction) {
192+
if (req.route && req.route.path) {
193+
transaction.name = `${req.method} ${req.route.path}`;
194+
}
195+
transaction.setData('url', req.originalUrl);
196+
transaction.setData('baseUrl', req.baseUrl);
197+
transaction.setData('query', req.query);
198+
}
199+
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
200+
}
201+
182202
/**
183203
* Takes all the function arguments passed to the original `app.use` call
184204
* and wraps every function, as well as array of functions with a call to our `wrap` method.

0 commit comments

Comments
 (0)