如何以编程方式从 spring-boot-actuator 获取指标?
我们在生产中有一个 spring 应用程序.它不是 Spring-boot.我发现这篇文章关于如何使用 spring-非弹簧启动应用程序中的启动执行器.
we have a spring application in production. It is NOT Spring-boot. I found this post on how to use spring-boot-actuator in a non-spring-boot application.
但是,我们的要求是聚合来自/metrics 端点的数据并对其进行一些分析并报告状态指示器.
However, the requirement for us is to aggregate the data from /metrics endpoint and do some analytics on it and report a status indicator.
例如,我们可能会使用堆参数,例如{"heap.committed":480768,"heap.init":262144,"heap.used":294461,"heap":3728384,"threads.peak":37}
表示状态应用程序 - 致命、警告或健康.
For eg, we might use heap parameter such as
{"heap.committed":480768,"heap.init":262144,"heap.used":294461,"heap":3728384,"threads.peak":37}
to indicate the status of the application - FATAL, WARN or HEALTHY.
这只是一个例子.我们的要求比较复杂.事实上,我们已经有一个 status endpoint
,我们想在其中添加更多信息(基于来自 /metrics
和 /health
端点的数据spring-boot-actuator
).
This is just an example. our requirement is more complex. In-fact, we already have a status endpoint
where we want to add more info (based on data from /metrics
and /health
endpoints of spring-boot-actuator
).
我想实现的一种方法是在应用程序中对 /metrics
和 /health
进行 REST 调用,收集数据,聚合它们并返回响应.我不认为这是推荐的方式.
One way I am thinking of acheiving this is
making REST call to /metrics
and /health
with-in the application, collect the data, aggregate them and return the response. I don't think it is a recommended way.
如果有一个可以直接提取这些参数的 bean,我会自动装配它并在需要时即时计算它们.(事实上,我会安排定期计算).
If there is a bean where I could extract these parameters directly, I would autowire it and calculate them on the fly as and when needed. (In fact, I will schedule to calculate periodically).
我对从 /metrics
返回的所有属性感兴趣.同时我也对 /health
中的以下内容感兴趣.
I am interested in all the attributes returned from /metrics
.
while I am also interested in the following from /health
.
{"diskSpace":{"status":"UP","free":386186194944,"threshold":10485760}}
我应该自动装配哪些 bean 并免费获得这些属性!
what beans should I autowire and get these attributes for free!
谢谢
编辑
这个帖子有@自动装配的 MetricRepository
.但出于某种原因,它只返回自定义计数器属性.它不返回堆、内存信息等例如:报告指标 counter.calls.get_greeting=4报告指标 counter.calls.get_greeting.1=1报告指标 counter.calls.get_greeting.2=1报告指标 counter.calls.get_greeting.3=1报告指标 counter.calls.get_greeting.4=1报告指标 counter.status.200.greeting.number=4报告指标 counter.status.404.star-star=1
This post has @Autowired MetricRepository
. But for some reason, it returning only the custom counter properties. It is NOT returning heap, memory info etc
Eg:
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
应该会给你你想要的数据.
The output from /metrics
is produced by MetricsEndpoint
. It's available as a bean that you can have @Autowired
. Calling invoke
on it should give you the data that you want.
您可以使用 HealthEndpoint
对 /health
执行相同的操作.
You can do the same for /health
with HealthEndpoint
.