为什么有时读取千分尺测量结果会返回NaN? [英] Why do reading Micrometer measurement returns NaN sometimes?

查看:252
本文介绍了为什么有时读取千分尺测量结果会返回NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式读取电表,如下所示:

I'm trying to programmatically read the Meters as follows:

获取注册表:

MeterRegistry registry = Metrics.globalRegistry.getRegistries().iterator().next();

读取测量值:

    double systemCpuUsage = registry.get("system.cpu.usage").gauge().measure().iterator().next().getValue();

问题是有时我会得到NaN.

我在文档中阅读了以下内容:为什么我的量规报告了NaN或消失了?

I read about this in the docs: Why is my Gauge reporting NaN or disappearing?

但是我不确定该怎么做.另外,我正在阅读Spring Boot执行器的内置"量规(由management.metrics.enable.process.cpu.usage=true公开),因此无法更改其构造.

but I'm not sure what I shall do. Also, I'm reading the "built-in" gauge of Spring Boot actuator (which is exposed by management.metrics.enable.process.cpu.usage=true) so I cannot change it's construction.

推荐答案

在这种情况下,由于您使用的是内置"指标,因此您可以覆盖io.micrometer.core.instrument.binder.MeterBinder#bindTo,使用自定义MeterBinder实现重新定义system.cpu.usage和将system.cpu.usage定义为(以及您使用的其他对象)

In this case, since you are using a "built in" metric, you can override io.micrometer.core.instrument.binder.MeterBinder#bindTo, redefine system.cpu.usage with a a custom MeterBinder implementation, and defining system.cpu.usage as (along with others which you use)

Gauge.builder("system.cpu.usage", operatingSystemBean, x -> invoke(systemCpuUsage))
.strongReference(true)//Add strong reference
.tags(tags)
.description("The recent cpu usage for the whole system")
.register(registry);

例如,

请参阅io.micrometer.core.instrument.binder.system.ProcessorMetrics来定义它.

ProcessorMetrics上的bean是在org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration中定义的,您还需要在某个地方定义bean. (或标记@Component)

The bean on ProcessorMetrics is defined in org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration, you need to define your bean too somewhere. (or mark @Component)

如果您不想依靠某个千分尺预定义的度量标准,例如要捕获一些自定义列表大小,则可以这样做.

If you don't want to rely on some predefined metric by micrometer, like to capture some custom list size, this is what you can do.

private static Map<String, Long> strongRefGauge = new ConcurrentHashMap<>();

要添加值,请执行以下操作

For adding values do the following

registry.gauge("CustomListSizeGuage", getMyCustomGuageTagsFor("myListName"), strongRefGauge, g -> g.get("myListName")).put("myListName", list.size());

这篇关于为什么有时读取千分尺测量结果会返回NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆