Monitoring JavaScript Applications: Metrics and Alerts from A to Z

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 Applications: Metrics and Alerts from A to Z

\n\n

In the modern development world, it is not enough to simply write working code. It is critically important to understand how your application behaves in a production environment. Monitoring JavaScript applications (especially on Node.js) is not just a trend, but a necessity. It allows you to identify bottlenecks, prevent failures, and ensure high service availability. In this article, we will cover the full cycle: from collecting key metrics to setting up alerts, using a modern stack of tools.

\n\n

Why is Node.js Monitoring Important?

\n

Node.js applications often operate asynchronously, processing thousands of requests per second. Without monitoring, you risk:

\n
    \n
  • Missing a memory leak that could crash the server.
  • \n
  • Not noticing an increase in response time (latency), which degrades the user experience.
  • \n
  • Losing data due to unhandled errors.
  • \n
\n

A monitoring system gives you visibility (observability) and allows you to make decisions based on data, not guesses.

\n\n

Key Metrics for JavaScript Applications

\n

All metrics can be divided into three groups: infrastructure, business metrics, and application metrics. Let's look at them in detail.

\n\n

1. Infrastructure Metrics (USE Method)

\n

These are basic server health indicators: CPU, memory, disk, network. For Node.js, memory (heap, non-heap) and the number of open file descriptors are especially important.

\n\n

2. Application Metrics (RED Method)

\n

The RED method (Rate, Errors, Duration) is the gold standard for microservices:

\n
    \n
  • Rate: requests per second (RPS).
  • \n
  • Errors: percentage of requests that resulted in an error (4xx, 5xx).
  • \n
  • Duration: request execution time (p50, p95, p99 percentiles).
  • \n
\n\n

3. Business Metrics

\n

Specific to your product: number of registered users, completed orders, active sessions, etc.

\n\n

Collecting Metrics with Prometheus and Node.js

\n

Prometheus is the de facto standard for collecting metrics. For Node.js, we use the prom-client library. Let's look at a setup example.

\n\n

Installation and Basic Setup

\n
npm install prom-client express
\n\n

Let's create a server with a /metrics endpoint:

\n
const express = require('express');\nconst client = require('prom-client');\n\nconst app = express();\nconst port = 3000;\n\n// Create a metrics registry\nconst register = new client.Registry();\n\n// Add default Node.js metrics\nclient.collectDefaultMetrics({ register });\n\n// Create a custom histogram for response time\nconst httpRequestDuration = new client.Histogram({\n    name: 'http_request_duration_seconds',\n    help: 'Duration of HTTP requests in seconds',\n    labelNames: ['method', 'route', 'status'],\n    buckets: [0.01, 0.05, 0.1, 0.5, 1, 2, 5]\n});\nregister.registerMetric(httpRequestDuration);\n\n// Middleware for measuring time\napp.use((req, res, next) => {\n    const end = httpRequestDuration.startTimer();\n    res.on('finish', () => {\n        end({ method: req.method, route: req.route?.path || 'unknown', status: res.statusCode });\n    });\n    next();\n});\n\n// Endpoint for metrics\napp.get('/metrics', async (req, res) => {\n    res.set('Content-Type', register.contentType);\n    res.end(await register.metrics());\n});\n\napp.get('/', (req, res) => {\n    res.send('Hello, monitoring!');\n});\n\napp.listen(port, () => {\n    console.log(`App listening at http://localhost:${port}`);\n});
\n\n

Custom Counters

\n

Let's add an error counter:

\n
const errorCounter = new client.Counter({\n    name: 'app_errors_total',                

Blogs

Book Recommendations