In the world of software development, understanding how your applications perform in real-time is crucial. Observability tools help you monitor and visualize metrics, logs, and traces to gain insights into your application's behavior. Two popular open-source tools for observability are Prometheus and Grafana. Prometheus is a powerful monitoring system that collects metrics from various sources, while Grafana provides an intuitive interface for visualizing these metrics.
In this tutorial, we'll explore how to set up Prometheus for monitoring and Grafana for visualization. We'll cover the basics of both tools and provide practical examples to help you get started.
Prometheus is a time-series database that collects metrics from targets at specified intervals. It uses a pull model, where it scrapes metrics from instrumented applications or exporters. Prometheus supports multi-dimensional data models with time series data identified by metric name and key/value pairs called labels.
Grafana is a visualization tool that works with Prometheus and other data sources to create dashboards. It allows you to visualize your data in various ways, such as graphs, tables, and singlestat panels.
Install Prometheus: You can download Prometheus from the official website. For this example, we'll use version 2.34.0.
wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz
tar xvfz prometheus-2.34.0.linux-amd64.tar.gz
cd prometheus-2.34.0.linux-amd64
Configure Prometheus:
Edit the prometheus.yml file to specify the targets you want to monitor.
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
Start Prometheus: Run Prometheus using the configuration file.
./prometheus --config.file=prometheus.yml
You can access the Prometheus UI at http://localhost:9090.
Install Grafana: Download and install Grafana from the official website.
wget https://dl.grafana.com/oss/release/grafana-8.3.4.linux-amd64.tar.gz
tar xvfz grafana-8.3.4.linux-amd64.tar.gz
cd grafana-8.3.4/bin
Start Grafana: Run Grafana using the default configuration.
./grafana-server
You can access the Grafana UI at http://localhost:3000. The default login credentials are admin/admin.
Add Prometheus as a Data Source:
http://localhost:9090).Create a Dashboard:
Go to Create > Dashboard.
Add a new panel.
Use PromQL to query metrics from Prometheus. For example, you can use:
rate(http_requests_total[1m])
Configure the visualization and save the dashboard.
Let's create a simple example where we monitor the CPU usage of a server using Prometheus and visualize it in Grafana.
Install Node Exporter: Node Exporter is a Prometheus exporter for hardware and OS metrics. Install it on your server.
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar xvfz node_exporter-1.2.2.linux-amd64.tar.gz
cd node_exporter-1.2.2.linux-amd64
Start Node Exporter: Run Node Exporter.
./node_exporter
Configure Prometheus to Scrape Node Exporter:
Update the prometheus.yml file to include Node Exporter as a target.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
Restart Prometheus: Restart Prometheus to apply the changes.
./prometheus --config.file=prometheus.yml
Create a Grafana Dashboard for CPU Usage:
In Grafana, create a new dashboard.
Add a panel and use the following PromQL query to visualize CPU usage:
rate(node_cpu_seconds_total{mode="user"}[1m])