Skip to content

fix(node): Fix 404 route handling in express 5 #16211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
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
Expand Up @@ -93,8 +93,7 @@ describe('express v5 tracing', () => {
const runner = createRunner()
.expect({
transaction: {
// FIXME: This is wrong :(
transaction: 'GET /',
transaction: 'GET /does-not-exist',
contexts: {
trace: {
span_id: expect.stringMatching(/[a-f0-9]{16}/),
Expand All @@ -103,8 +102,6 @@ describe('express v5 tracing', () => {
'http.response.status_code': 404,
url: expect.stringMatching(/\/does-not-exist$/),
'http.method': 'GET',
// FIXME: This is wrong :(
'http.route': '/',
'http.url': expect.stringMatching(/\/does-not-exist$/),
'http.target': '/does-not-exist',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,18 @@ export class ExpressInstrumentationV5 extends InstrumentationBase<ExpressInstrum
// remove duplicate slashes to normalize route
.replace(/\/{2,}/g, '/');

const actualRoute = route.length > 0 ? route : undefined;

const attributes: Attributes = {
// eslint-disable-next-line deprecation/deprecation
[SEMATTRS_HTTP_ROUTE]: route.length > 0 ? route : '/',
[SEMATTRS_HTTP_ROUTE]: actualRoute,
};
const metadata = getLayerMetadata(route, layer, layerPath);
const type = metadata.attributes[AttributeNames.EXPRESS_TYPE] as ExpressLayerType;

const rpcMetadata = getRPCMetadata(context.active());
if (rpcMetadata?.type === RPCType.HTTP) {
rpcMetadata.route = route || '/';
rpcMetadata.route = actualRoute;
}

// verify against the config if the layer should be ignored
Expand Down
Loading