Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Sentry from '@sentry/node';
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
import express from 'express';

const app = express();

const users = express.Router();

users.get('/users/:userId', (_req, res) => {
Sentry.captureMessage('Custom Message');
return res.send('Success');
});

const api = express.Router();

api.use('/v1', users, (_req, res) => {
res.status(404).send('Not found');
});

app.use('/api', api);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ describe('express multiple routers', () => {
});
});

createCjsTests(__dirname, 'scenario-mount-with-two-handlers.mjs', 'instrument.mjs', (createRunner, test) => {
// One `use` call with two handlers adds two layers, and only the last one gets a registration event.
test('should construct correct url for a router that is mounted together with a second handler.', async () => {
const runner = createRunner()
.ignore('span')
.expect({ event: { message: 'Custom Message', transaction: 'GET /api/v1/users/:userId' } })
.start();
runner.makeRequest('get', '/api/v1/users/1234');
await runner.completed();
});
});

describe('complex-router', () => {
createCjsTests(__dirname, 'scenario-complex-router.mjs', 'instrument.mjs', (createRunner, test) => {
test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function getSpanForLayer(data: HandleChannelContext, options: ExpressIntegration
// `request_handler` can reconstruct the full route with parameters intact
// (`req.baseUrl` only exposes the *resolved* mount prefix). The matching pop
// happens on `asyncStart` when the layer hands off via `next`.
const registeredPath = getLayerRegisteredPath(layer);
const registeredPath = getLayerRegisteredPath(layer) ?? getMatchedLayerPath(layer);
if (registeredPath != null) {
pushLayerPath(req, registeredPath);
data._sentryStoredLayer = true;
Expand Down Expand Up @@ -377,6 +377,19 @@ function getSpanForLayer(data: HandleChannelContext, options: ExpressIntegration
return span;
}

/**
* The path of a matched layer for which no registration event recorded a path. That is the case when the events
* arrive after the registration calls: Cloudflare delivers the events of calls at module scope with the first
* request.
*/
function getMatchedLayerPath(layer: ExpressLayer): string | undefined {
if (layer.route) {
return getLayerPath([layer.route.path]);
}
// A mount path without parameters is equal to the part of the URL that the layer matched.
return layer.keys?.length ? undefined : layer.path || undefined;
}

function getLayerType(layer: ExpressLayer): ExpressLayerType {
if (layer.name === 'router') {
return 'router';
Expand Down
1 change: 1 addition & 0 deletions packages/server-utils/src/integrations/express/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ExpressLayer {
handle?: { length?: number };
name?: string;
path?: string;
keys?: unknown[];
route?: { path?: unknown };
// Only set on a Route's *inner* method-handler layers (e.g. `'get'`), which
// run inside the route-dispatch layer we already span.
Expand Down
Loading