개발일기/Server
Spring boot 에 Prometheus 연동하기
w.llama
2024. 12. 19. 18:45
필자는 IntelliJ를 사용하여 Spring boot와 Prometheus를 연동하려고한다.
관련 글을 조사해보니 application.yml을 수정하거나 혹은 Prometheus에서 Spring boot를 직접 생성하여 작업을 진행하는 글을 보았으나, 나는 이미 만들어진 SpringBoot에 Prometheus를 적용하려고한다
.yml말고 application.properties를 수정하는 방향을 채택했다.
또한 Gradle에 Prometheus의 의존성을 추가해줘야한다.해당 내용은 다음과 같다.
//prometheus 의존성 추가
runtimeOnly 'io.micrometer:micrometer-registry-prometheus' // 마이크로미터 프로메테우스 구현체
implementation 'org.springframework.boot:spring-boot-starter-actuator' // 엑츄에이터
설정을 적용하고나서, application.properties에다가 아래 내용을 추가해준다
# prometheus 설정
management.endpoints.web.exposure.include=health,metrics,prometheus
management.endpoint.health.show-details=always
management.endpoints.web.base-path=/actuator
이후 Prometheus.yml 에 Spring boot 호스트와 포트를 지정 로컬에서 진행할예정이므로 localhost:8080을 추가
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-boot-server"
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["localhost:8080"]
이후 접속을하려고하니 접근이 불가한 문제가 발생했다... 곰곰히 생각해보니 cors문제.... 포트를 열어주자 (config를 수정)
다시 서버와 프로메테우스를 열고 http://localhost:8080/actuator/prometheus 로 접속하니 드디어 연결되었다!!
다음은 이 promethues 를 grafana와 연결하여 모니터링시스템을 구축해보자