Web/환경설정 관련

스프링 부트 모니터링 (3) - 프로메테우스와 그라파나 활용하기

now0204 2024. 5. 14. 18:23

 

프로메테우스란?

  • 시간이 지남에 따라 추이가 변하는 데이터를 메트릭이라고 한다. 
  • 대표적으로 CUP 사용량, 메모리 사용량등을 생각할 수 있는데, SpringBoot는 메트릭의 수집을
  • MicroMeter 모듈을 내장한 엑추에이터가 담당한다.
  • 이때, 메트릭은 시간이 흐를수록 계속 쌓이게 되고, Application 메모리가 증가하여, 지속적으로 쌓이는 메트릭을 모두 커버할 수 없다. 
  • 따라서 메트릭 전용 외부 DB를 연결해 두는데, 이것이 프로메테우스이다. 
  •  
    https://lordofkangs.tistory.com/327
  • 프로메테우스는 마이크로미터가 수집한 메트릭 데이터를 일정시간 간격으로 PULL해서 DB에 수집한다.
  • 이때, MicroMeter Prometheus구현체가 필요하다. 왜냐면, 마이크로미터는 수집한 메트릭을 JSON 형식으로 전달하지만,
  • 프로메테우스는 JSON 데이터를 처리하지 못하기 때문이다. 따라서 중간에 메트릭을 변환해 줄 구현체가 필요하고, 이 역할을 MicroMeter Prometheus가 수행한다. 

그라파나란?

  • 오픈소스 메트릭 데이터 시각화 도구이다. 메트릭 분석 플랫폼으로 사용된다. 
  • 다양한 데이터 소스를 지원하고, 여러 시각화 대시보드를 제공하며, 경고 및 알림 기능을 제공한다. 
  • 이외에도 다양한 장점을 가지고 있다. 

1. 측정 지표 프로메테이스로 수집

 

 

Download | Prometheus

An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.

prometheus.io

  • 다운 받아서 압축을 푼 후에, prometheus.exe를 실행하자
  • 후에 localhost:9090으로 접속하면 프로메테우스 대시보드에 접속할 수 있다. 

  • 프로메테우스 설치하고 압축을 푼 폴더에서 prometheus.yml파일을 찾아서 수정한다.
  • 프로메테우스가 http://localhost:8080/actuator/prometheus와 같은 특정 경로 바라보도록 설정하는 단계이다.
#아래 내용 맨 밑에 추가
- job_name: "spring-actuator"
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']
  • 전체 설정 예시는 아래와 같다
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']
  • 위와 같이 입력 완료 후에 다시 프로메테우스를 키고 localhost:9090으로 이동해서
  • Status -> Targets에 들어가면 아래와 같이 잘 연결 된 것을 볼 수 있다.

  • 프로메테우스 DB에 쿼리를 한번 날려보자 
  • http_server_requests_seconds_count{uri="/actuator/prometheus"}
  • 해당 경로로 http request가 요청한 횟수를 반환한다 (프로메테우스도 계속 요청을 날리기 때문에 꽤 많이 찍힌다)
  • 그래프 탭을 통해 위와 같이 그래프 형식으로 볼 수 도 있다. 

 

 

Querying basics | Prometheus

An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.

prometheus.io


 

 

2. 그라파나 설치 

 

 

Download Grafana | Grafana Labs

Overview of how to download and install different versions of Grafana on different operating systems.

grafana.com

  • 아래 사이트에 들어가서 OS에 맞는 그라파나를 설치한다.
  • 설치 완료 후 localhost:3000포트에 접속하면 그라파나 대시보드를 확인할 수 있다.

  • admin, admin을 입력하고, 비밀번호를 알맞게 다시 설정하자 

  • 이와 같은 그라파나 홈에 접속할 수 있다.

3. 그라파나 프로메테우스 연동 

 

  • Add your first data source를 클릭 
  • 프로메테우스 선택 

  • 일단 설정 기본값으로 두고, 프로메테우스 url만 잘 입력한 후 save&Test를 누르자 

  • 다음으로 대시보드를 생성해보자 

  • 대시보드 생성을 누른 다음에 프로메테우스를 선택하면 

  • 아래와 같은 빈 화면이 나온다.

  • 오른쪽 최상단에 Time series를 클릭하면, 보고 싶은 시각화 좌표를 선택할 수 있다.
  • 그 하단에 Panel options에서 해당 대시보드에 대한 정보를 적을 수 있다.
  • 화면 왼쪽에 
  • 밑에 메트릭을 고를 수 있다.  메트릭을 고른 후 쿼리를 작성할 수 있고,
  • 옵션을 누르면, Format도 지정할 수 있다. 모두 지정한 후에 run queries를 실행하면, 시각화를 시작한다. 
  • 대시보드 구성 완료 후에 save를 하면, 해당 지표를 추적하고, 시각화하는 대시보드 완성이다! 

 

대시보드 구성에 대한 더 자세한 사항은 

https://grafana.com/docs/grafana/latest/getting-started/build-first-dashboard/

 

Build your first dashboard | Grafana documentation

Thank you! Your message has been received!

grafana.com

공식 문서를 참고하자

 


참고자료 

https://hstory0208.tistory.com/entry/Grafana-%EA%B7%B8%EB%9D%BC%ED%8C%8C%EB%82%98-%ED%94%84%EB%A1%9C%EB%A9%94%ED%85%8C%EC%9A%B0%EC%8A%A4-%EC%97%B0%EB%8F%99-%EB%B0%A9%EB%B2%95

 

[Grafana] 그라파나 - 프로메테우스 연동 방법

그라파나는 프로메테우스를 통해서 데이터를 조회하고 보여주는 역할을 한다. 그렇기 때문에 먼저 프로메테우스가 실행되어 있어야 한다. 1. 그라파나 대쉬보드로 이동한다. http://localhost:3000 대

hstory0208.tistory.com

https://lordofkangs.tistory.com/327

 

[Prometheus] 프로메테우스 연동하기 ( With SpringBoot )

프로메테우스(Prometheus)란? 시간이 지남에 따라 추이가 변하는 데이터를 메트릭(Metric)이라고 한다. CPU사용량, 메모리 사용량 등이 메트릭에 해당한다. SpringBoot는 Metric 수집을 마이크로미터(MicroMet

lordofkangs.tistory.com

https://www.yes24.com/Product/Goods/122002340

 

실전 스프링 부트 - 예스24

인류에겐 이런 스프링 부트 가이드북이 필요했다방대한 스프링 부트 공식 문서 중 실무에서 잘 쓰이는 팁을 찾기란 어렵다. 이 책은 초급에서 중급 수준의 독자를 대상으로 스프링 부트의 기본

www.yes24.com