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
12 changes: 12 additions & 0 deletions dev-packages/e2e-tests/test-applications/express-5/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ app.get('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});

const mountedRouter = express.Router({ mergeParams: true });
mountedRouter.get('/posts/:postId', function (req, res) {
res.send({ params: req.params });
});
// `Router#use` with two handlers adds two layers but sends one registration event, for the last
// layer. So on every runtime, the path of `mountedRouter` comes from the matched layer.
const parentRouter = express.Router();
parentRouter.use('/test-mounted/:userId', mountedRouter, function (_req, res) {
res.status(404).send({ error: 'Not found' });
});
app.use(parentRouter);

app.get('/test-mysql', function (req, res) {
// One connection per request, because on Cloudflare a socket only works in the request that opened it.
const connection = mysql.createConnection({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ test('Names the request span after the parameterized route', async ({ baseURL })
});
});

test('Names the request span after the route of a mounted router', async ({ baseURL }) => {
const spansPromise = collectStreamedSpansUntilSegment('express-5', 'GET /test-mounted/:userId/posts/:postId');

const response = await fetch(`${baseURL}/test-mounted/42/posts/7`);
expect(await response.json()).toEqual({ params: { userId: '42', postId: '7' } });

const segmentSpan = (await spansPromise).find(span => span.is_segment)!;
expect(segmentSpan.attributes).toMatchObject({
'sentry.op': { type: 'string', value: 'http.server' },
'sentry.segment.name.source': { type: 'string', value: 'route' },
'http.route': { type: 'string', value: '/test-mounted/:userId/posts/:postId' },
'url.path': { type: 'string', value: '/test-mounted/42/posts/7' },
});
});

test('Keeps the scope data of concurrent requests apart', async ({ baseURL }) => {
const ids = ['1', '2', '3'];
const errorPromises = ids.map(id =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ function getMatchedLayerPath(layer: ExpressLayer): string | undefined {
if (layer.route) {
return getLayerPath([layer.route.path]);
}
// `router` 2.3.0 and later keep the registered mount path in `rawPath` and no longer store the matched `path`.
if (layer.rawPath !== undefined) {
return getLayerPath([layer.rawPath]);
}
// 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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/server-utils/src/integrations/express/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface ExpressLayer {
name?: string;
path?: string;
keys?: unknown[];
// The path pattern of an `app.use()` or `router.use()` call, e.g. `/users/:id`. Set by `router` 2.3.0 and later.
rawPath?: 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