Node.js Monitoring: Tools and Libraries for Observability

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Monitoring JavaScript (Node.js) Applications: Libraries and Tools



In modern web development, monitoring Node.js applications is not just an option, but a necessity. Without it, it is impossible to guarantee stability, performance, and rapid error detection. In this article, we will thoroughly examine the key tools and libraries for building an observability system in the Node.js ecosystem.



Why is Node.js Monitoring Needed?

Node.js is a single-threaded environment with non-blocking I/O. This means that a single error or memory leak can paralyze the entire application. Monitoring helps to:

  • Track metrics (CPU, memory, request count, response time).
  • Detect anomalies (sudden increase in errors, performance drops).
  • Perform profiling (code bottlenecks, memory leaks).
  • Configure alerts (notifications in Slack, Telegram, email).


Key Libraries for Node.js Monitoring



1. Prometheus Client (prom-client)

This is the standard metrics exporter for Node.js, compatible with the popular Prometheus metrics collection system. The library allows you to create custom counters, histograms, and summary metrics.

const client = require('prom-client');const express = require('express');const app = express();

// Create a request counterconst httpRequestsTotal = new client.Counter({ name: 'http_requests_total', help: 'Total number of HTTP requests', labelNames: ['method', 'route', 'status']});

// Middleware for collecting metricsapp.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { httpRequestsTotal.inc({ method: req.method, route: req.route?.path || 'unknown', status: res.statusCode }); }); next();});

// Endpoint for Prometheusapp.get('/metrics', async (req, res) => { res.set('Content-Type', client.register.contentType); res.end(await client.register.metrics());});

app.listen(3000);


2. OpenTelemetry for Node.js

OpenTelemetry is a modern standard for collecting telemetry (traces, metrics, logs). For Node.js, the @opentelemetry/sdk-node package is available. It automatically instruments popular libraries (Express, HTTP, gRPC) and collects distributed traces.

const { NodeSDK } = require('@opentelemetry/sdk-node');const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

const sdk = new NodeSDK({ traceExporter: new ConsoleSpanExporter(), instrumentations: [getNodeAutoInstrumentations()]});

sdk.start();

// Example of automatic Express tracingconst express = require('express');const app = express();app.get('/', (req, res) => res.send('Hello'));app.listen(3000);


3. PM2 — Process Manager with Monitoring

PM2 not only manages the startup of Node.js processes but also provides built-in monitoring. The pm2 monit command shows CPU, memory, and restart count in real-time. For advanced monitoring, use pm2-plus or pm2-metrics.

// ecosystem.config.jsmodule.exports = {  apps: [{    name: 'my-app',    script: 'app.js',    instances: 2,    exec_mode: 'cluster',    env: {      NODE_ENV: 'production'    }  }]};


4. Clinic.js — Profiling and Diagnostics

Clinic.js is a set of tools for in-depth performance analysis of Node.js. It includes Doctor (quick diagnostics), Bubbleprof (async delays), and Flame (flamegraph for CPU).

// Installation: npm install -g clinic// Run with profiling                    

Recommendations