아무거나

[Spring Actuator] Example 6편 - Timer 본문

Java & Kotlin/Spring

[Spring Actuator] Example 6편 - Timer

전봉근 2023. 11. 30. 14:32
반응형

이전글: https://bkjeon1614.tistory.com/785

 

[Spring Actuator] Example 5편 - Gauge

이전글: https://bkjeon1614.tistory.com/784 Gauge Counter 타입인 경우 increment 메서드만 제공되지 decrement 메서드가 존재하지 않는다. 그러므로 커졌다 작아졌다 하는 값으로는 Gauge 를 사용하면 된다. 또한 Co

bkjeon1614.tistory.com

 

 

  • Timer
    • 시간을 측정하는 metric
    • 샘플
      • Timer
        [TimerConfig.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);
            }
        
        }
        
        [TimerController.java]
        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]
        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);
            }
        
        }      
        
        [TimerController.java]
        @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]
        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;
            }
        
        }
        
        [FunctionTimerConfig.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 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);
                    }
                };
            }
        
        }
        
반응형
Comments