yas*_*eco 5 java spring-boot micrometer spring-micrometer
我正在尝试以编程方式读取仪表,如下所示:
获取注册表:
MeterRegistry registry = Metrics.globalRegistry.getRegistries().iterator().next();
Run Code Online (Sandbox Code Playgroud)
读取测量值:
double systemCpuUsage = registry.get("system.cpu.usage").gauge().measure().iterator().next().getValue();
Run Code Online (Sandbox Code Playgroud)
问题是有时我会得到NaN.
我在文档中读到了这一点:为什么我的仪表报告 NaN 或消失?
但我不确定我该怎么做。另外,我正在阅读 Spring Boot 执行器的“内置”仪表(由 公开management.metrics.enable.process.cpu.usage=true),因此我无法更改它的构造。
在这种情况下,由于您使用的是“内置”指标,因此您可以使用自定义 MeterBinder 实现覆盖 io.micrometer.core.instrument.binder.MeterBinder#bindTo、重新定义system.cpu.usage,并将 system.cpu.usage 定义为(以及您使用的其他指标)
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);
Run Code Online (Sandbox Code Playgroud)
请参阅io.micrometer.core.instrument.binder.system.ProcessorMetrics目前定义它的示例。
ProcessorMetrics 上的 bean 是在 中定义的org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,您也需要在某处定义您的 bean。(或标记@Component)
如果您不想依赖某些以微米为单位的预定义度量,例如捕获某些自定义列表大小,那么您可以这样做。
private static Map<String, Long> strongRefGauge = new ConcurrentHashMap<>();
Run Code Online (Sandbox Code Playgroud)
要添加值,请执行以下操作
registry.gauge("CustomListSizeGuage", getMyCustomGuageTagsFor("myListName"), strongRefGauge, g -> g.get("myListName")).put("myListName", list.size());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3920 次 |
| 最近记录: |