BlogSoftware Developmenttop

Custom Metrics With Micrometer And Prometheus using Spring Boot Actuator

By April 24, 2019 November 26th, 2019 No Comments

Spring Boot Actuator includes a number of additional features to help us monitor and manage our application when we push it to production. We can choose to manage and monitor our application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to our application.

Spring Boot Actuator uses Micrometer, an application metrics facade that supports external application monitoring systems like Prometheus, Elastic, Datadog, Graphite and many more.

Getting Started

To start using Spring Boot Actuator and Micrometer, we need to add them as dependencies to our pom.xml file:

Firstly, create a function to return distance. This function creates an equation between two points.

After dependencies are imported, Spring Boot configures PrometheusMeterRegistry that will collect and export metrics for Prometheus server.

Actuator Default Endpoints

Spring Boot Actuator endpoints let us monitor and interact with our application. It includes a number of built-in endpoints and enables us to add our own.

Here are the most common endpoints that Spring Boot Actuator offers out of the box:

/health Provides basic application health information.

/info Displays arbitrary application info.

/metrics Shows ‘metrics’ information for the current application.

/env Exposes properties from Spring’s ConfigurableEnvironment.

By default all endpoints except /shutdown endpoint are enabled. If we want to enable or disable endpoints (i.e. shutdown) we can use:

Or we can enable or disable all endpoints by default:

Also we have the option to decide which endpoints will be exposed. We can do this by using following example:

This means that health, info and metrics endpoints are exposed over HTTP and none are exposed over JMX.

By calling /actuator endpoint (i.e. http://localhost:8080/actuator) we can see all enabled endpoints.

In order to enable Prometheus to gather metrics, we need to expose /prometheus endpoint which it can use. As we can see from the top response, we have already did it using  property.

Now by calling /prometheus endpoint (i.e. http://localhost:8080/actuator/prometheus) we can see all collected metrics:

Example of custom metrics

Micrometer, as part of Spring Boot, provides a lot of default metrics, e.g. JVM metrics, CPU metrics, Tomcat metrics, but sometimes we need to collect custom metrics. In the following example DataSource status is monitored.

In order to expose DataSource custom metric, following steps are implemented:

1.Defined dataSource, meterRegistry and dataSourceStatusProbe beans (ActuatorConfig.class)

2.Defined dataSource metric collector (DataSourceStatusProbe.class)

ActuatorConfig configures all needed beans.

DataSourceStatusProbe is implementing specific logic for collecting dataSource status metrics via MeterBinde bindTo() method and depending on dataSource status () method. MeterBinders register one or more metrics to provide informations about the state of the application.

Abstract controller can be introduced in order to reuse flow logic between different probes.

Now if we call /prometheus endpoint, we will see that our DataSource status metric is also collected.

Micrometer also provides MeterFilter which can be used to decide if one or multiple metrics will be added to MetricRegistry. We can create custom filters with provided MeterFilter methods. If we add following MeterFilter bean implementation to our ActuatorConfig.class we will exclude all metrics starting with ‘tomcat’:

Now in the /prometheus response we can see that all tomcat related metrics are excluded and ignored.