小编典典

如何使用弹簧护套2和千分尺测量维修方法

java

我在Spring Boot 2(RC1)上开始了我的第一个项目。由于已经有了很好的文档,Spring Boot 1.x并没有使它变得很困难。

但是,现在我想要集成指标,因此我感到很沮丧。据我目前所能找到的信息,只有默认情况下随附的指标文档。但是我还想测量服务级别的执行时间以及dynamodb中使用的时间。

编辑 我正在寻找一种使用Micrometer的解决方案,该解决方案是spring-boot 2随附的新执行器库中使用的库。

是否有关于如何完成此操作的指南?从这个我读了有任意春豆没有简单的基于注解的解决方案呢。能否给我一个示例/链接到有关如何计量如下方法的文档?

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

阅读 216

收藏
2020-12-03

共1个答案

小编典典

这是一个小样本,应该可以帮助您。这里Timer.record()没有显示更多的变体。(此外:字段注入仅出于简洁目的。)您不必将调用的方法名称放入标签中。您也可以使其成为度量标准名称本身的一部分。只是想展示您可以做什么。

更新2018年3月12日: 中作为Micrometer 1.0.0一个TimedAspect已经出台,让你也可以使用@Timed注解。现在,您需要注册Bean自己。(尽管在@TimedSpring-MVC或Jersey资源上具有自定义注释时,您必须要谨慎。)[MichalStepan]在后续回答中已经提到了这一点。

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
2020-12-03