Minimal example on how to setup Prometheus for Spring Boot
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Minimal example on how to setup Spring Boot with Prometheus

What to expect

Run a Spring Boot application an read metrics from JVM and annotated application methods using a Prometheus endpoint.


This is a working example for a minimal Spring-Boot 2.0.x app using

How to Run

To run with maven

mvn compile spring-boot:run

Open http://localhost:8080 to issue calls to be counted and to access the metrics.

Key bits and pieces

  1. Add Micrometer to the projects. Add Actuator starter to let the autoconfiguration create a Prometheus metrics endpoint.

  2. Configure some specifics
    public class MetricsApplicationConfig {
        // as of now, this aspect needs to be created manually, see
        public TimedAspect timedAspect(MeterRegistry registry) {
            return new TimedAspect(registry);
        public MetricsApplicationConfig(PrometheusMeterRegistry prometheusMeterRegistry) {
            // add existing Prometheus modules as needed
            new MemoryPoolsExports().register(prometheusMeterRegistry.getPrometheusRegistry());
            new GarbageCollectorExports().register(prometheusMeterRegistry.getPrometheusRegistry());
            new VersionInfoExports().register(prometheusMeterRegistry.getPrometheusRegistry());
  3. Collect application metrics at specific methods
    @Timed(histogram = true) // customize this to use a histogram
    public String countedCall() throws InterruptedException {
        /* ... */
        return "waited: " + delay + " ms";
    @Timed(value = "doSomething", description = "this is doing something")
    public void doSomething() {"hi");