728x90
타이머는 @Timed라는 애노테이션을 통해 AOP를 적용할 수 있다.
OrderServiceV4
@Slf4j
@Timed(value = "my.order")
public class OrderServiceV4 implements OrderService {
private AtomicInteger stock = new AtomicInteger(100);
@Override
public void order() {
log.info("주문");
stock.decrementAndGet();
sleep(500);
}
@Override
public void cancel() {
log.info("취소");
stock.incrementAndGet();
sleep(200);
}
@Override
public AtomicInteger getStock() {
return stock;
}
private void sleep(int l) {
try {
Thread.sleep(l + new Random().nextInt(200));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
- @Timed("my.order") 타입이나 메서드 중에 적용할 수 있다. 타입에 적용하면 해당 타입의 모든 public 메서드에 타이머가 적용된다. 참고로 이 경우 getStock() 에도 타이머가 적용된다.
OrderConfigV4
@Configuration
public class OrderConfigV4 {
@Bean
public OrderService orderService() {
return new OrderServiceV4();
}
@Bean
public TimedAspect timedAspect(MeterRegistry registry){
return new TimedAspect(registry);
}
}
- TimedAspect 를 적용해야 @Timed 에 AOP가 적용된다.
ActuatorApplication - 변경
@Import(OrderConfigV4.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();
}
}
- OrderConfigV3 -> OrderConfigV4 로 변경한다.
실행
- http://localhost:8080/order: Order 메서드 실행
- http://localhost:8080/cancel: cancel 메서드 실행
- http://localhost:8080/actuator/metrics/my.order: 액츄에이터 메트릭 확인
- tag 중에 exception 이 추가 되는 부분을 제외하면 기존과 같다.
- 타이머를 사용하면 총 3가지 측정 항목이 생기는 것을 확인할 수 있다.
- http://localhost:8080/actuator/prometheus: 프로메테우스 포멧 메트릭 확인
- http://localhost:3000: 그라파나 대시보드 확인
728x90
'스프링 부트(핵심 원리와 활용)' 카테고리의 다른 글
| Ch09. 모니터링 메트릭 활용 - 실무 모니터링 환경 구성 팁 (0) | 2023.05.12 |
|---|---|
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(게이지) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(Timer) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(@Counted) (0) | 2023.05.09 |
| Ch09. 모니터링 메트릭 활용 - 메트릭 등록(카운터) (0) | 2023.05.09 |