그라파나 설정하기 까지

PUROMANGA·2025년 6월 18일

기술스택

목록 보기
20/22

개요

Spring Boot Actuator, Micrometer, Prometheus를 가지고 그라파나를 설정할 수 있게끔 할 거임.

출처

https://rachel0115.tistory.com/entry/Prometheus-Grafana-%EB%AA%A8%EB%8B%88%ED%84%B0%EB%A7%81-%EC%8B%9C%EC%8A%A4%ED%85%9C-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

✅ 1. Spring Boot Actuator

  • 📌 역할:
    스프링 부트 애플리케이션의 상태를 실시간으로 확인할 수 있는 엔드포인트들을 자동으로 제공함.
  • 📚 주요 기능:
    /actuator/health → 시스템 상태 확인
    /actuator/metrics → 다양한 메트릭(메모리 사용량, GC 횟수 등)
    /actuator/env → 환경 변수 조회
    /actuator/beans → 빈 정보 확인 등

🛠 설정 예시

management:
  endpoints:
    web:
      exposure:
        include: health, metrics, prometheus

만약에 false로 안 가져와도 되는 서브파티가 있다면

이렇게

management:
  endpoints:
    web:
      exposure:
        include: health, metrics, prometheus
  server:
    port: 9292
    
  health:
    elasticsearch:
      enabled: false

https://velog.io/@zenon8485/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B6%80%ED%8A%B8-%EC%95%A1%EC%B6%94%EC%97%90%EC%9D%B4%ED%84%B0

✅ 2. Micrometer

  • 📌 역할
    JVM 기반 애플리케이션의 성능 데이터를 수집하고, 다양한 백엔드(예: Prometheus, Datadog, New Relic 등)로 보낼 수 있게 해주는 계측 라이브러리.
  • 📚 Micrometer가 수집하는 데이터 예시:
    CPU 사용률
    메모리 사용량
    HTTP 요청 수
    응답 시간
    커스텀 메트릭(예: 서비스별 주문 수)

🔧 커스텀 메트릭 예시

@Autowired
private MeterRegistry meterRegistry;

@PostConstruct
public void init() {
    meterRegistry.counter("my.custom.counter").increment();
}

✅ 3. Prometheus

  • 📌 역할
    Micrometer로 수집된 메트릭을 스크래핑(pull) 해서 저장하고, 시계열로 조회하거나 알람을 설정할 수 있는 모니터링 서버.

🔧 prometheus.yml 설정

https://rachel0115.tistory.com/entry/Prometheus-Grafana-%EB%AA%A8%EB%8B%88%ED%84%B0%EB%A7%81-%EC%8B%9C%EC%8A%A4%ED%85%9C-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

https://motti.tistory.com/entry/%ED%94%84%EB%A1%9C%EB%A9%94%ED%85%8C%EC%9A%B0%EC%8A%A4Prometheus-%EA%B8%B0%EB%B3%B8-%EC%82%AC%EC%9A%A9%EB%B2%95

https://alswns7984.tistory.com/63

# 📌 전역 설정
global:
  scrape_interval: 15s        # 메트릭을 수집할 기본 주기 (15초마다)
  evaluation_interval: 15s    # 알림 규칙 등을 평가하는 주기 (15초마다)

# 📌 Alertmanager 설정 (알림을 위한 구성, 지금은 비어 있음)
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093  # 알림 전송 대상 (지금은 비활성화됨)

# 📌 규칙 파일 (알림 조건 등을 정의하는 rule 파일, 지금은 사용 안 함)
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# 📌 메트릭 수집 대상 (scrape 설정)
scrape_configs:
  # Prometheus 자기 자신을 모니터링하는 job
  - job_name: "prometheus"    # job 이름 (시계열 데이터에 라벨로 들어감)

    # 기본 경로는 '/metrics'
    # 기본 프로토콜은 'http'

    static_configs:
      - targets: ["localhost:9090"]  # 수집할 대상 (Prometheus 서버 자신)

  # 별도 job 설정 (예: 다른 애플리케이션)
  - job_name: "job_onl"

    # 기본 경로는 '/metrics'

    static_configs:
      - targets: ["localhost:9219"]  # 수집할 대상 (9219 포트에서 메트릭 제공 중인 앱)

근데 우리는 지금 필요 없으니까

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: "spring-boot-app"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["host.docker.internal:9292"]  # 또는 localhost:8080 등 실제 actuator 포트

0개의 댓글