[Node] Transaction not showing properly for express/node14

I have a simple node api server build using express
in Top 20 transactions sorted by Slowest average response time, the transaction names are not proper and “POST /api” is always on top.

Hi @testwicket02

Thanks for reaching out here for support, we are here to help!

I can see my colleague @JoiConverse has gone ahead and looped in the engineer team to help support here, as its a little out of our scope.

Please note they will reach out via this post with their findings.

Also should you have any additional questions, findings or fixes, please do reach out!

so, I was going through the docs, found the top_n config in I tried setting it to higher value like ~30 and also tried setting it 0 still the same problem is there.
Can you please explain what it does and how to use it properly?

Hi @testwicket02 ,

The top_n configuration option pertains to changing the amount of transactions that are eligible for transaction traces and would not address the problem that you are seeing. If you are using Express and only seeing one transaction report, then I would double-check to make sure that require('newrelic'); is the first line in your application’s main module, which is a requirement for installing the Node agent. You can read more about this at the following link:

If you are using Babel or a similar transpiler you can safeguard against any issues related to module load order by utilizing the Node command line option ‘-r’ to preload the ‘newrelic’ module at application startup. For example, if your application’s entry point is ./dist/server.js then you would use the require flag like so:

node -r newrelic ./dist/server.js

If you are unable to require('newrelic'); as the first line of your app’s main module and you are unable to use the require flag as above (e.g. asynchronously loading api keys from a remote location during application bootstrapping), you may also add stock instrumentation to an already loaded supported module by using newrelic.instrumentLoadedModule:

// module loaded before newrelic 
const expressModule = require('express');

// load the agent
const newrelic = require('newrelic');

// instrument express after the agent has been loaded
newrelic.instrumentLoadedModule( 
  'express',    // the module's name, as a string  
  expressModule // the module instance
);