英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论