【问题标题】:Spring-boot micrometer timer() how does it work?Spring-boot千分尺定时器()它是如何工作的?
【发布时间】:2019-01-27 21:19:28
【问题描述】:

我不熟悉使用 spring-boot 指标并从千分尺开始。我找不到在我的 spring-boot 应用程序中执行计时器指标的好例子(事实上它是新的)。我正在使用 spring-boot-starter-web:2.0.2.RELEASE 依赖项。 但是运行 spring-boot 服务器并启动 jconsole,我没有看到它显示 Metrics (MBeans),所以我还明确包含了以下依赖项:

spring-boot-starter-actuator:2.0.2.RELEASE

还有千分尺依赖性:'io.micrometer:micrometer-registry-jmx:latest' 添加执行器后,它确实显示 Metrics 文件夹,但我没有在列表中看到我的计时器(app.timer)属性。难道我做错了什么?任何建议表示赞赏!

下面的代码sn-p:

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

【问题讨论】:

    标签: java spring-boot spring-boot-actuator micrometer


    【解决方案1】:

    Spring Boot Micrometer 输出定时器信息的另一种方法是在要跟踪执行的方法中添加注解io.micrometer.core.annotation.Timed,并在ApplicationContext类中添加,或者在任何具有@Configuration注解的类中添加,方法如下:

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

    另请参阅此答案:https://stackoverflow.com/a/49193908/5538923

    【讨论】:

    • 这不允许在执行中添加动态标签,但是
    【解决方案2】:

    这可能是。如果您使用的是 Spring Boot 2,只需在您想要的任何地方调用 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());
    }
    

    【讨论】:

    • 这是我正在寻找的示例,关于如何不仅使用 Metrics.timer() (我通过 Spring Boot 2.3.4 中方法上的注释 @Timer 使用它,因为它对我来说更简单),还有 Metrics.summary(),我用它登录 Prometheus 的详细数据的大小。
    【解决方案3】:

    请参阅文档的this part。我对其进行了调整,使其与您想要的更相似。您只需使用AutoConfigured MetricRegistry 注册您的Timer

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

    【讨论】:

    • 感谢 dovmo 的回复。我会检查一下。但是我可以在不创建注册表的情况下使用 timer() Metric 吗?像 Metrics.timer().record() 这样的东西。我不认为我想创建自定义注册表。我只是希望能够为我的应用记录计时器指标。
    • 啊,我明白了。让我修改一下。您想使用自动配置的
    • 所以我可以使用上面的代码(在帖子末尾编辑),请检查。在 JMX 控制台中,我现在可以看到指标详细信息,但没有可视化图表,因此不确定是否有更好的方法可以直观地理解这些指标。
    • 太棒了!如果您可以投票并选择我的答案是正确的,那就太好了。在可视化方面,JMX 只会给你瞬时值。其他监控系统插入 JMX 以进行可视化。请参阅此处了解您可以使用(即使只是在本地)查看该数据的监控工具列表:micrometer.io/docs; Atlas、Datadog 等
    • 我尝试投票,但不幸的是,由于我的声誉较低,它可能不会显示。尽管根据我收到的通知,它已被记录下来。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 2020-02-17
    • 2020-03-15
    • 2019-02-22
    • 2020-01-07
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多