일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- laravel
- jenkins
- ReactJS
- elasticsearch
- db
- java
- 요리
- it
- Spring Boot
- Gradle
- JVM
- Git
- Web Server
- MySQL
- 맛집
- Spring Batch
- AWS
- Oracle
- php
- ubuntu
- Spring
- javascript
- devops
- Design Patterns
- linux
- springboot
- tool
- jsp
- redis
- IntelliJ
Archives
- Today
- Total
아무거나
[Spring Actuator] Example 6편 - Timer 본문
반응형
이전글: https://bkjeon1614.tistory.com/785
- Timer
- 시간을 측정하는 metric
- 샘플
- Timer
[TimerConfig.java]
[TimerController.java]package com.example.bkjeon.base.config.actuator.timer; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @RequiredArgsConstructor public class TimerConfig { private final MeterRegistry meterRegistry; @Bean public Timer myTimer() { return Timer.builder("my.timer").register(meterRegistry); } }
package com.example.bkjeon.base.services.api.v1.actuator.controller; import io.micrometer.core.instrument.Timer; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("v1/timers") @RequiredArgsConstructor public class TimerController { private final Timer timer; @GetMapping public String getTimer() { timer.record(() -> { // 시간을 재고 싶은 로직을 넣는다. try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); return HttpStatus.OK.getReasonPhrase(); } }
- /api/v1/timers/timer1 호출 후 api/actuator/metrics/my.timer 를 확인
- Timer.Sample
[TimerController.java]... private final MeterRegistry meterRegistry; ... @GetMapping("timer2") public String getTimer2() throws InterruptedException { Timer.Sample sample = Timer.start(meterRegistry); // logic Thread.sleep(4000); sample.stop(meterRegistry.timer("my.timer2")); return HttpStatus.OK.getReasonPhrase(); }
- /api/v1/timers/timer2 호출 후 /api/actuator/metrics/my.timer2 를 확인
- @Timed
[TimedConfiguration.java]
[TimerController.java]package com.example.bkjeon.base.config.actuator.timer; import io.micrometer.core.aop.TimedAspect; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TimedConfiguration { // @Timed 를 사용하기위한 Aspect 등록 @Bean public TimedAspect timedAspect(MeterRegistry meterRegistry) { return new TimedAspect(meterRegistry); } }
@Timed("my.timer4") @GetMapping("timer3/{sleepSecond}") public String getTimer3(@PathVariable("sleepSecond") int sleepSecond) throws InterruptedException { Thread.sleep(sleepSecond * 1000); return HttpStatus.OK.getReasonPhrase(); }
- /api/v1/timers/timer3/3 호출 후 /api/actuator/metrics/my.timer3 를 확인
- FunctionTimer
[MyTimerManager.java]
[FunctionTimerConfig.java]package com.example.bkjeon.base.services.api.v1.actuator.service.timer; import org.springframework.stereotype.Service; @Service public class MyTimerManager { public long getCount() { return System.currentTimeMillis(); } public long getTotalTime() { return System.currentTimeMillis() * 2; } }
package com.example.bkjeon.base.config.actuator.timer; import com.example.bkjeon.base.services.api.v1.actuator.service.timer.MyTimerManager; import io.micrometer.core.instrument.FunctionTimer; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class FunctionTimerConfig { @Bean public FunctionTimer myFunctionTimer(MyTimerManager myTimerManager, MeterRegistry meterRegistry) { FunctionTimer functionTimer = FunctionTimer.builder( "my.timer5", myTimerManager, value -> { return value.getCount(); }, value -> { return value.getTotalTime(); }, TimeUnit.SECONDS ) .register(meterRegistry); return functionTimer; } }
- /actuator/metrics/my.timer5 호출하면 확인할 수 있다.
- MeterBinder
[TimerConfigWithMeterBinder.java]package com.example.bkjeon.base.config.actuator.timer; import com.example.bkjeon.base.services.api.v1.actuator.service.timer.MyTimerManager; import io.micrometer.core.instrument.FunctionTimer; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.MeterBinder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class TimerConfigWithMeterBinder { @Bean public MeterBinder myTimerMeterBinder(MyTimerManager myTimerManager) { return new MeterBinder() { @Override public void bindTo(MeterRegistry registry) { FunctionTimer functionTimer = FunctionTimer.builder( "my.timer6", myTimerManager, value -> { return value.getCount(); }, value -> { return value.getTotalTime(); }, TimeUnit.SECONDS ) .register(registry); } }; } }
- Timer
반응형
'Java & Kotlin > Spring' 카테고리의 다른 글
@Async 를 활용한 비동기 호출 구현 (0) | 2024.10.28 |
---|---|
[Spring Actuator] Aspect 를 활용한 특정 metric 의 tag 의 여러 value 값 별로 시간단위로 실시간 카운트 저장 (0) | 2023.12.04 |
[Spring Actuator] Example 5편 - Gauge (2) | 2023.11.27 |
[Spring Actuator] Example 4편 - Metrics Tags (0) | 2023.11.27 |
[Spring Actuator] Example 3편 - Counter (0) | 2023.11.26 |
Comments