Prometheus collects data about your app's health and performance. Grafana shows this data in easy-to-understand charts and graphs.
Prometheus and Grafana integration concept in Spring Boot
1. Add Prometheus dependency to Spring Boot project. 2. Configure Spring Boot to expose metrics at /actuator/prometheus. 3. Set up Prometheus server to scrape metrics from Spring Boot app. 4. Install Grafana and add Prometheus as a data source. 5. Create dashboards in Grafana to visualize metrics.
Spring Boot uses Micrometer to expose metrics in Prometheus format.
Prometheus scrapes metrics by regularly requesting the /actuator/prometheus endpoint.
dependencies {
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}management.endpoints.web.exposure.include=health,info,prometheus management.endpoint.prometheus.enabled=true
scrape_configs: - job_name: 'springboot-app' static_configs: - targets: ['localhost:8080']
In Grafana UI: - Add Prometheus as data source with URL http://localhost:9090 - Create dashboard and add panels to show metrics like JVM memory, HTTP requests
This setup lets Prometheus collect metrics from your Spring Boot app and Grafana display them.
1. Add to build.gradle: implementation 'io.micrometer:micrometer-registry-prometheus' implementation 'org.springframework.boot:spring-boot-starter-actuator' 2. In application.properties: management.endpoints.web.exposure.include=health,info,prometheus management.endpoint.prometheus.enabled=true 3. Run Spring Boot app on port 8080. 4. Prometheus config (prometheus.yml): scrape_configs: - job_name: 'springboot-app' static_configs: - targets: ['localhost:8080'] 5. Start Prometheus server. 6. Start Grafana, add Prometheus as data source (http://localhost:9090). 7. Create Grafana dashboard to visualize metrics.
Make sure your Spring Boot app allows access to the /actuator/prometheus endpoint.
Prometheus and Grafana usually run on ports 9090 and 3000 respectively by default.
Use Grafana's pre-built dashboards for Spring Boot and Micrometer to save time.
Prometheus collects app metrics by scraping an endpoint.
Grafana connects to Prometheus to show these metrics visually.
Spring Boot apps expose metrics easily using Micrometer and Actuator.
Practice
Solution
Step 1: Understand Prometheus function
Prometheus collects metrics by scraping HTTP endpoints exposed by applications.Step 2: Differentiate roles of tools
Grafana is used for visualization, not collection. Spring Boot exposes metrics but does not collect them.Final Answer:
To collect metrics by scraping the application's exposed endpoint -> Option AQuick Check:
Prometheus collects metrics = A [OK]
- Confusing Grafana as the collector
- Thinking Spring Boot collects metrics
- Assuming Prometheus sends alerts directly
Solution
Step 1: Identify metrics registry dependency
Micrometer provides metrics support; the Prometheus registry enables exposing metrics in Prometheus format.Step 2: Exclude unrelated dependencies
Web starter is for REST APIs, JPA for database, Security for auth; none expose Prometheus metrics.Final Answer:
micrometer-registry-prometheus -> Option CQuick Check:
Prometheus metrics need micrometer-registry-prometheus [OK]
- Adding only spring-boot-starter-web without metrics registry
- Confusing JPA or Security starters as metrics providers
- Forgetting to include Prometheus registry dependency
management.endpoints.web.exposure.include=health,prometheus
management.endpoint.prometheus.enabled=trueWhat will be the URL path to scrape metrics for Prometheus?
Solution
Step 1: Understand actuator endpoint exposure
Spring Boot exposes actuator endpoints under /actuator by default.Step 2: Identify Prometheus endpoint path
Prometheus metrics endpoint is /actuator/prometheus when enabled and exposed.Final Answer:
/actuator/prometheus -> Option DQuick Check:
Prometheus metrics at /actuator/prometheus [OK]
- Using /metrics instead of /actuator/prometheus
- Assuming /prometheus/metrics is default
- Forgetting to enable prometheus endpoint
Solution
Step 1: Check Prometheus scraping configuration
If Prometheus scrapes wrong URL, no metrics collected, so Grafana shows nothing.Step 2: Exclude other causes
Grafana supports Prometheus; database unrelated; if Prometheus down, Grafana shows errors.Final Answer:
Prometheus is not scraping the correct endpoint URL -> Option BQuick Check:
Wrong scrape URL blocks metrics flow [OK]
- Blaming Grafana for lack of support
- Assuming database affects metrics exposure
- Ignoring Prometheus scrape configuration
Solution
Step 1: Understand metrics for response time
http_server_requests_seconds_sum is total time; count is number of requests.Step 2: Calculate average over 5 minutes
Use rate() to get per-second increase, then divide sum by count for average.Final Answer:
rate(http_server_requests_seconds_sum[5m]) / rate(http_server_requests_seconds_count[5m]) -> Option AQuick Check:
Average = sum rate / count rate [OK]
- Using sum without rate causes wrong aggregation
- Using avg_over_time on sum metric alone
- Counting requests instead of averaging time
Ready to prove it?
Pass the test and this pattern moves unknown
