spring-boot - 如何以编程方式从 spring-boot-actuator 获取指标?

标签 spring-boot spring-boot-actuator

我们在生产中有一个 Spring 应用程序。它不是 Spring-boot。我找到了this post关于如何在非 spring-boot 应用程序中使用 spring-boot-actuator。

但是,我们的要求是聚合来自/metrics 端点的数据并对其进行一些分析并报告状态指示器。

例如,我们可能会使用堆参数,例如{"heap.committed":480768,"heap.init":262144,"heap.used":294461,"heap":3728384,"threads.peak":37}指示应用程序的状态 - 致命、警告或健康。

这只是一个例子。我们的要求更复杂。事实上,我们已经有一个 status endpoint我们要在其中添加更多信息(基于来自 /metrics/health 端点的数据 spring-boot-actuator )。

我正在考虑实现这一目标的一种方法是
/metrics 进行 REST 调用和 /health在应用程序中,收集数据,聚合它们并返回响应。我不认为这是推荐的方式。

如果有一个 bean 可以直接提取这些参数,我会 Autowiring 它并在需要时动态计算它们。 (事实上​​,我会安排定期计算)。

我对从 /metrics 返回的所有属性感兴趣.
而我也对 /health 中的以下内容感兴趣.

{"diskSpace":{"status":"UP","free":386186194944,"threshold":10485760}}

我应该 Autowiring 什么 bean 并免费获得这些属性!

谢谢

编辑

这个post@Autowired MetricRepository .但由于某种原因,它只返回自定义计数器属性。它不返回堆、内存信息等
例如: Reporting metric counter.calls.get_greeting=4 Reporting metric counter.calls.get_greeting.1=1 Reporting metric counter.calls.get_greeting.2=1 Reporting metric counter.calls.get_greeting.3=1 Reporting metric counter.calls.get_greeting.4=1 Reporting metric counter.status.200.greeting.number=4 Reporting metric counter.status.404.star-star=1

最佳答案

/metrics 的输出由 MetricsEndpoint 制作.它以 bean 的形式提供,您可以拥有 @Autowired .调用invoke它应该给你你想要的数据。

您可以对 /health 执行相同的操作与 HealthEndpoint .

关于spring-boot - 如何以编程方式从 spring-boot-actuator 获取指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32382349/

相关文章:

java - spring data,PagingAndSorting存储库,根据类类型的(嵌套)属性值进行排序

java - 为什么 hystrix 或任何其他微服务断路器?

spring-boot-actuator - 将 Spring 执行器插件添加到 build.gradle 会抛出 BeanCreationException -EndpointAutoConfiguration$InfoPropertiesConfiguration

java - 一个应用程序是否可以使用 Spring 框架的任何功能来检查另一个应用程序的运行状况?

java - 将 spring boot actuator 健康端点更改为自定义端点

java - 如何使用 spring boot 2 和千分尺测量服务方法

java - 如何通过环境变量设置名称中带有下划线的 Spring Boot 属性?

java - Spring 启动验证不起作用

spring-boot - Spring-Boot 配置 : How to keep whitespace in yaml key being used to populate Map<String, 字符串>

Spring Boot 2 : actuator/health endpoint is taking more time