How to add prometheus and grafana to spring boot?

by filiberto , in category: Third Party Scripts , a month ago

How to add prometheus and grafana to spring boot?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , a month ago

@filiberto 

To add Prometheus and Grafana to a Spring Boot application, you can follow these steps:

  1. Add Prometheus dependencies to your Spring Boot application by adding the following dependency to your pom.xml file:
1
2
3
4
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>


  1. Configure Prometheus by adding the following properties to your application.properties file:
1
2
management.endpoints.web.exposure.include=*
management.metrics.export.prometheus.enabled=true


  1. Add a Prometheus endpoint by adding the following annotation to your main Spring Boot application class:
1
2
3
4
5
6
7
@SpringBootApplication
@EnablePrometheusEndpoint
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}


  1. Install Grafana on your machine and start the Grafana server.
  2. Set up Grafana to connect to Prometheus by adding a Prometheus data source in Grafana. Configure the data source with the URL of your Prometheus server (e.g. http://localhost:9090) and save the configuration.
  3. Create dashboards in Grafana to visualize the metrics collected by Prometheus. You can create custom dashboards to monitor different aspects of your Spring Boot application.


By following these steps, you can add Prometheus and Grafana to your Spring Boot application and monitor its performance and health in real-time.