Back to blog

Building Enterprise Monitoring: A Step-by-Step Prometheus and Grafana Guide

When running microservices in production, you cannot afford to fly blind. You need to know when your server CPU spikes, when RAM usage goes out of control, or when request latency increases.

Prometheus and Grafana are the industry-standard stack for application monitoring and metrics visualization. Let us build an enterprise monitoring setup step-by-step.

1. How the Stack Works

The monitoring workflow is straightforward:

  • Exporters: Small applications that extract metrics from your systems (e.g. Node Exporter for OS metrics, or library endpoints for your backend) and expose them on an HTTP port.
  • Prometheus: A time-series database that periodically pulls (scrapes) metrics from your exporters.
  • Grafana: A visualization server that queries Prometheus to build beautiful, real-time dashboards.

2. Exposing Metrics in Your Application

Let us expose custom metrics from a Node.js application using the prom-client library.

First, install the library:

npm install prom-client

Next, set up an endpoint to expose the default metrics:

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

const app = express();

// Enable collection of default metrics (CPU, Memory, etc.)
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics();

// Expose the metrics endpoint
app.get('/metrics', async (req, res) => {
  res.set('Content-Type', client.register.contentType);
  res.end(await client.register.metrics());
});

app.listen(3000, () => {
  console.log('App listening on port 3000');
});

When you navigate to http://localhost:3000/metrics, you will see raw metrics data in the Prometheus format.

3. Configuring Prometheus

We need to tell Prometheus to pull metrics from our Node.js application. Create a configuration file called prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'nodejs-app'
    static_configs:
      - targets: ['localhost:3000']

Start Prometheus pointing to this configuration file. It will begin scraping your application every 15 seconds.

4. Visualizing with Grafana

Once Prometheus is collecting data:

  1. Start Grafana (usually running on http://localhost:3000 by default, but if you have Node on 3000, run Grafana on port 3001).
  2. Go to Connections > Data Sources and select Prometheus.
  3. Set the URL to your Prometheus server (e.g. http://localhost:9090).
  4. Click Save & Test.
  5. Create a new Dashboard, add a visualization panel, and write PromQL (Prometheus Query Language) queries to chart your CPU, memory, or request rates over time.

Conclusion

A robust monitoring system gives you visibility into your production environment, allowing you to catch bottlenecks before they cause downtime. With Prometheus scraping metrics and Grafana displaying them, you have the observability needed to run reliable systems.