Spring boot 弹簧靴测微计计时器()它是如何工作的?

Spring boot 弹簧靴测微计计时器()它是如何工作的?,spring-boot,spring-boot-actuator,micrometer,Spring Boot,Spring Boot Actuator,Micrometer,我刚开始使用spring boot metrics,从测微计开始。我在我的spring boot应用程序中找不到执行计时器度量的好例子(事实上它是新的)。我使用的是SpringBootStarterWeb:2.0.2.0版本依赖关系。 但在运行spring boot server和启动jconsole时,我没有看到它显示指标(MBean),所以我还明确地包含了以下依赖项: spring-boot-starter-actuator:2.0.2.RELEASE 还有测微计依赖项:“io.测微计:测

我刚开始使用spring boot metrics,从测微计开始。我在我的spring boot应用程序中找不到执行计时器度量的好例子(事实上它是新的)。我使用的是SpringBootStarterWeb:2.0.2.0版本依赖关系。 但在运行spring boot server和启动jconsole时,我没有看到它显示指标(MBean),所以我还明确地包含了以下依赖项:

spring-boot-starter-actuator:2.0.2.RELEASE
还有测微计依赖项:
“io.测微计:测微计注册表jmx:最新”
添加执行器后,它会显示Metrics文件夹,但我在列表中看不到我的计时器(app.timer)属性。我做错什么了吗?任何建议,谢谢

下面是代码片段:

MeterRegistry registry = new CompositeMeterRegistry();
long start = System.currentTimeMillis();
Timer timer = registry.timer("app.timer", "type", "ping");
timer.record(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
这项工作:

Metrics.timer("app.timer").record(()-> {

 didSomeLogic;
 long t = timeOccurred - timeScheduled;
 LOG.info("recorded timer = {}", t);
});
请参阅文档的一部分。我把它调整得更像你想要的。您只需使用
自动配置的
MetricRegistry
注册您的
计时器

@Component
public class SampleBean {

    private final Timer timer;

    public SampleBean(MeterRegistry registry) {
        long start = System.currentTimeMillis();
        this.timer = registry.timer("app.timer", "type", "ping");
    }

    public void handleMessage(String message) {
        timer.record(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
    }

}

这可能是。如果您使用的是SpringBoot2,只需在任何地方调用Timer,无需使用构造函数

public void methodName(){
    //start
    Stopwatch stopwatch = Stopwatch.createStarted();// Google Guava
    
    // your job here
         
    // check time
    Metrics.timer("metric-name",
            "tag1", this.getClass().getSimpleName(), //class
            "tag2", new Exception().getStackTrace()[0].getMethodName()) // method 
            .record(stopwatch.stop().elapsed());
}

使用弹簧靴测微计输出计时器信息的另一种方法是将注释
io.dimenomer.core.annotation.Timed
添加到要跟踪其执行的方法中,并在ApplicationContext类或具有
@配置
注释的任何类中添加以下方法:

@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
    return new TimedAspect(registry);
}
在此之后,一个工作示例是:

@PostMapping(value = "/save", produces = "application/json")
@Timed(description = "Time spent saving results")
public ResponseEntity<Integer> saveResults(@CookieValue(name = "JSESSIONID") String sessionId) {
    return new ResponseEntity<>(importer.saveResults(sessionId), HttpStatus.OK);
}
@PostMapping(value=“/save”,products=“application/json”)
@定时(description=“保存结果所花费的时间”)
public ResponseEntity saveResults(@CookieValue(name=“JSESSIONID”)字符串sessionId){
返回新的响应属性(importer.saveResults(sessionId),HttpStatus.OK);
}

另请参见以下答案:。

感谢dovmo的回复。我来看看。但是我可以在不创建注册表的情况下使用timer()度量吗?类似Metrics.timer().record()的内容。我不想创建自定义注册表。我只想能够记录我的应用程序的计时器指标。啊,我明白了。让我修改一下。如果您想使用自动配置的代码,那么我可以使用上面的代码(在文章末尾编辑),请检查。在JMX控制台中,我现在可以看到度量的详细信息,但没有视觉图形,因此不确定是否有更好的方法可以直观地理解这些度量。太好了!如果你能投票并选择我的答案是正确的,那就太好了。在可视化方面,JMX只提供瞬时值。其他监控系统插入JMX进行可视化。请参见此处,以获取可用于查看该数据的监视工具列表(即使仅在本地):;Atlas、Datadog等。我试图提高投票率,但不幸的是,因为我的声望较低,所以可能无法显示。这是根据我收到的通知记录的。谢谢这就是我正在寻找的示例,它不仅涉及如何使用Metrics.timer()(我通过Spring Boot 2.3.4中方法上的注释@timer使用它,因为它对我来说更简单),还涉及如何使用Metrics.summary(),我使用它登录Prometheus,以确定详细数据的大小。