Inspector
BlogTry for free
  • Concepts
    • Introduction
    • Metrics
    • Notification Channels
      • Email
      • Slack
      • Telegram
      • Microsoft Teams
      • Google Chat
      • Webhook
      • Discord
      • Pushover
      • PagerDuty
      • Twilio - SMS
    • Filtering Syntax
    • Alerts
    • Grouping Patterns
    • Custom Segments
    • Billing
    • AI Bug Fixer
  • Security and access
    • Access control
    • Two-factor authentication
    • Connected Devices
  • SDK
    • PHP
      • Installation & Set Up
      • Custom Segments
      • Exceptions Monitoring
      • Configuration
    • Laravel / Lumen
      • Upgrade Guide
      • Installation & Set-up
      • Http Requests Monitoring
      • Configuration
      • Exception Monitoring
      • Laravel Vapor
      • Laravel Octane
      • Laravel Nova Tool
      • Group by service name
    • Symfony
      • Installation
      • Configuration
      • Exception Monitoring
    • CodeIgniter
      • Installation
      • Configuration
      • Exception Monitoring
    • Drupal
    • Spring Boot
    • Slim
    • NodeJS
      • Configurations
      • Custom Segments
      • Exception monitoring
      • Autowiring
    • ExpressJs
    • Fastify
    • Python
    • Django
      • Installation & Set Up
      • Custom Segments
      • Error Monitoring
  • REST API
    • Authentication
    • Apps
    • Platforms
    • Transactions
    • Segments
    • Analytics
Powered by GitBook
On this page
  • Install
  • Configure the Ingestion Key
  • Initialization
  • Exclude paths
  • Access the Inspector instance
  • Custom Segments
  1. SDK

ExpressJs

Code Execution Monitoring for Express applications.

PreviousAutowiringNextFastify

Last updated 2 years ago

Install

Install the latest version using the npm command below:

npm install @inspector-apm/inspector-nodejs --save

Configure the Ingestion Key

You need an Ingestion Key to create an Inspector instance. Get a new one by creating a project in your dashboard.

If you use dotenv you can configure the Inspector Ingestion Key in your environment file:

INSPECTOR_INGESTION_KEY=[ingestion key]

Initialization

Just two steps:

  1. Initialize Inspector as first line of code, before you require any other modules - i.e. before express, http, mysql, etc.

  2. Attach the expressMiddleware() to your express app.

/* -------------------------------------------
 * Initialize Inspector at the top of the file, before any other module.
 --------------------------------------------- */
const inspector = require('@inspector-apm/inspector-nodejs')({
  ingestionKey: 'xxxxxxxxxxxxx',
});

const app = require('express')();

/* ----------------------------------------------
 * Attach the middleware to monitor HTTP requests fulfillment.
 ------------------------------------------------ */
app.use(inspector.expressMiddleware());

/* ----------------------------------------------
 * The rest of the app
 ------------------------------------------------ */
app.get('/', function (req, res) {
    return res.send('Home Page!');
});

app.get('/posts/:id', function (req, res) {
    return res.send('Single Post Details!');
});

app.listen(3006);

Send an HTTP request to your express app to see the first data in your dashboard.

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 which routes you want to exclude:

app.use(inspector.expressMiddleware({
    excludePaths: [
        '/posts',
        '/posts/:id',
        '/admin'
    ]
}));

You can also use the wildcard character * to match a subset of your urls:

app.use(inspector.expressMiddleware({
    excludePaths: [
        // Single wildcard
        '/posts*',
        
        // More wildcards
        '/admin/*/posts*'
    ]
}));

Access the Inspector instance

Inspector will decorate the request instance with a new property to access Inspector anywhere in your application:

app.get('/', (req, res) => {
    
    // Access Inspector from the request instance.
    req.inspector.addSegment(...);
    
});

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.

Some of the most used modules will be autowired by default. .

Inspector
Learn more about autowiring
Custom Segments