💡 WHEN TO USE THE PUSHGATEWAY From Prometheus Docs
- Pushgateway는 Prometheus의 Pull 모델로 데이터를 수집할 수 없는 작업에서 메트릭을 푸시할 수 있도록 돕는 중간 서비스입니다.
- 예를 들어, 서비스 레벨의 배치 작업(특정 머신이나 인스턴스와 관련되지 않은 작업)에서 작업 결과를 수집하고자 할 때 유용합니다.
/build.gradle
// ... 생략 ...
dependencies {
// spring batch
implementation 'org.springframework.boot:spring-boot-starter-batch'
// prometheus 관련 라이브러리 추가
implementation 'io.micrometer:micrometer-registry-prometheus-simpleclient'
implementation 'io.prometheus:simpleclient_pushgateway'
}
// ... 생략 ...
io.micrometer:micrometer-registry-prometheus
대신에 io.micrometer:micrometer-registry-prometheus-simpleclient
를 추가해야한다.💡 Supported Monitoring Systems - Spring Boot 3.4.1
- Prometheus Pushgateway는 현재 Prometheus simpleclient만 지원하며, 이는 Prometheus 1.x 클라이언트가 Pushgateway 지원을 추가할 때까지 지속된다.
- simpleclient로 전환하려면, 프로젝트에서
io.micrometer:micrometer-registry-prometheus
를 제거하고 대신io.micrometer:micrometer-registry-prometheus-simpleclient
를 추가해야한다.
/src/main/resources/application.yml
prometheus:
push:
rate: 5000 # prometheus에 push 주기 (5초)
job:
name: batch_server_scrape # prometheus에서 인식할 pushgateway job 이름
grouping:
key: appname
pushgateway:
url: localhost:9091 # pushgateway 주소
../config/PrometheusConfig.java
@Slf4j
@Configuration
public class PrometheusConfig {
@Value("${prometheus.job.name}")
private String prometheusJobName;
@Value("${prometheus.grouping.key}")
private String prometheusGroupingKey;
@Value("${prometheus.pushgateway.url}")
private String prometheusPushGatewayUrl;
private Map<String, String> groupingKey = new HashMap<>();
private PushGateway pushGateway;
private CollectorRegistry collectorRegistry;
@PostConstruct
public void init() {
pushGateway = new PushGateway(prometheusPushGatewayUrl);
groupingKey.put(prometheusGroupingKey, prometheusJobName);
PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(DEFAULT);
collectorRegistry = prometheusMeterRegistry.getPrometheusRegistry();
Metrics.globalRegistry.add(prometheusMeterRegistry);
}
@Scheduled(fixedRateString = "${prometheus.push.rate}")
public void pushMetrics() {
try {
pushGateway.pushAdd(collectorRegistry, prometheusJobName, groupingKey);
}
catch (Throwable ex) {
}
}
}
docker-compose.yml
를 만들고, Prometheus 설정을 해보도록 하겠다.docker-compose.yml
services:
pushgateway:
image: prom/pushgateway
container_name: 'pushgateway'
ports:
- '9091:9091'
prometheus:
image: prom/prometheus
container_name: 'prometheus'
ports:
- '9090:9090'
volumes:
- ./docker-prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
image: grafana/grafana
container_name: 'grafana'
user: "$UID:$GID"
ports:
- '3000:3000'
volumes:
- ./grafana-data:/var/lib/grafana
depends_on:
- prometheus
prometheus.yml 설정 파일
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
- job_name: 'batch_server_scrape'
honor_labels: true
static_configs:
- targets: ['host.docker.internal:9091'] # pushgateway
host.docker.internal:9010
으로 기입했는데, linux 등을 다를 수 있다. 테스트 환경에 맞게 URL를 기입하도록 한다.% docker-compose up -d
Grafana 데이터 소스 설정
Grafana Dashboard 설정 및 확인
최종 Spring Batch - Grafana 모니터링 화면