ExpressJs
Code Execution Monitoring for Express applications.

Install

Install the latest version using the npm command below:
1
npm install @inspector-apm/inspector-nodejs --save
Copied!

Configure the Ingestion Key

You need an Ingestion Key to create an Inspector instance. You can obtain a key creating a new project in your Inspector dashboard.
If you use dotenv you can configure the Inspector Ingestion Key in your environment file:
1
INSPECTOR_INGESTION_KEY=[ingestion key]
Copied!

Initialization

Just two steps:
    1.
    Initialize Inspector before you require any other modules in your application - i.e. before express, http, mysql, etc.
    2.
    Attach the expressMiddleware() to your express app.
1
/* -------------------------------------------
2
* Initialize Inspector at the top of the file, before any other module.
3
--------------------------------------------- */
4
const inspector = require('@inspector-apm/inspector-nodejs')({
5
ingestionKey: 'xxxxxxxxxxxxx',
6
})
7
8
const app = require('express')()
9
10
/* ----------------------------------------------
11
* Attach the middleware to monitor HTTP requests fulfillment.
12
------------------------------------------------ */
13
app.use(inspector.expressMiddleware())
14
15
app.get('/', function (req, res) {
16
return res.send('Home Page!')
17
})
18
19
app.get('/posts/:id', function (req, res) {
20
return res.send('Single Post Details!')
21
})
22
23
app.listen(3006)
Copied!
Send an HTTP request to your express app to see the first data in your dashboard.
Some of the most used modules will be autowired by default. Learn more about autowiring.

Exclude paths

If you want to turn off monitoring in some parts of your application you can pass a JSON object to the expressMiddleware function with excludePaths property to define in which routes you want to disable monitoring:
1
app.use(inspector.expressMiddleware({
2
excludePaths: [
3
'/posts',
4
'/posts/:id',
5
'/admin'
6
]
7
}))
Copied!

Access the Inspector instance

Inspector will decorate the request instance with a new property to access Inspector anywhere in your application:
1
app.get('/', (req, res) => {
2
3
// Access Inspector from the request instance.
4
req.inspector.addSegment(...);
5
6
});
Copied!

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.
Last modified 24d ago