How to Create a Grafana Dashboard to Visualize Prometheus Metrics: A Step-by-Step Guide
Grafana is a powerful open-source platform for monitoring and observability that integrates seamlessly with Prometheus to visualize metrics in real-time. In this guide, we will install Grafana, connect it to Prometheus, and create a dashboard that visualizes key metrics like CPU usage and memory usage.
Prerequisites
- Prometheus installed and running (as detailed in the previous blog).
- Node.js application exposing metrics at
/metrics
. - Basic understanding of Prometheus and PromQL.
- Docker installed (optional but recommended for Grafana installation).
Step 1: Install Grafana
Option 1: Install Grafana Using Docker (Recommended)
Grafana is simple to run in Docker. Use the following command to pull and run the Grafana container:
docker run -d -p 3000:3000 --name=grafana grafana/grafana
This will run Grafana on port 3000
. You can access it at http://localhost:3000.
Option 2: Install Grafana Manually
If you’re not using Docker, you can install Grafana directly on your system:
- Download and install Grafana by the Grafana installation guide.
- Start Grafana:
sudo systemctl start grafana-server
Once installed, Grafana will be accessible at http://localhost:3000
. Log in with the default username admin
and password admin
. You will be prompted to change your password on the first login.
Step 2: Configure Prometheus as a Data Source
- Open Grafana at http://localhost:3000.
- After logging in, go to the Configuration (gear icon) > Data Sources.
- Click Add Data Source.
- Select Prometheus from the list.
- Configure the Prometheus data source:
- Name: Prometheus (or any name you prefer)
- URL:
http://localhost:9090
(or wherever Prometheus is running)
- Click Save & Test. If successful, you’ll see a green confirmation message.
Step 3: Create a Grafana Dashboard
- From the Grafana home screen, go to the + icon on the left sidebar and select Dashboard.
- Click Add New Panel. This will open the panel editor, where you can configure the queries and visualization types.
Step 4: Add Panels for CPU and Memory Metrics
Now that the dashboard is created, we can add individual panels for different metrics such as CPU and memory usage.
1. Panel for CPU Usage
- In the Query section of the panel editor, type the following PromQL query to monitor CPU usage:
rate(node_cpu_seconds_total{mode!="idle"}[1m])
2. This query calculates the rate of CPU usage by excluding idle time.
3. In the Visualization tab, choose Graph to display the data as a time series chart.
4. Click Apply to add the panel to your dashboard.
2. Panel for Memory Usage
- Add another panel by clicking Add Panel at the top-right corner of the dashboard.
- Use the following PromQL query to monitor memory usage:
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100
3. This query shows the percentage of available memory.
4. Select the Gauge visualization type, so you can visualize memory usage as a gauge.
5. Click Apply to add the panel.
Step 5: Customize the Dashboard
Once you have added the panels for CPU and memory usage, you can customize the dashboard further by:
- Adding more panels for other metrics (disk I/O, network traffic, etc.).
- Setting up alerting in Grafana to notify you when metrics cross predefined thresholds.
- Applying dashboard themes and rearranging panels for better visibility.
Conclusion
In this guide, you learned how to set up Grafana, configure Prometheus as a data source, and create a dashboard to visualize key metrics like CPU and memory usage from your Node.js application. With this setup, you can gain real-time insights into your application’s performance and ensure system health.