如何使用弹簧靴2和千分尺测量服务方法

Chr*_*yer 14 java spring spring-boot spring-boot-actuator

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

但是现在我想整合指标我很蠢.据我目前所知,目前只有默认情况下发布的指标文档.但我还要测量服务级别执行时间以及dynamodb中使用的时间.

编辑 我正在寻找一种使用Micrometer的解决方案,Micrometer是弹簧启动2附带的新执行器库中使用的库.

有没有关于如何做到这一点的指南?从这里我读到,对于任意春豆来说,没有简单的基于注释的解决方案.可以这样给我一个示例/链接到关于如何计量下面的方法的文档吗?

@Service
@Timed
public class MyService {
    public void doSomething() {
        ...;
    }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*pan 21

@io.micrometer.core.annotation.Timed由于范围缩小,注释似乎与自定义调用无关,在您的问题的链接中提到.

您需要手动设置Aspect:

@Configuration
@EnableAspectJAutoProxy
public class AutoTimingConfiguration {
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
        }
}
Run Code Online (Sandbox Code Playgroud)

这样的方法是这样的:

@Timed("GET_CARS")
public List<Car> getCars(){
        return Lists.newArrayList();
}
Run Code Online (Sandbox Code Playgroud)

将导致(默认)端点中的GET_CARS度量/actuator/metrics.

  • 我无法使用Spring Boot 2.0.4.任何的想法? (3认同)
  • 因为没有外部调用,就没有遍历代理,也就没有aop。 (3认同)

mwe*_*uch 13

这里有一些小样本可以帮到你.这里Timer.record()没有显示更多变体.(另外:字段注入仅用于简洁.)您不必将被调用的方法名称放入标记中.您还可以将其作为度量标准名称本身的一部分.只是想表明你能做什么.

更新2018-03-12:已经介绍了Micrometer 1.0.0一个,TimedAspect以便您也可以使用@Timed注释.现在你需要Bean自己注册.(当你@Timed在Spring-MVC或Jersey资源上有自定义注释时,你需要保持谨慎.)Michal Stepan在后续回答中已经提到过这一点.

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

}
Run Code Online (Sandbox Code Playgroud)

如果你去普罗米修斯,你最终会得到类似的东西:

# 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
Run Code Online (Sandbox Code Playgroud)

  • @user_x您需要将Aspectj添加到您的依赖项中。最简单的方法是使用org.springframework.boot:spring-boot-starter-aop。 (2认同)