From c8cf0b9201f05eb7cbd3f7516967023d19f22157 Mon Sep 17 00:00:00 2001 From: isikhi Date: Wed, 20 Sep 2023 16:59:09 +0300 Subject: [PATCH] fix: capture route from base on express app.use --- CHANGELOG.asciidoc | 1 + lib/instrumentation/express-utils.js | 2 ++ test/instrumentation/express-utils.test.js | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 7ccdf5d942d..3f5e9eaaa9e 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -50,6 +50,7 @@ See the <> guide. [float] ===== Bug fixes +* The problem of route name not being captured in Express.Use direct use in middleware has been resolved. [float] ===== Chores diff --git a/lib/instrumentation/express-utils.js b/lib/instrumentation/express-utils.js index 0a2edb65208..82595aa64fb 100644 --- a/lib/instrumentation/express-utils.js +++ b/lib/instrumentation/express-utils.js @@ -48,6 +48,8 @@ function getPathFromRequest(req, useBase, usePathAsTransactionName) { return path ? join([path, route]) : route; } else if (path && (path !== '/' || useBase)) { return path; + } else if (req.baseUrl && req.baseUrl !== '/') { + return req.baseUrl; } if (usePathAsTransactionName) { diff --git a/test/instrumentation/express-utils.test.js b/test/instrumentation/express-utils.test.js index 3917d132845..8ec19e4d90b 100644 --- a/test/instrumentation/express-utils.test.js +++ b/test/instrumentation/express-utils.test.js @@ -26,13 +26,26 @@ test('#getPathFromRequest', function (t) { t.equals(path, '/foo/bar'); t.end(); }); + t.test('should return path for express.use base urls as path ', function (t) { + const req = createRequest( + 'https://test.com/foo/bar?query=value#hash', + 'example.com', + { + baseUrl: '/foo/bar', + }, + ); + const path = getPathFromRequest(req, false, false); + t.equals(path, '/foo/bar'); + t.end(); + }); }); -function createRequest(url, host = 'example.com') { +function createRequest(url, host = 'example.com', additionalRequestItems) { return { url, headers: { host, }, + ...additionalRequestItems, }; }