728x90
Metric types | Prometheus
An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.
prometheus.io
- 게이지는 임의로 오르내릴 수 있는 단일 숫자 값을 나타내는 메트릭
- 값의 현재 상태를 보는데 사용
- 값이 증가하거나 감소할 수 있음
- 예) 차량의 속도, CPU 사용량, 메모리 사용량
참고: 카운터와 게이지를 구분할 때는 값이 감소할 수 있는가를 고민해 보면 도움이 된다.
StockConfigV1
@Configuration
public class StockConfigV1 {
@Bean
public MyStockMetric myStockMetric(OrderService orderService, MeterRegistry registry){
return new MyStockMetric(orderService, registry);
}
@Slf4j
static class MyStockMetric{
private OrderService orderService;
private MeterRegistry registry;
public MyStockMetric(OrderService orderService, MeterRegistry registry) {
this.orderService = orderService;
this.registry = registry;
}
@PostConstruct
public void init(){
Gauge.builder("my.stock", orderService, service -> {
log.info("stock gauge call");
int stock = service.getStock().get();//stock 현재 값을 측정해야한다.
return stock;
}).register(registry);
}
}
}
- my.stock 이라는 이름으로 게이지를 등록했다.
- 게이지를 만들 때 함수를 전달했는데, 이 함수는 외부에서 메트릭을 확인할 때 마다 호출된다.
- 이 함수의 반환 값이 게이지의 값이다
ActuatorApplication - 변경
@Import({OrderConfigV4.class, StockConfigV1.class})
@SpringBootApplication(scanBasePackages = "hello.controller")
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
@Bean
public InMemoryHttpExchangeRepository httpExchangeRepository() {
return new InMemoryHttpExchangeRepository();
}
}
- OrderConfigV4 -> @Import({OrderConfigV4.class, StockConfigV1.class}) 로 변경한다.
애플리케이션을 실행하면 stock gauge call 로그가 주기적으로 남는 것을 확인할 수 있다.
게이지를 확인하는 함수는 외부에서 메트릭을 확인할 때 호출 된다. 현재 프로메테우스가 다음 경로를 통해 주기적으로 메트릭을 확인하기 때문이다.(http://localhost:8080/actuator/prometheus)
프로메테우스를 종료해 보면 해당 함수가 호출되지 않는 것을 확인할 수 있다. 물론 메트릭 확인 경로를 직접 호출하면 해당 함수가 호출된다. 카운터와 다르게 게이지는 무언가를 누적할 필요도 없고, 딱 현재 시점의 값을 보여주면 된다. 따라서 측정 시점에 현재 값을 반환한다.
실행
- http://localhost:8080/actuator/metrics/my.stock: 액츄에이터 메트릭 확인
- 게이지는 현재 값을 그대로 보여주면 되므로 단순하다.
- http://localhost:8080/actuator/prometheus: 프로메테우스 포맷 메트릭 확인
그라파나 등록 - 재고
패널 옵션
- Title : 재고
PromQL
- my_stock
게이지 단순하게 등록하기
StockConfigV2
@Slf4j
@Configuration
public class StockConfigV2 {
@Bean
public MeterBinder stockSize(OrderService orderService) {
return registry -> Gauge.builder("my.stock", orderService, service -> {
log.info("stock gauge call");
return service.getStock().get();
}).register(registry);
}
}
- MeterBinder 타입을 바로 반환해도 된다.
ActuatorApplication - 변경
@Import({OrderConfigV4.class, StockConfigV2.class})
@SpringBootApplication(scanBasePackages = "hello.controller")
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
@Bean
public InMemoryHttpExchangeRepository httpExchangeRepository() {
return new InMemoryHttpExchangeRepository();
}
}
- @Import({StockConfigV1.class, StockConfigV2.class}) -> @Import({OrderConfigV4.class, StockConfigV2.class}) 로 변경한다.
실행
- http://localhost:8080/actuator/metrics/my.stock: 액츄에이터 메트릭 확인
- 게이지는 현재 값을 그대로 보여주면 되므로 단순하다.
- http://localhost:8080/actuator/prometheus: 프로메테우스 포멧 메트릭 확인
- http://localhost:3000: 그라파나 확인
728x90
'스프링 부트(핵심 원리와 활용)' 카테고리의 다른 글
| Ch09. 모니터링 메트릭 활용 - 실무 모니터링 환경 구성 팁 (0) | 2023.05.12 |
|---|---|
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(@Timed) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(Timer) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(@Counted) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(카운터) (0) | 2023.05.09 |