Grafana và Prometheus là hai công cụ phổ biến trong hệ sinh thái giám sát và quản lý hệ thống, thường được sử dụng cùng nhau để giám sát hiệu năng, thu thập và phân tích dữ liệu trong các ứng dụng và hệ thống phân tán.
1. Prometheus là gì?
Prometheus là một hệ thống giám sát và cảnh báo mã nguồn mở, tập trung vào việc thu thập, lưu trữ và xử lý các số liệu (metrics).
Đặc điểm chính
- Pull-based: Prometheus thu thập số liệu từ các endpoint của ứng dụng bằng cách gọi HTTP (thường là
/metrics
). - Thời gian thực: Hỗ trợ lưu trữ chuỗi thời gian (time-series database – TSDB) để phân tích dữ liệu thời gian thực.
- Ngôn ngữ truy vấn PromQL: Cho phép viết các truy vấn phức tạp để phân tích số liệu.
- Cảnh báo (Alert): Có thể tích hợp với Alertmanager để gửi cảnh báo qua Email, Slack, PagerDuty, v.v.
- Exporter: Dùng để chuyển đổi các số liệu từ hệ thống hoặc dịch vụ khác (MySQL, Kafka, JVM, Docker, v.v.) sang định dạng mà Prometheus hiểu được.
Ứng dụng
- Theo dõi hiệu năng ứng dụng, CPU, RAM, I/O.
- Giám sát hệ thống phân tán như Kubernetes, Docker Swarm.
- Tích hợp với các dịch vụ microservices.
2. Grafana là gì?
Grafana là một nền tảng mã nguồn mở để trực quan hóa và phân tích dữ liệu, thường được sử dụng để xây dựng dashboard.
Đặc điểm chính
- Đa dạng nguồn dữ liệu: Grafana hỗ trợ tích hợp không chỉ Prometheus mà còn nhiều nguồn khác như Elasticsearch, InfluxDB, MySQL, Graphite, v.v.
- Dashboard mạnh mẽ: Cung cấp các biểu đồ, bảng, đồ thị thời gian và nhiều loại trực quan hóa khác.
- Cảnh báo (Alerting): Cho phép cấu hình cảnh báo trực tiếp từ dashboard.
- Tùy chỉnh: Có thể thêm plugin, thiết kế giao diện dashboard theo ý muốn.
- User management: Hỗ trợ phân quyền cho từng người dùng (viewer/editor/admin).
Ứng dụng
- Hiển thị và theo dõi số liệu từ Prometheus.
- Tạo các dashboard cho các hệ thống giám sát.
- Theo dõi lịch sử, phân tích hiệu năng hệ thống.
3. Sự kết hợp Prometheus và Grafana
- Prometheus sẽ đóng vai trò thu thập và lưu trữ số liệu từ hệ thống (CPU, bộ nhớ, trạng thái container, API call, v.v.).
- Grafana sẽ kết nối với Prometheus để truy xuất dữ liệu và hiển thị trên dashboard dưới dạng các biểu đồ, giúp dễ dàng theo dõi.
Ví dụ về flow:
- Ứng dụng Spring Boot xuất số liệu metrics tại endpoint
/actuator/prometheus
. - Prometheus thu thập số liệu từ endpoint này.
- Grafana kết nối với Prometheus để hiển thị số liệu thông qua dashboard.
4. Lợi ích khi sử dụng cùng nhau
- Theo dõi real-time: Nhanh chóng phát hiện các vấn đề về hiệu năng.
- Cảnh báo: Chủ động xử lý sự cố qua các thông báo cảnh báo.
- Tối ưu hóa hệ thống: Phân tích lịch sử dữ liệu để cải thiện hiệu suất.
5. Cấu hình Grafana + Prometheus vào Spring Boot
1. Cấu hình Spring Boot
– pom.xml
<!-- Health check --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Monitoring --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency>
– application.yml
management: endpoints: web: exposure: include: '*' # include: health,prometheus,metrics
2. Cài đặt và Cấu hình Prometheus
2.1 Cài đặt Prometheus với HomeBrew
– Update HomeBrew
$ brew update
– Cài đặt Prometheus
$ brew install prometheus
– Tạo Job: tạo file prometheus.yml
trong thư mục /opt/homebrew/etc
với nội dung như mô tả phía dưới
$ cd /opt/homebrew/etc $ vim prometheus.yml
# prometheus.yml global: scrape_interval: 15s scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"] - job_name: 'backend-service' metrics_path: '/actuator/prometheus' static_configs: - targets: [ 'backend-service:8080' ] labels: application: 'Backend Service'
– Khởi động Prometheus
$ brew services start prometheus
– Dừng Prometheus
$ brew services stop prometheus
– Gỡ cài đặt Prometheus
$ brew uninstall prometheus
2.2 Cài đặt Prometheus với Docker compose
– Tạo file prometheus.yml
nằm trong backend-service
# prometheus.yml global: scrape_interval: 15s scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"] - job_name: 'backend-service' metrics_path: '/actuator/prometheus' static_configs: - targets: [ 'backend-service:8080' ] labels: application: 'Backend Service'
# docker-compose.yml services: prometheus: image: prom/prometheus container_name: prometheus restart: unless-stopped command: - --config.file=/etc/prometheus/prometheus.yml volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml ports: - '9090:9090' networks: default: name: api-network
➡ Xem thông tin Backend Service tại Prometheus
3. Cài đặt và Cấu hình Grafana
3.1 Cài đăt Grafana với HomeBrew
– Cập nhật HomeBrew
$ brew update
– Cài đặt Grafana
$ brew install grafana
– Khởi động Grafana
$ brew services start grafana
– Dừng Grafana
$ brew services stop grafana
– Gỡ cài đặt Grafana
$ brew uninstall grafana
3.2 Cài đăt Grafana với Docker compose
services: grafana: image: grafana/grafana container_name: grafana restart: unless-stopped environment: # account: grafana/password - GF_SECURITY_ADMIN_USER=grafana - GF_SECURITY_ADMIN_PASSWORD=password ports: - '3000:3000' links: - prometheus volumes: - grafana:/var/lib/grafana networks: default: name: api-network volumes: grafana:
3.3 Cấu hình Grafana
➡ Xem thông tin Grafana đăng nhập với tài khoản grafana/password
– Tạo Datasource
– Cấu hình Dashboard