如何使用 spring 引导 2 和千分尺测量服务方法

How to measure service methods using spring boot 2 and micrometer

我在 Spring Boot 2 (RC1) 上开始了我的第一个项目。多亏了已经很好的文档,这并不难来自 Spring Boot 1.x.

但是,现在我想整合指标,我遇到了麻烦。据我所知,目前只有默认情况下提供的指标的文档。但我还想测量服务级别执行时间以及 dynamodb 中使用的时间。

编辑 我正在寻找使用 Micrometer 的解决方案,spring-boot 2.

附带的新执行器库中使用的库

是否有关于如何完成此操作的指南?从 this 我读到对于任意 spring beans 还没有简单的基于注释的解决方案。可以s.o。给我一个示例/link 文档,说明如何对下面的方法进行计量?

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

这里有一个小示例,应该可以帮助您开始。 Timer.record() 还有更多变体,此处未显示。 (另外:现场注入仅用于简洁。) 您不必将调用的方法名称放入标记中。您还可以将其作为指标名称本身的一部分。只是想展示你的能力。

2018-03-12 更新:Micrometer 1.0.0 起引入了 TimedAspect,因此您也可以使用 @Timed注解。现在您需要自己注册 Bean。 (尽管在 Spring-MVC 或 Jersey 资源上有自定义 @Timed 注释时,您需要谨慎。)Michal Stepan in a follow-up .

已经提到了这一点
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
由于范围缩小,

@io.micrometer.core.annotation.Timed 注释似乎无法正常调用自定义调用,其中提到 in link in your question.

您需要手动设置一个方面:

@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();
}

将在 /actuator/metrics(默认)端点中产生 GET_CARS 指标。