Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 662
brain storm Avatar asked Sep 03 '15 17:09

brain storm


People also ask

What is spring boot actuator metrics?

Spring Boot Actuator provides dependency management and auto-configuration for Micrometer, an application metrics facade that supports numerous monitoring systems, including: AppOptics. Atlas. Datadog. Dynatrace.

How do you get spring boot actuator endpoints?

When we add Spring Actuator Dependencies to our spring boot project, it automatically enables actuator endpoints. Add below dependencies to your spring application to enable spring boot actuator endpoints. Now when you will run the application, you will see actuator endpoints being mapped in the logs.

What is metrics in actuator?

The /metrics endpoint shows several useful metrics information like JVM memory used, system CPU usage, open files, and much more. The /loggers endpoint shows application's logs and also lets you change the log level at runtime. Note that, every actuator endpoint can be explicitly enabled and disabled.

What value does spring boot actuator provide?

What value does Spring Boot Actuator provide? Spring Boot actuator allows you to monitor and interact with your application which is very important for a production application. Without a spring boot actuator, you need to build your own monitoring and interaction system using JMX.


1 Answers

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.

like image 106
Andy Wilkinson Avatar answered Nov 19 '22 15:11

Andy Wilkinson