Monitor java application using prometheus
To monitor a JAVA (spring boot) application you need to add some prometheus dependencies in your JAVA code. For more information about spring boot and dependencies follow the link below:-
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html
For serving metrics in java spring boot application we need to add dependencies in our codebase, in my case I am having a spring boot application on gradle too, for exposing metrics add dependencies in build.gradle file.
Here is an example:-
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-nginx"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
//Prometheus
compile 'io.prometheus:simpleclient_spring_boot:0.0.21'
compile 'io.prometheus:simpleclient_servlet:0.0.21'
compile 'io.prometheus:simpleclient_hotspot:0.0.21'
}
After adding this make the build of your project and run the application. Suppose your application is running on port no. 8080 and it is exposing its metrics on path /prometheus then you need to add it to your prometheus.yml file and then create a dashboard for it in grafana.
After adding the application ip and port no. in your prometheus.yml for monitoring, your config file should look like this
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'my-java-app'
scrape_interval: 5s
metrics_path: '/prometheus'
static_configs:
- targets: ['your-ip:8080']