Prometheus는 오픈소스 메트릭 수집 및 경고 시스템이며, Grafana는 메트릭 시각화 대시보드 플랫폼입니다. 두 도구는 함께 사용되어 인프라, 애플리케이션, AI/ML 파이프라인까지 실시간 모니터링을 제공합니다.
Prometheus가 대상 엔드포인트에서 HTTP로 메트릭을 주기적으로 가져옵니다. (Pull 방식)
시계열 데이터베이스(TSDB)에 저장하며, PromQL로 유연하게 질의 가능.
Grafana 대시보드로 메트릭을 시각화하고, Alertmanager로 알림 전송.
기존 모니터링 대안 (Nagios, Zabbix, CloudWatch)과 비교한 핵심 차이점:
| 기능 | Prometheus + Grafana | Nagios / Zabbix | CloudWatch (AWS) |
|---|---|---|---|
| 데이터 모델 | 시계열 (레이블 기반) | 트리 기반 | 시계열 (제한적 레이블) |
| 질의 언어 | PromQL (강력한 집계) | 제한적 | CloudWatch Metrics Insights |
| Pull vs Push | Pull (기본) | Push / Agent | Push (API) |
| 시각화 | Grafana (탁월한 대시보드) | 내장 (제한적) | CloudWatch Dashboards |
| 오픈소스 | 완전 오픈소스 | 부분 유료 | 독점 |
Prometheus 아키텍처는 아래와 같이 단순하면서도 강력합니다.
rate(http_requests_total[5m]) 같은 강력한 시계열 질의.로컬 환경에서 Prometheus + Grafana를 빠르게 시작합니다. (Python 예제 포함)
# docker-compose.yml (최신 버전) $ cat <<EOF > docker-compose.yml version: '3.8' services: prometheus: image: prom/prometheus:latest ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml grafana: image: grafana/grafana:latest ports: - "3000:3000" environment: - GF_SECURITY_ADMIN_PASSWORD=admin EOF $ docker compose up -d
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'python-app'
static_configs:
- targets: ['host.docker.internal:8000']
# app.py (pip install prometheus-client)
from prometheus_client import start_http_server, Counter, Histogram
import time, random
REQUEST_COUNT = Counter('app_requests_total', 'Total requests')
REQUEST_DURATION = Histogram('app_request_duration_seconds', 'Request duration')
def process():
with REQUEST_DURATION.time():
time.sleep(random.uniform(0.1, 0.5))
REQUEST_COUNT.inc()
if __name__ == '__main__':
start_http_server(8000)
while True:
process()
time.sleep(1)
$ python app.py &
# 1. http://localhost:3000 (admin/admin) # 2. Configuration → Data Sources → Add → Prometheus # 3. URL: http://prometheus:9090 (docker network) 또는 http://localhost:9090 # 4. Save & Test → 대시보드 생성
| 설정 항목 | 설명 | 예시 |
|---|---|---|
scrape_interval | 메트릭 수집 주기 | 15s |
evaluation_interval | 규칙 평가 주기 (알림) | 15s |
static_configs | 대상 목록 직접 지정 | targets: ['localhost:9090'] |
relabel_configs | 레이블 재작성 (필터링/변경) | source_labels: [__meta_ec2_tag_Name] |
alerting | Alertmanager 엔드포인트 | alertmanagers: [...] |
rule_files | 알림 규칙 파일 경로 | ['alerts.yml'] |
Grafana에서는 Datasource 설정과 Dashboard provisioning (YAML)을 통해 자동화 가능.
| 문제 | 원인 | 해결책 |
|---|---|---|
| 메트릭이 안 보임 | Scrape 대상이 /metrics 제공 안 함 | curl http://target:port/metrics 확인 |
| Grafana "No data" | 데이터소스 시간 범위/레이블 불일치 | Prometheus 탐색기에서 PromQL 테스트 |
| 알림이 안 옴 | Alertmanager 설정 누락 | amtool check-config 로 검증 |
| 디스크 사용량 급증 | TSDB 보존 기간이 김 | --storage.tsdb.retention.time=30d |
| 카디널리티 폭발 | 레이블 값이 무한히 증가 | 레이블 제한, __tmp_* 사용, relabel로 정리 |
--web.enable-lifecycle 옵션으로 설정 리로드 (curl -X POST localhost:9090/-/reload). Grafana는 Provisioning 폴더로 대시보드와 데이터소스를 코드 관리하세요.