x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Spring Boot Micrometer Prometheus example

    Spring Boot Actuator offers basic metrics but Micrometer provides a way to collect the metrics, acts as a facade for these metrics and intergate them with any other third party system like Prometheus, Netfloix Atlas for monitoring. You can also write custom metrics using Micrometer.

    Prometheus is a time-series metrics monitoring tool. In this example we will use the actuator metrics to be collected from Micrometer and then use Prometheus to read the metrics and create graph to monitor the application health.

    Step 1) Add below dependencies to pom.xml

        <!-- Micrometer Prometheus registry -->

    Step 2) Create Player, PlayerController, PlayerService PlayerApplication classes

    package com.example.demo;
    public class Player {
        private String id;
        private String firstName;
        private String lastName;
        public Player() {
        public Player(String id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        //removed getter & setter
        public String toString() {
            return "Player [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";

    package com.example.demo;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    public class PlayerApplication {
        public static void main(String[] args) {
            SpringApplication.run(PlayerApplication.class, args);
        public ApplicationRunner playerInitializer(PlayerService playerService) {
            return args -> {
                playerService.create(new Player("leo", "Leonardo ", "Messi"));
                playerService.create(new Player("cristi", "Cristiano", "Ronaldo"));
                playerService.create(new Player("diego", "Diego", "Maradona"));

    package com.example.demo;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    public class PlayerController {
        private PlayerService playerService;
        private static Logger logger = LoggerFactory.getLogger(PlayerController.class);
        public Player get(@PathVariable("id") String id) throws Exception {
            logger.info("getting player");
            return playerService.get(id);

    package com.example.demo;
    import java.util.HashMap;
    import java.util.Map;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    public class PlayerService {
        private static Logger logger = LoggerFactory.getLogger(PlayerService.class);
        private Map<String, Player> players = new HashMap<>();
        public Player get(String id) {
            logger.info("getting player");
            return players.get(id);
        public void create(Player player) {
            players.put(player.getId(), player);

    Step 3) Expose prometheus as actuator endpoint in application.properties file as shown below


    Step 4) Launch PlayerApplication

    After PlayerApplication starts, hit http://localhost:8080/players/leo few times and then launch http://localhost:8080/actuator/prometheus to see metrics exposed on the prometheus actuator endpoint. Observe that http_server_requests_seconds_count keeps increasing as you keep hitting the players endpoint.

    Micrometer Prometheus

    Step 5) Checking metrics in Prometheus

    To check any metrics Prometheus, we will install Prometheus server where we can see the stats graphically.

    You can download Prometheus from this link https://prometheus.io/download/

    Go to the Prometheus installation directoy and open prometheus.yml file. Here you need to specify the actuator path and the local server address as shown below.

      # The job name is added as a label `job=` to any timeseries scraped from this config.
      - job_name: "prometheus"
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
          - targets: ["localhost:9090"]
      - job_name: 'spring-actuator'
        metrics_path: '/actuator/prometheus'
        scrape_interval: 5s
          - targets: ["localhost:8080"]

    Step 6) Launch Prometheus server

    Launch Prometheus server by clicking prometheus.exe and then launch http://localhost:9090.
    You can check if the metrics published by actuator is received by prometheus server or not using this link http://localhost:9090.

    Search for any metrics like http_server_requests_seconds_count and you will see the stats plotted as graph.

    Micrometer Prometheus


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Share This