Skip to content

Monitoring with Prometheus and Grafana

Medium ~50 min

What you'll build

A monitoring stack: a Node.js app with Prometheus metrics, a Prometheus server scraping metrics, and a Grafana dashboard visualizing them.

Goals

  • Instrument an app with Prometheus metrics
  • Configure Prometheus to scrape metrics
  • Set up Grafana with a data source
  • Build a monitoring dashboard

Prerequisites

  • Docker basics
  • Understanding of metrics and time series
  • HTTP endpoints

Steps

  1. 1

    Instrument the app with metrics

    Add the prom-client library to a Node.js app and expose a /metrics endpoint. Track request count and response time.

    Code
    // app.js
    const express = require('express');
    const promClient = require('prom-client');
    
    const app = express();
    
    // Collect default metrics (CPU, memory, etc.)
    promClient.collectDefaultMetrics();
    
    // Custom counter
    const requestCount = new promClient.Counter({
      name: 'http_requests_total',
      help: 'Total HTTP requests',
      labelNames: ['method', 'route']
    });
    
    // Custom histogram
    const responseTime = new promClient.Histogram({
      name: 'http_response_time_seconds',
      help: 'HTTP response time in seconds',
      buckets: [0.1, 0.5, 1, 2, 5]
    });
    
    app.use((req, res, next) => {
      const start = Date.now();
      res.on('finish', () => {
        requestCount.inc({ method: req.method, route: req.route?.path || 'unknown' });
        responseTime.observe((Date.now() - start) / 1000);
      });
      next();
    });
    
    app.get('/metrics', (req, res) => res.set('Content-Type', promClient.register.contentType).end(promClient.register.metrics()));
    
    app.get('/', (req, res) => res.json({ hello: 'world' }));
    app.listen(3000);

    Expected result

    The app exposes /metrics with request counts, response times, and default Node.js metrics.

  2. 2

    Configure Prometheus

    Create a prometheus.yml config that scrapes the app's /metrics endpoint every 15 seconds.

    Code
    # prometheus.yml
    global:
      scrape_interval: 15s
    
    scrape_configs:
      - job_name: 'node-app'
        static_configs:
          - targets: ['app:3000']
    
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']

    Expected result

    Prometheus is configured to scrape the app and itself every 15 seconds.

  3. 3

    Set up the monitoring stack with Docker Compose

    Create a docker-compose.yml with the app, Prometheus, and Grafana services. Acceptance: all three services start and Prometheus can reach the app.

  4. 4

    Configure Grafana data source

    Add Prometheus as a data source in Grafana. This can be done via the UI or by provisioning a config file. Acceptance: Grafana's data source test shows Prometheus is connected.

  5. 5

    Build a dashboard

    Create a Grafana dashboard with panels for: request rate (rate(http_requests_total[5m])), p95 response time (histogram_quantile(0.95, rate(...))), and CPU usage. Acceptance: the dashboard shows live data when you send requests to the app.

    Code
    # Key PromQL queries for the dashboard:
    # Request rate (requests per second)
    rate(http_requests_total[5m])
    
    # P95 response time
    histogram_quantile(0.95, rate(http_response_time_seconds_bucket[5m]))
    
    # CPU usage percentage
    rate(process_cpu_seconds_total[1m]) * 100
    
    # Memory usage
    process_resident_memory_bytes / 1024 / 1024

    Expected result

    The dashboard shows live metrics updating every few seconds as traffic hits the app.

Try it yourself

What you learned

You built a complete monitoring stack — instrumenting an app, collecting metrics with Prometheus, and visualizing with Grafana. Monitoring is essential for operating production systems: you can't fix what you can't see.

Related Resources

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic