Bird
Raised Fist0
Spring Bootframework~10 mins

Prometheus and Grafana integration concept in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Prometheus and Grafana integration concept
Spring Boot App
↓
Expose Metrics Endpoint
↓
Prometheus Scrapes Metrics
↓
Store Metrics in Time-Series DB
↓
Grafana Queries Prometheus
↓
Visualize Metrics on Dashboard
The Spring Boot app exposes metrics, Prometheus collects and stores them, and Grafana visualizes these metrics on dashboards.
Execution Sample
Spring Boot
management.endpoints.web.exposure.include=health,info,prometheus
management.endpoint.prometheus.enabled=true

# Prometheus scrape config
scrape_configs:
  - job_name: 'springboot-app'
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ['localhost:8080']
This config exposes the Prometheus endpoint in Spring Boot and sets Prometheus to scrape metrics from the app.
Execution Table
StepActionComponentResult
1Spring Boot app startsSpring BootMetrics endpoint /actuator/prometheus is available
2Prometheus scrapes metricsPrometheusFetches metrics from /actuator/prometheus
3Prometheus stores metricsPrometheusMetrics saved in time-series database
4Grafana queries PrometheusGrafanaRetrieves metrics data for visualization
5Grafana displays dashboardGrafanaUser sees live metrics charts
6User monitors app healthUserCan detect issues via Grafana dashboards
💡 Integration runs continuously; metrics update as app runs and Prometheus scrapes periodically
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Metrics EndpointNot exposedExposed at /actuator/prometheusScraped by PrometheusStored in DBQueried by GrafanaVisualized on Dashboard
Prometheus DataEmptyEmptyFetched metricsStored metricsQueried metricsUsed for visualization
Grafana DashboardEmptyEmptyEmptyEmptyData loadedCharts displayed
Key Moments - 3 Insights
Why does Prometheus need the Spring Boot app to expose a special endpoint?
Prometheus collects metrics by scraping a specific HTTP endpoint (/actuator/prometheus). Without this, Prometheus cannot get the app's metrics. See execution_table step 1 and 2.
How does Grafana get the data to show on dashboards?
Grafana queries Prometheus's stored metrics data via its API. It does not connect directly to the app. See execution_table step 4 and 5.
What happens if Prometheus cannot reach the Spring Boot metrics endpoint?
Prometheus will fail to scrape metrics, so no new data is stored. Grafana dashboards will show stale or no data. This is shown by the transition from step 2 to 3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Prometheus store metrics in its database?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row for step 3.
According to variable_tracker, what is the state of the Grafana Dashboard after Step 4?
AData loaded
BCharts displayed
CEmpty
DMetrics endpoint exposed
💡 Hint
Look at the 'Grafana Dashboard' row and the 'After Step 4' column in variable_tracker.
If the Spring Boot app does not expose /actuator/prometheus, what will happen in the execution flow?
APrometheus will scrape metrics successfully
BPrometheus scraping will fail at Step 2
CGrafana will query Prometheus with data
DUser will see live metrics on Grafana
💡 Hint
Refer to key_moments about the importance of the metrics endpoint and execution_table step 2.
Concept Snapshot
Spring Boot app exposes /actuator/prometheus endpoint
Prometheus scrapes this endpoint regularly
Prometheus stores metrics in time-series DB
Grafana queries Prometheus for metrics
Grafana visualizes metrics on dashboards
This setup enables live monitoring of app health
Full Transcript
This concept shows how a Spring Boot application integrates with Prometheus and Grafana for monitoring. The app exposes a special HTTP endpoint /actuator/prometheus that provides metrics data. Prometheus regularly scrapes this endpoint to collect metrics and stores them in its time-series database. Grafana then queries Prometheus to retrieve these metrics and displays them on dashboards for users to monitor the application's health and performance. The flow starts with the app exposing metrics, followed by Prometheus scraping and storing data, and ends with Grafana visualizing the data. If the metrics endpoint is not exposed, Prometheus cannot scrape data, and Grafana dashboards will not update. This integration runs continuously to provide live monitoring.

Practice

(1/5)
1. What is the primary role of Prometheus in a Spring Boot application monitoring setup?
easy
A. To collect metrics by scraping the application's exposed endpoint
B. To visualize metrics using dashboards
C. To expose application metrics automatically
D. To send alerts directly to users

Solution

  1. Step 1: Understand Prometheus function

    Prometheus collects metrics by scraping HTTP endpoints exposed by applications.
  2. Step 2: Differentiate roles of tools

    Grafana is used for visualization, not collection. Spring Boot exposes metrics but does not collect them.
  3. Final Answer:

    To collect metrics by scraping the application's exposed endpoint -> Option A
  4. Quick Check:

    Prometheus collects metrics = A [OK]
Hint: Remember: Prometheus scrapes, Grafana shows [OK]
Common Mistakes:
  • Confusing Grafana as the collector
  • Thinking Spring Boot collects metrics
  • Assuming Prometheus sends alerts directly
2. Which dependency must be added to a Spring Boot project to expose metrics for Prometheus scraping?
easy
A. spring-boot-starter-web
B. spring-boot-starter-data-jpa
C. micrometer-registry-prometheus
D. spring-boot-starter-security

Solution

  1. Step 1: Identify metrics registry dependency

    Micrometer provides metrics support; the Prometheus registry enables exposing metrics in Prometheus format.
  2. Step 2: Exclude unrelated dependencies

    Web starter is for REST APIs, JPA for database, Security for auth; none expose Prometheus metrics.
  3. Final Answer:

    micrometer-registry-prometheus -> Option C
  4. Quick Check:

    Prometheus metrics need micrometer-registry-prometheus [OK]
Hint: Add micrometer-registry-prometheus for Prometheus metrics [OK]
Common Mistakes:
  • Adding only spring-boot-starter-web without metrics registry
  • Confusing JPA or Security starters as metrics providers
  • Forgetting to include Prometheus registry dependency
3. Given this Spring Boot application.properties snippet:
management.endpoints.web.exposure.include=health,prometheus
management.endpoint.prometheus.enabled=true

What will be the URL path to scrape metrics for Prometheus?
medium
A. /metrics
B. /actuator/metrics
C. /prometheus/metrics
D. /actuator/prometheus

Solution

  1. Step 1: Understand actuator endpoint exposure

    Spring Boot exposes actuator endpoints under /actuator by default.
  2. Step 2: Identify Prometheus endpoint path

    Prometheus metrics endpoint is /actuator/prometheus when enabled and exposed.
  3. Final Answer:

    /actuator/prometheus -> Option D
  4. Quick Check:

    Prometheus metrics at /actuator/prometheus [OK]
Hint: Actuator endpoints default under /actuator [OK]
Common Mistakes:
  • Using /metrics instead of /actuator/prometheus
  • Assuming /prometheus/metrics is default
  • Forgetting to enable prometheus endpoint
4. You configured Prometheus to scrape your Spring Boot app, but metrics are not appearing in Grafana. Which is the most likely cause?
medium
A. Prometheus server is down but Grafana is running
B. Prometheus is not scraping the correct endpoint URL
C. Spring Boot app does not have a database connection
D. Grafana does not support Prometheus data sources

Solution

  1. Step 1: Check Prometheus scraping configuration

    If Prometheus scrapes wrong URL, no metrics collected, so Grafana shows nothing.
  2. Step 2: Exclude other causes

    Grafana supports Prometheus; database unrelated; if Prometheus down, Grafana shows errors.
  3. Final Answer:

    Prometheus is not scraping the correct endpoint URL -> Option B
  4. Quick Check:

    Wrong scrape URL blocks metrics flow [OK]
Hint: Verify Prometheus scrape URL matches app endpoint [OK]
Common Mistakes:
  • Blaming Grafana for lack of support
  • Assuming database affects metrics exposure
  • Ignoring Prometheus scrape configuration
5. You want to create a Grafana dashboard showing the average response time of your Spring Boot app over the last 5 minutes. Which Prometheus query should you use?
hard
A. rate(http_server_requests_seconds_sum[5m]) / rate(http_server_requests_seconds_count[5m])
B. sum(http_server_requests_seconds_sum) / sum(http_server_requests_seconds_count)
C. avg_over_time(http_server_requests_seconds_sum[5m])
D. count_over_time(http_server_requests_seconds_count[5m])

Solution

  1. Step 1: Understand metrics for response time

    http_server_requests_seconds_sum is total time; count is number of requests.
  2. Step 2: Calculate average over 5 minutes

    Use rate() to get per-second increase, then divide sum by count for average.
  3. Final Answer:

    rate(http_server_requests_seconds_sum[5m]) / rate(http_server_requests_seconds_count[5m]) -> Option A
  4. Quick Check:

    Average = sum rate / count rate [OK]
Hint: Divide sum rate by count rate for average response time [OK]
Common Mistakes:
  • Using sum without rate causes wrong aggregation
  • Using avg_over_time on sum metric alone
  • Counting requests instead of averaging time
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard