Skip to content
master
Switch branches/tags
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit 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.

Motivation

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

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.

    pom.xml
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>...</version>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>...</version>
    </dependency>
  2. Configure some specifics

    MetricsApplicationConfig.java
    @Configuration
    public class MetricsApplicationConfig {
    
        // as of now, this aspect needs to be created manually, see
        // https://github.com/micrometer-metrics/micrometer/issues/361
        @Bean
        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

    RestEndpoint.java
    @GetMapping("/countedCall")
    @Timed(histogram = true) // customize this to use a histogram
    public String countedCall() throws InterruptedException {
        /* ... */
        return "waited: " + delay + " ms";
    }
    ServiceClass.java
    @Timed(value = "doSomething", description = "this is doing something")
    public void doSomething() {
        log.info("hi");
    }

About

Minimal example on how to setup Prometheus for Spring Boot

Resources

Packages

No packages published