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.

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.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
</dependency>
</dependencies>
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.
management:
metrics:
export:
prometheus:
pushgateway:
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.

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.

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.