Perfect — let’s go through metrics and monitoring for Node.js, focusing on Prometheus and Grafana, which are essential for observability and performance tracking in production applications.


1️⃣ Why Metrics & Monitoring Matter

  • Track application health (CPU, memory, requests, errors)

  • Detect performance bottlenecks early

  • Enable alerting for critical issues

  • Provide insights for scaling and optimization


2️⃣ Prometheus

  • Open-source monitoring & alerting system

  • Pull-based metrics collection via HTTP endpoints

  • Stores metrics in time-series database

  • Works well with Node.js apps, containers, and microservices

a) Install Prometheus Node Client

npm install prom-client

b) Example: Expose Metrics in Express

const express = require("express");
const client = require("prom-client");
 
const app = express();
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics(); // CPU, memory, etc.
 
// Custom metric
const httpRequestDurationMicroseconds = new client.Histogram({
  name: "http_request_duration_ms",
  help: "Duration of HTTP requests in ms",
  labelNames: ["method", "route", "status_code"],
  buckets: [50, 100, 200, 500, 1000],
});
 
app.use((req, res, next) => {
  const end = httpRequestDurationMicroseconds.startTimer();
  res.on("finish", () => {
    end({ method: req.method, route: req.path, status_code: res.statusCode });
  });
  next();
});
 
app.get("/metrics", async (req, res) => {
  res.set("Content-Type", client.register.contentType);
  res.end(await client.register.metrics());
});
 
app.listen(3000, () => console.log("Server running on port 3000"));
  • Visit http://localhost:3000/metrics → exposes Prometheus-compatible metrics

3️⃣ Grafana

  • Open-source dashboard & visualization tool

  • Pulls metrics from Prometheus (or other data sources)

  • Creates real-time dashboards, graphs, and alerts

a) Setup

  1. Install Grafana locally, via Docker, or cloud

  2. Add Prometheus as a data source

  3. Create dashboards with graphs, tables, and alerts

  • Example: Graph HTTP request duration from Prometheus metrics

  • Can set alerts: e.g., trigger if response time > 500ms


4️⃣ Metrics to Monitor in Node.js

Metric TypeExamples
System MetricsCPU usage, memory usage, disk I/O
Application MetricsHTTP request count, latency, error rate
Custom MetricsBusiness-specific events (e.g., orders processed)
Process MetricsNode.js event loop lag, heap size, garbage collection

5️⃣ Alerts & Observability

  • Prometheus supports alerting rules → send notifications via Slack, email, PagerDuty

  • Grafana supports alerting dashboards

  • Combine with loggers (Winston/Pino) → full observability


6️⃣ Key Takeaways

  1. Prometheus → collects metrics from Node.js app

  2. Grafana → visualizes metrics and sets alerts

  3. Use metrics to track performance, errors, throughput, and latency

  4. Combine metrics + logging + tracing for complete production observability


Next, we could cover Node.js production best practices, combining performance, monitoring, logging, security, clustering, and deployment strategies for real-world applications.

Do you want to go there next?