Skip to content

Commit

Permalink
fix: Express transaction name (#3048)
Browse files Browse the repository at this point in the history
  • Loading branch information
HazAT authored Nov 12, 2020
1 parent e9bad47 commit 4b80692
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/tracing/src/integrations/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {

switch (arity) {
case 2: {
return function(this: NodeJS.Global, _req: Request, res: Response & SentryTracingResponse): any {
return function(this: NodeJS.Global, req: Request, res: Response & SentryTracingResponse): any {
const transaction = res.__sentry_transaction;
addExpressReqToTransaction(transaction, req);
if (transaction) {
const span = transaction.startChild({
description: fn.name,
Expand All @@ -134,6 +135,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
next: NextFunction,
): any {
const transaction = res.__sentry_transaction;
addExpressReqToTransaction(transaction, req);
const span =
transaction &&
transaction.startChild({
Expand All @@ -158,6 +160,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
next: NextFunction,
): any {
const transaction = res.__sentry_transaction;
addExpressReqToTransaction(transaction, req);
const span =
transaction &&
transaction.startChild({
Expand All @@ -179,6 +182,23 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
}
}

/**
* Set parameterized as transaction name e.g.: `GET /users/:id`
* Also adds more context data on the transaction from the request
*/
function addExpressReqToTransaction(transaction: Transaction | undefined, req: any): void {
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
if (transaction) {
if (req.route && req.route.path) {
transaction.name = `${req.method} ${req.route.path}`;
}
transaction.setData('url', req.originalUrl);
transaction.setData('baseUrl', req.baseUrl);
transaction.setData('query', req.query);
}
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
}

/**
* Takes all the function arguments passed to the original `app.use` call
* and wraps every function, as well as array of functions with a call to our `wrap` method.
Expand Down

0 comments on commit 4b80692

Please sign in to comment.