Prometheus + grafana 적용기(2) - dockercompose에 AlertManager까지

조현진·2025년 5월 4일

Prometheus&Grafana

목록 보기
3/4

이제 운영중인 테스트 서버에 Prometheus와 grafana를 적용해보자.

먼저 도커 컴포즈 파일에 prometheus와 grafana를 같이 올린다.

docker-compose

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
    ports:
      - "9090:9090"
    restart: always
    networks:
      - ggogit-alpha-network

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    depends_on:
      - prometheus
    restart: always
    networks:
      - ggogit-alpha-network

prometheus.yml

그리고 프로메테우스 실행 설정파일(prometheus.yml)을 작성해주자. 이 파일은 도커 컴포즈 파일에서 명시한 경로 (위 설정에서는./monitoring/prometheus.yml)에 위치해야 제대로 컨테이너 내부에 마운트해서 사용된다. (사용을 위한 경로는 command로 명시한다.)

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "ggogit-spring-boot"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets:
          - "alpha.ggogit.io:8080"
          - "ggogit.io:8080"

이때 targets에 들어갈 문자열 배열은 반드시 host 의 형식으로 적어준다. url전체를 적는게 아님.

application.yml + actuator 의존성 추가

서버 내부에서는 actuator를 이용해 프로메테우스가 메트릭을 수집할 수 있도록 메트릭 수집용 API를 노출시켜줘야 한다. 아래처럼 진행하면 되고, 이전 포스팅에서 진행한 바와 같다.

프로젝트에 Actuator와 Prometheus 의존성 추가(gradle)

//Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
//Prometheus
implementation 'io.micrometer:micrometer-registry-prometheus'

메트릭 API 노출(application.yml 등 설정파일 설정)

management:
 endpoints:
   web:
     exposure:
       include: "health, info, metrics, prometheus"
 endpoint:
   prometheus:
     enabled: true

Alert.rules

테스트 삼아서 모든 API가 특정 시간 이상 응답이 걸릴경우 디스코드에 알림이 가도록 해보자.

지피티에게 물어본 API 별 응답 시간 한계이다.

💬 사용자 인터페이스(API/UI) > 500ms 이상 → 지연 느낌 발생 가능
📦 비동기 백엔드 처리 > 1~2초 이상 → 큐 적체 또는 병목 우려
💳 결제/인증 등 민감 API > 300ms 이상 → 신뢰성에 직접 영향
🧠 AI inference API > 3초 이상 → 일부 용인되나 너무 길면 문제
📄 배치성/통계성 API > 5초 이상 → 일반적으로 허용, 그러나 한계 있음

현재 내 사이드프로젝트는 대부분 사용자 인터페이스와 관련된 API가 위주임. 0.5초를 한계로 두고 알림을 보내도록 해보자...

docker compose에 AlertManager 추가

  alertmanager:
    image: prom/alertmanager:latest
    container_name: alertmanager
    volumes:
      - ./monitoring/alertmanager.yml:/etc/alertmanager/alertmanager.yml
    ports:
      - "9093:9093"
    command:
      - "--config.file=/etc/alertmanager/alertmanager.yml"
    restart: always
    networks:
      - ggogit-alpha-network

alertmanager.yml설정

global:
  resolve_timeout: 5m

route:
  receiver: "discord-webhook"
  group_wait: 10s
  group_interval: 30s
  repeat_interval: 1h

receivers:
  - name: "discord-webhook"
    webhook_configs:
      - url: "디스코드 웹 훅 url"

알림이 디스코드로 오게끔 하기 위해 디스코드 웹 훅 url을 추가했다.

prometheus + alertmanager

프로메테우스가 alertmanager를 인식할 수 있게 prometheus.yml에 alertmanager 설정을 추가한다.

alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - "alertmanager:9093"

이제 환경설정을 마쳤으니, 어떤 경우에 알림을 보낼지 룰을 설정하면 된다.

alert.rules 작성

이 alert.rules파일도 프로메테우스 컨테이너내에 정확히 마운트 되어야 한다.
docker-compose에서 실제 위치와 마운트 될 위치를 잘 지정해주고, prometheus.yml에서 rule파일이 있다는 것을 명시해준다.

alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - "alertmanager:9093"

rule_files:
  - "alert.rules.yml"

이후 지피티에게 부탁해서 평균 1분 동안 API응답이 0.5초 이상이 되면 알림이 오는 alert.rules를 짜달라고 했다.

groups:
  - name: all-api-latency-alerts
    rules:
      - alert: AllApiHighLatency
        expr: |
          (rate(http_server_requests_seconds_sum[1m])
           / rate(http_server_requests_seconds_count[1m]))
          > 0.5
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: "🚨 전체 API 응답 지연 경고"
          description: |
            모든 API 평균 응답 시간이 0.5초를 초과하고 있으며,
            1분 이상 지속되고 있습니다.

이제 테스트 해보자.

0개의 댓글