Problem:
I am using Elastic APM to monitor Express application. However, APM is not able to instrument any HTTP requests when nested routing is implemented across multiple files. It only works when all routes are defined in the same file (app.js).
Actual Behaviour:
No transaction data is being collected for any HTTP requests handled by the nested routes, which limits the visibility and performance insights.
Expected Behaviour:
I want Elastic APM to instrument all HTTP requests, regardless of whether the routes are defined in the same file or across multiple files.
How do I configure Elastic APM to properly instrument nested routes in separate files on my Express server? Is there a specific configuration or workaround I need to implement?
Please note: Due to separation of concerns, we cannot define all routes in the same file (app.js). We need a solution that works with our current code structure.
Code Structure (to reproduce the problem):
app.js: Initializes APM and sets up the Express app.
outerRouter.js: Defines a router and adds it to the app.
innerRouter.js: Defines nested routes and exports the router.
App.js
const apm = require('elastic-apm-node').start({
secretToken: 'nsdiand',
serverUrl: 'http://apm-server.domain.com',
environment: 'production'
})
import { init } from './outerRouter';
const mysql = require('mysql');
const express = require('express');
const app = express();
const port = 3000;
init(app,express)
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
outerRouter.js
const { default: newRouter } = require("./newRouter");
export function init(app, express){
const router=express.Router();
app.use(router);
router.use(newRouter);
}
innerRouter.js
const express = require('express');
const newRouter = express.Router();
newRouter.get('/nestedroute', function (req, res, next) {
res.send("test string")
})
export default newRouter
Version information:
"elastic-apm-node": "^4.0.
"express": "^4.16.4",
"node" : 16.19.1
**Kibana version**: 8.8.1
**Elasticsearch version**: 8.8.0
**APM Server version**: 8.8.1
We've already tried the following:
- Manually setting the transaction using
apm.startTransaction
- didn't work. - Loading the APM module before application starts using
-r elastic-apm-node/start.js
command line option - didn't work. - apm.middleware.connect() for every nested router. -did'nt worked
- Initializing APM in a separate file and import the APM instance from
apm.js
in nested router files.
I would appreciate any help and suggestions on how to resolve this issue.