Grafana and Prometheus with Spring Boot

I’m not sure how I feel about this combination. I had high hopes but it’s been frustrating to get working. I guess if I had the time it would be useful to create a slick dashboard and alerts against Spring Actuator parameters.

Let’s get started building a working example. The source code is here.

Go to Spring Initializer and create an app called dashboard-demo including these dependencies: Spring Web, Spring Boot Actuator, Lombok, and Spring Boot Dev Tools.

Replace the DashboardDemoApplication class with MovieServiceApplication. This is a simple web application we can play with.

public class MovieServiceApplication {

    public static void main(final String[] args) {, args);

    private final List<Movie> movieList = Arrays.asList(
            Movie.builder().id(1L).title("Watchmen").director("Zack Snyder").build(),
            Movie.builder().id(2L).title("The Color of Magic").director("Vadim Jean").build());

    public List<Movie> findAllBooks() {
        return movieList;

    public Movie findBook(@PathVariable final Long movieId) {
        return -> b.getId().equals(movieId)).findFirst().orElse(null);


An create a Movie class

public class Movie {
    private String director;
    private Long id;
    private String title;

Now edit src/main/resources/ to enable the actuator endpoints.

#Metrics related configurations

Now we just need to include the Prometheus dependencies that will expose the actuator values to the Prometheus server. Add these dependencies to build.gradle.

// Micormeter core dependecy
implementation 'io.micrometer:micrometer-core'
// Micrometer Prometheus registry  -->
implementation 'io.micrometer:micrometer-registry-prometheus'

Test it

Run the application

gradlew bootRun

Open a browser to http://localhost:8080/actuator and you should see this


Running Prometheus and Grafana

We will run these with docker compose. create a docker-compose.yml file.

version: '2'
    image: prom/prometheus:0.18.0
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - '-config.file=/etc/prometheus/prometheus.yml'
      - '9090:9090'
    image: grafana/grafana
      - 3000:3000

We need to provide the configuration to prometheus so create prometheus.yml like this.

  scrape_interval: 5s
    monitor: 'my-monitor'
  - job_name: 'prometheus'
      - targets: ['localhost:9090']
  - job_name: 'movie-service'
    metrics_path: '/actuator/prometheus'
      - targets: ['host.docker.internal:8080']

Now launch them

docker-compose up

Open a browser to http://localhost:9090/graph for prometheus. Go to Status > Targets to make sure the Movie-Service is being monitored.

Now open a browser to http://localhost:3000/login. The default credentials are admin:admin. Once in you will want to add Prometheus as a data source. Set the URL to http://localhost:9090 and the Access to ‘Browser’. On the Dashboards tab you can import some prebuilt Prometheus dashboards to get started.

Copyright © 2020 Code Green LLC