programing

스프링 부트 2 및 마이크로미터를 사용하여 서비스 방법을 측정하는 방법

cafebook 2023. 2. 27. 22:22
반응형

스프링 부트 2 및 마이크로미터를 사용하여 서비스 방법을 측정하는 방법

첫 프로젝트는 스프링 부츠2(RC1)로 시작했습니다.Spring Boot 1.x 에서는 이미 좋은 매뉴얼이 준비되어 있기 때문에 그다지 어렵지 않습니다.

그러나 이제 메트릭스를 통합하고 싶기 때문에 망설이고 있습니다.현재 제가 알 수 있는 것은 기본적으로 출하된 메트릭에 대한 문서뿐입니다.그러나 dynamodb에서 사용되는 시간뿐만 아니라 서비스 수준 실행 시간도 측정하고 싶습니다.

편집 스프링 부트 2와 함께 제공되는 새 액추에이터 라이브러리에 사용되는 마이크로미터를 사용하여 해결 방법을 찾고 있습니다.

어떻게 해야 하는지에 대한 가이드가 있나요?이것으로부터, 임의의 스프링 콩에 대한 간단한 주석 베이스의 솔루션은 아직 없는 것을 알 수 있었습니다.S.O.는 다음과 같은 방법을 어떻게 미터링할 수 있는지에 대한 예/문서 링크를 제공해 주시겠습니까?

@Service
@Timed
public class MyService {
    public void doSomething() {
        ...;
    }
}

@io.micrometer.core.annotation.Timed주석은 질문 링크에서 언급되어 있는 범위 축소로 인해 커스텀콜에 적합하지 않은 것 같습니다.

Aspect를 수동으로 설정해야 합니다.

@Configuration
@EnableAspectJAutoProxy
public class AutoTimingConfiguration {
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
        }
}

이 방법은 다음과 같습니다.

@Timed("GET_CARS")
public List<Car> getCars(){
        return Lists.newArrayList();
}

결과적으로GET_CARS미터법으로 측정하다/actuator/metrics(디폴트) 엔드 포인트

여기 여러분이 진행하실 수 있는 작은 샘플이 있습니다.에는 더 많은 변형이 있습니다.Timer.record()여기에 나와 있지 않습니다.(또한:필드 주입은 간결하게 하기 위해서만 사용됩니다.호출된 메서드 이름을 태그에 입력할 필요가 없습니다.메트릭 이름 자체의 일부로 만들 수도 있습니다.네가 뭘 할 수 있는지 보여주고 싶었어

업데이트 2018-03-12: 현재Micrometer 1.0.0 a TimedAspect이 도입되어 있기 때문에,@Timed주석입니다.일단 등록이 필요합니다.Bean네 자신.(단, 커스텀이 있을 때는 주의할 필요가 있습니다.@TimedSpring-MVC 또는 Jersey 리소스에 대한 주석)이것은 Michal Stephan에 의해 이미 후속 답변에서 언급되었습니다.

package io.github.mweirauch.micrometered.eval;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.Timer.Sample;

@Configuration
@EnableAspectJAutoProxy
public class TimingStuff {

    @Service
    static class MyService {

        @Autowired
        private MeterRegistry registry;

        public void helloManual() {
            // you can keep a ref to this; ok to call multiple times, though
            Timer timer = Timer.builder("myservice").tag("method", "manual").register(registry);

            // manually do the timing calculation
            long start = System.nanoTime();
            doSomething();
            timer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
        }

        public void helloSupplier() {
            Timer timer = Timer.builder("myservice").tag("method", "supplier").register(registry);

            // execution of the method is timed internally
            timer.record(() -> doSomething());
        }

        public void helloSample() {
            Timer timer = Timer.builder("myservice").tag("method", "sample").register(registry);

            // records time taken between Sample creation and registering the
            // stop() with the given Timer
            Sample sample = Timer.start(registry);
            doSomething();
            sample.stop(timer);
        }

        // TimedAspect adds "class" and "method" tags
        @Timed(value = "myservice.aspect")
        public void helloAspect() {
            doSomething();
        }

        private void doSomething() {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                //
            }
        }

    }

    @Autowired
    private MyService myService;

    @Bean
    TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }

    @Scheduled(fixedRate = 1000)
    public void postConstruct() {
        myService.helloManual();
        myService.helloSupplier();
        myService.helloSample();
        myService.helloAspect();
    }

}

프로메테우스를 노리는 경우, 당신은 결국 다음과 같은 일이 벌어지겠지

# HELP myservice_seconds  
# TYPE myservice_seconds summary
myservice_seconds_count{application="micrometered",method="manual",} 4.0
myservice_seconds_sum{application="micrometered",method="manual",} 0.200378014
myservice_seconds_max{application="micrometered",method="manual",} 0.050115291
myservice_seconds_count{application="micrometered",method="supplier",} 4.0
myservice_seconds_sum{application="micrometered",method="supplier",} 0.200393455
myservice_seconds_max{application="micrometered",method="supplier",} 0.05011635
myservice_seconds_count{application="micrometered",method="sample",} 4.0
myservice_seconds_sum{application="micrometered",method="sample",} 0.200527005
myservice_seconds_max{application="micrometered",method="sample",} 0.050250191
# HELP myservice_aspect_seconds  
# TYPE myservice_aspect_seconds summary
myservice_aspect_seconds_count{application="micrometered",class="io.github.mweirauch.micrometered.eval.TimingStuff$MyService",method="helloAspect",} 4.0
myservice_aspect_seconds_sum{application="micrometered",class="io.github.mweirauch.micrometered.eval.TimingStuff$MyService",method="helloAspect",} 0.201824272
myservice_aspect_seconds_max{application="micrometered",class="io.github.mweirauch.micrometered.eval.TimingStuff$MyService",method="helloAspect",} 0.051014296

언급URL : https://stackoverflow.com/questions/48704789/how-to-measure-service-methods-using-spring-boot-2-and-micrometer

반응형