Spring Boot Push Micrometer Metrics to Prometheus Pushgateway

Prometheus, as a metrics solution, gets its data by actively reading it from designated services – a process known as scraping. This approach might not work if your workload contains short-lived tasks, as your task may not fall within the scraping time window.

Luckily, Prometheus has a solution for this: the Pushgateway.

It presents a push-based target for your metrics that itself is scraped by Prometheus. But how do you configure this in a Spring Boot application? Let me show you.

For a pleasant development experience, you can run a Pushgateway container in Docker on your computer. Port 9091 is the default for the Pushgateway.

docker run -d -p 9091:9091 --name=prometheus-pushgateway prom/pushgateway:latest

You can use your browser for verification by going to “localhost:9091”. After the first start, you will see an empty page.

The default view of a locally running Pushgateway service in the browser.

If you run your workloads in Kubernetes, you can install the Pushgateway using a Helm Chart.

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install [RELEASE_NAME] prometheus-community/prometheus-pushgateway

Moving on to the Spring Boot portion of this tutorial, let me start with the dependencies. First of all, you need the actuator. Additionally, you must add micrometer-registry-prometheus and the secret sauce, simpleclient_pushgateway.




Spring Boot being Spring Boot, it configures itself based on what it finds on the classpath. Therefore, the Pushgateway configuration is automatically created. To enable the pushing of metrics, you must set a few configuration values.

          enabled: true
          base-url: http://localhost:9091
          shutdown-operation: push

The value for “base-url” you see here is the default and is not required for local development. You certainly want to set this to the actual service in production. You have three options available for “shutdown-operation”. Setting it to “push” sends all the metrics to the Pushgateway when your application shuts down.

When you refresh your browser, you will first see the following.

The Pushgateway service website displays a group for all the metrics of the sample application.

When you open the group, you can see all the metrics Spring Boot generates and your custom ones. I used a timer called “workertask.runtime” for my sample application, which shows up like this.

The Pushgateway service website displays the expanded group showing the custom metrics of the sample application.

Unfortunately, I could not find a good description of all the available configuration options. Spring’s documentation on the topic is very sparse. The best I managed to dig up is the Spring configuration class PrometheusProperties.Pushgateway.

You can find a working sample project on my GitHub.

I hope this was helpful. Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.