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

brain storm picture brain storm · Sep 3, 2015 · Viewed 8.9k times · Source

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.

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

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.

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).

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.

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).

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}}

what beans should I autowire and get these attributes for free!

Thanks

EDIT

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

Answer

Andy Wilkinson picture Andy Wilkinson · Sep 4, 2015

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.

You can do the same for /health with HealthEndpoint.