How to programmatically get metrics from spring-boot-actuator?

we have a spring application. This is NOT Spring-boot. I found this post on how to use Spring-boot-actuator in a spring-boot application.

However, the requirement for us is to aggregate data from the endpoint / metric and do some analytics on it and report a status indicator.

For example, we can use a heap parameter, such as {"heap.committed":480768,"heap.init":262144,"heap.used":294461,"heap":3728384,"threads.peak":37} , to indicate application status - FATAL, WARN or HEALTHY.

This is just an example. our requirement is more complex. In fact, we already have a status endpoint where we want to add additional information (based on data from /metrics and /health spring-boot-actuator endpoints).

At one point, I think about it. making a REST call on /metrics and /health from within the application, collects the data, aggregates it and returns a response. I do not think this is the recommended way.

If there is a bean where I could directly extract these parameters, I would automatically run it and calculate them on the fly, when and when it is needed. (In fact, I plan to count periodically).

I'm interested in all the attributes returned from /metrics . while I'm interested in the following from /health .

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

that beans should be authorized and get these attributes for free!

thanks

EDIT

This post has @Autowired MetricRepository . But for some reason, it only returns custom counter properties. This does NOT return heap, memory information, etc. For example: 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

+5
source share
1 answer

Exiting from /metrics done by MetricsEndpoint . It is available as a bean that @Autowired can have. Calling invoke on it should provide you with the data you need.

You can do the same for /health with HealthEndpoint .

+13
source

All Articles