본문 바로가기

개발 경험기

Prometheus PushGateway 사용해 Metric 수집하기

문제

특정 서비스 지표를 Prometheus에 저장해 Grafana를 이용해 모니터링하고 싶지만 환경상 Prometheus Pull 구조를 사용하지 못할 수 있다. 예를 들면, K8J Job에서 특정 로직을 실행하거나, 부하 이슈로 api 서버에서 scrapping을 할 수 없는 상황이 있을 수 있다.


Prometheus Archiectrue : https://prometheus.io/docs/introduction/overview/

 

해결책

이런 케이스에는 Pushgateway 서버를 구축해 Promethus에 Metric을 저장할 수 있다.

https://prometheus.io/docs/practices/pushing/

 

여기서 명심할 부분은 Prometheus에서는 Pull 방식을 가이드하고 있고, 어쩔 수 없는 상황에만 이용하라고 가이드하고 있다.

 

 

어쩔 수 없이 PushGateway를 사용하려고 한다면, 아래 2가지 작업이 필요하다.

  1. PushGateway 서버 구축 & Prometheus 서버에서 PushGateway 서버 scrap 하도록 설정
  2. 서비스에서 Metric을 PushGateway로 전송

 

PushGateway 서버 구축 및 Prometheus 설정

간단하게 말하면, Docker Registry에 있는 Image를 가져와 서버를 구축하면 된다.

자세한 설명은 아래 문서를 참조하면 된다.

https://github.com/prometheus/pushgateway/blob/master/README.md

 

Tips

 

- K8S 환경에 배포할시 Pod의 상태를 체크하기 위한 Health Check API도 제공해준다.

Provide a HTTP Health Check · Issue #105 · prometheus/pushgateway

- PushGateway 서버에서 서버관련 metric을 endpoin로 얻을 수 있어 grafana를 이용해 Pushgateway 서버도 모니터링 가능하다.

 

 

서비스에서 Metric을 PushGateway로 전송

Java를 이용할 경우 아래 pushgateway용 client 라이브러리가 있다. 아래 문서를 참고해 개발하면 된다.

https://prometheus.github.io/client_java/exporters/pushgateway/

 

 

Tips

1. 

Java PushGateway 객체 안에 registry가 존재한다. 해당 registry는 우리가 보통 Spring Actuator와 Prometheus를 연동해 사용하는 registry와는 별개이다.

PushGateway의 registry는 PrometheusRegistry 이고,

Spring Actuator와 연동해 사용할때는 PrometheusMeterRegistry를 사용한다.

 

2.

Counter Metric을 사용하고 싶은 경우 PushGateway가 아니라 PushGateway에게 Metric을 전송하는 서버에서 그 값을 관리해야 한다.