Micrometer/Prometheus如何获取记录的指标数据

huangapple go评论69阅读模式
英文:

Micrometer/Prometheus how to get recorded metric

问题

我目前正在使用`@Timed(value = "data.processing.time")`注解记录方法的执行时间但我也想读取方法的执行时间数据并将其与我在属性中设置的方法执行限制进行比较然后将数据发送到Prometheus我认为有一种方法可以从MeterRegistry中获取指标但目前无法理解如何实现是否有实现的方法

当前使用的依赖

    'io.micrometer:micrometer-registry-prometheus:1.10.4'
    'org.springframework.boot:spring-boot-starter-actuator:2.5.12'

当前用法

        @Bean
        public TimedAspect timedAspect(MeterRegistry registry) {
            return new TimedAspect(registry);
        }

  

        @Timed(value = "data.processing.time")
        private boolean process(byte[] data, String ti, Integer priority) {
            //do something
        }
英文:

I'm currently recording method's execution time using @Timed(value = "data.processing.time") annotation, but I also would love to read the method's execution time data and compare it with the method's execution limit that I want to set in my properties and then send the data to prometheus, I would assume that there is a way to get the metrics out of MeterRegistry, but currently can't get how, is there a way to do so?

Currently used dependancies:

'io.micrometer:micrometer-registry-prometheus:1.10.4'
'org.springframework.boot:spring-boot-starter-actuator:2.5.12'

Current usage:

    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }



    @Timed(value = "data.processing.time")
    private boolean process(byte[] data, String ti, Integer priority) {
        //do something
    }

答案1

得分: 1

这可能会有所帮助,看到更多你的代码,但也许你可以将其记录为直方图 - 就像这样:

methodDuration = Histogram.build()
    .name("data_processing_duration")
    .register(meterRegistry);

Histogram.Timer timer = methodDuration.startTimer();
timer.observeDuration();
英文:

It would be helpful to see more of your code, but maybe you could record it as a histogram - like this:

methodDuration = Histogram.build()
    .name("data_processing_duration")
    .register(meterRegistry);

Histogram.Timer timer = methodDuration.startTimer();
timer.observeDuration();

答案2

得分: 0

你需要创建一个 @Bean 来定义 TimedAspect。之后,@Timed 注解的工作取决于代理和方法的可见性。根据我所知,使用 JDK 代理,private 方法无法正常工作,所以首先尝试将其改为包私有:

@Timed(value = "data.processing.time")
boolean process(byte[] data, String ti, Integer priority) {
    //做一些操作
}
英文:

You need to create a @Bean of TimedAspect. After that @Timed should work depending on the proxy and method visibility. private methods do not work with the JDK proxy as far as I know so fisrt try to make it package-private:

@Timed(value = "data.processing.time")
boolean process(byte[] data, String ti, Integer priority) {
    //do something
}

huangapple
  • 本文由 发表于 2023年3月3日 22:26:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定