1 분 소요

개요

Micrometer

  • Micrometer
    - https://micrometer.io/
    - JVM 기반의 애플리케이션의 Metrics 제공
    - Spring Framework 5, Spring Boot 2부터 Spring의 Metrics 처리
    - Prometheus등의 다양한 모니터링 시스템 지원

  • Timer
    - 짧은 지연 시간, 이벤트의 사용 빈도를 측정
    - 시계열로 이벤트의 시간, 호출 빈도 등을 제공
    - @Timed 제공

구현

user-service

pom.xml

<!-- Micrometer -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

application.yml

management:
  endpoints:
    web:
      exposure:
        include: refresh, health, beans, busrefresh, info, metrics, prometheus # 마지막 세 개 추가

UserController.java

@RestController
@RequestMapping("/") 
public class UserController {
    ...
    @GetMapping("/health-check")
    @Timed(value = "users.status", longTask = true) // 🌟 @Timed 추가
    public String status() { ... }

    @GetMapping("/welcome")
    @Timed(value = "users.welcome", longTask = true) // 🌟 @Timed 추가
    public String welcome() { ... }
}

gateway-service

pom.xml

<!-- Micrometer -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

application.yml

management:
  endpoints:
    web:
      exposure:
        include: refresh, health, beans, busrefresh, info, metrics, prometheus # 마지막 세 개 추가

order-service

pom.xml

오더 서비스에는 이전에 actuator를 추가하지 않았기 때문에, actuator도 추가한다.

<!-- Spring Boot Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Micrometer -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

application.yml

기존에는 아래 코드가 아예 추가되어 있지 않았기 때문에, 추가한다.

management:
  endpoints:
    web:
      exposure:
        include: health, httptrace, info, metrics, prometheus

테스트

아래 순서대로 서비스들을 기동하자.

  • 유레카 서버
  • configuration 서버
  • 게이트웨이 서버
  • rabbitmq 서버
  • zipkin 서버
  • 유저 서비스
  • 오더 서비스

이제 유저 서비스에 가서 actuator에 추가한 metrics와 prometheus가 어떤 정보를 가지고 있는지 확인해보자.

먼저, 아까 @Timed 추가를 한 welcome()status()를 호출해보자.
그리고 유레카 서버에서 user-service 클릭 후, 엔드 포인트를 /actuator/metrics로 변경하면 아래와 같이 정보가 나온다.
스크린샷 2022-10-15 오전 12 48 02

위에서 확인한 것처럼 @Timed 추가한 메서드를 사용하면, metrics에 정보가 찍힌다.

이번에는 /actuator/prometheus에 접속해보자.
접속 후 users_welocme 키워드로 검색해보면 아래와 같이 몇 번 호출되었는지, 총 사용 시간 등의 정보가 나온다.
스크린샷 2022-10-15 오전 12 51 58

users_status도 마찬가지로 찾아볼 수 있다.
스크린샷 2022-10-15 오전 12 54 01



💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기