NodeJS
Code Execution Monitoring for NodeJS applications.
Install
Install the latest version using the npm command below:
Configure the Ingestion Key
You need an Ingestion Key to create an Inspector instance. Get a new one by creating a project in your Inspector dashboard.
If you use dotenv
you can configure the Inspector Ingestion Key in your environment file:
Initialization
It's important that Inspector is required in the first line of the script, before you require any other modules - i.e. before, http
, mysql
, etc.
This means that you should probably require Inspector in your application's main file (usually index.js
, server.js
or app.js
).
Here's a code example of how Inspector is normally initialized in a NodeJS script:
Some of the most used modules will be autowired by default. Learn more about autowiring.
HTTP Request Monitoring
Inspector wrap incoming http requests in an entity called "Transaction". To create a real picture of what happen in your application during an http request you should start the transaction as soon as possible in the web server.
Take a look on the example below where a custom web server use patterns to route requests to handlers.
If the transaction is sterted correctly, Inspector will automatically monitor all other task based on supported modules like mysql2
, pg
, mongodb
, etc.
If you are using Express framework, transactions are automatically labeled based on the names of your routes. You should follow the specific express documentation.
Instrument a CLI script
A NodeJS application that run by the command line can start the transaction immediately after initialization:
Custom Segments
By default Inspector will report many different tasks based on the application's dependencies, but you are free to "wrap" some parts of your code that you consider relevant to create a more complete picture of what statements are executed during an execution cycle and its performance.
Custom SegmentsLast updated