DEV Community

DevCorner
DevCorner

Posted on

Monitoring with Prometheus and Grafana: A Comprehensive Guide

Introduction

In modern cloud-native applications, monitoring is crucial for ensuring system reliability, performance, and availability. Prometheus and Grafana are two widely used tools in the observability stack, helping teams collect, analyze, and visualize metrics in real-time.

This blog explores how Prometheus and Grafana work together, the key metrics they provide, and how they enhance system monitoring.


What is Prometheus?

Prometheus is an open-source monitoring system that collects and stores time-series data. It is widely used for monitoring applications, microservices, and infrastructure.

Key Features of Prometheus:

  • Pull-based Data Collection: Prometheus scrapes metrics from targets via HTTP endpoints.
  • Multi-dimensional Data Model: Uses key-value labels for powerful querying.
  • Time-Series Storage: Efficiently stores high-cardinality data.
  • PromQL (Prometheus Query Language): A powerful query language for analyzing metrics.
  • Alerting Support: Integrates with Alertmanager for notifications.
  • Service Discovery: Automatically detects targets in Kubernetes, AWS, and other environments.

What is Grafana?

Grafana is an open-source visualization tool that integrates with Prometheus and other data sources to create dashboards and alerts.

Key Features of Grafana:

  • Rich Visualizations: Graphs, heatmaps, tables, and more.
  • Multiple Data Sources: Supports Prometheus, InfluxDB, MySQL, Elasticsearch, etc.
  • Dynamic Dashboards: Enables real-time monitoring with custom filters.
  • Alerting Mechanism: Notifies teams via email, Slack, PagerDuty, etc.
  • User Authentication & Role Management: Secures access with RBAC and integrations like OAuth, LDAP.

Key Metrics in Prometheus and Grafana

1. Infrastructure Metrics

These metrics provide insights into server and network performance.

  • CPU Usage (node_cpu_seconds_total): Tracks CPU consumption per core.
  • Memory Usage (node_memory_Active_bytes): Shows active RAM consumption.
  • Disk Usage (node_filesystem_avail_bytes): Monitors available disk space.
  • Network Traffic (node_network_receive_bytes_total): Measures network bandwidth usage.

2. Application Metrics

These metrics help in understanding application behavior and performance.

  • Request Rate (http_requests_total): Tracks the number of HTTP requests.
  • Error Rate (http_requests_total{status_code=~"5.."}): Monitors 5xx errors.
  • Latency (histogram_quantile(0.95, rate(http_request_duration_seconds[5m]))): Measures request response time (95th percentile).

3. Database Metrics

These metrics help optimize database performance.

  • Query Execution Time (pg_stat_statements): Measures SQL query duration.
  • Connections (pg_stat_activity): Tracks active database connections.
  • Cache Hit Ratio (pg_stat_database_blks_hit/pg_stat_database_blks_read): Indicates how effectively cache is being used.

4. Container & Kubernetes Metrics

For cloud-native environments, Prometheus collects vital Kubernetes data.

  • Pod CPU Usage (container_cpu_usage_seconds_total): Monitors per-container CPU consumption.
  • Memory Limits (container_memory_usage_bytes): Tracks memory utilization.
  • Pod Restarts (kube_pod_container_status_restarts_total): Detects unstable workloads.
  • Service Availability (kube_service_spec_ports): Checks service reachability.

5. Custom Business Metrics

Teams can define custom application-specific metrics, such as:

  • User Sign-ups (business_user_signups_total): Tracks new registrations.
  • Order Processing Time (business_order_processing_seconds): Measures order handling speed.
  • Failed Transactions (business_transaction_failures_total): Monitors failed payments.

Prometheus + Grafana: A Powerful Combination

  1. Prometheus collects metrics from various sources (applications, servers, Kubernetes, databases).
  2. Grafana queries Prometheus for stored metrics using PromQL.
  3. Grafana dashboards visualize real-time and historical trends.
  4. Alerts trigger notifications based on predefined thresholds in Prometheus or Grafana.

Setting Up Prometheus and Grafana

1. Install Prometheus

# Download Prometheus
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-linux-amd64.tar.gz
tar -xvf prometheus-linux-amd64.tar.gz
cd prometheus-linux-amd64

# Start Prometheus
./prometheus --config.file=prometheus.yml
Enter fullscreen mode Exit fullscreen mode

2. Install Grafana

# Install Grafana on Linux
wget https://dl.grafana.com/oss/release/grafana-9.3.6.linux-amd64.tar.gz
tar -zxvf grafana-9.3.6.linux-amd64.tar.gz
cd grafana-9.3.6/bin
./grafana-server
Enter fullscreen mode Exit fullscreen mode

3. Configure Prometheus as Data Source in Grafana

  1. Open Grafana (http://localhost:3000).
  2. Navigate to Settings > Data Sources.
  3. Add Prometheus (http://localhost:9090) as a new data source.
  4. Save and test the connection.

Conclusion

Prometheus and Grafana together form a robust monitoring solution for modern applications. With detailed metrics, real-time visualization, and alerting capabilities, they help teams maintain system health and performance effectively.

By integrating Prometheus for metrics collection and Grafana for visualization, organizations gain deeper insights into their applications, allowing them to prevent downtime, optimize performance, and troubleshoot issues efficiently.


Do you use Prometheus and Grafana for monitoring? Share your experiences in the comments! 🚀

Top comments (0)