📖 Scenario: You are running a small Spring Boot web application that serves users. To keep your app healthy and responsive, you want to monitor its status and key metrics like uptime and request count.
🎯 Goal: Build a simple Spring Boot app with a health check endpoint and a counter for requests. Then add monitoring logic to track and display these metrics.
📋 What You'll Learn
Create a Spring Boot application class
Add a REST controller with a health check endpoint
Add a request counter variable
Increment the counter on each request
Print the current health status and request count
💡 Why This Matters
🌍 Real World
Monitoring helps keep applications running smoothly by alerting you to problems early. This simple app shows how to track basic health and usage.
💼 Career
DevOps engineers and backend developers often build and maintain monitoring endpoints to ensure system reliability and performance.
Progress0 / 4 steps
1
Create Spring Boot application class
Create a Spring Boot application class called MonitoringApp with the @SpringBootApplication annotation and a main method that runs the app using SpringApplication.run(MonitoringApp.class, args).
Spring Boot
Hint
Use @SpringBootApplication on the class and write a main method that calls SpringApplication.run.
2
Add REST controller with health check
Add a REST controller class called HealthController with the @RestController annotation. Inside it, create a method healthCheck mapped to /health using @GetMapping that returns the string "UP".
Spring Boot
Hint
Use @RestController on the class and @GetMapping("/health") on the method that returns "UP".
3
Add request counter and increment logic
Inside the HealthController class, add a private integer variable called requestCount initialized to 0. Modify the healthCheck method to increment requestCount by 1 each time it is called.
Spring Boot
Hint
Declare private int requestCount = 0; and add requestCount++; inside the healthCheck method.
4
Print health status and request count
Modify the healthCheck method to return a string showing both the health status and the current requestCount in this format: "Status: UP, Requests: X" where X is the value of requestCount. Use String.format to build the string.
Spring Boot
Hint
Use return String.format("Status: UP, Requests: %d", requestCount); to show the status and count.
Practice
(1/5)
1. Why is monitoring important in a Spring Boot application?
easy
A. It replaces the need for writing tests.
B. It automatically fixes all bugs without developer input.
C. It slows down the application to prevent overload.
D. It helps detect problems early and keeps the app running smoothly.
Solution
Step 1: Understand monitoring purpose
Monitoring tracks app health and performance in real time.
Step 2: Identify benefits in Spring Boot context
It helps find and fix issues quickly, ensuring smooth operation.
Final Answer:
It helps detect problems early and keeps the app running smoothly. -> Option D
Quick Check:
Monitoring = Early problem detection [OK]
Hint: Monitoring = early problem detection and smooth running [OK]
Common Mistakes:
Thinking monitoring fixes bugs automatically
Believing monitoring slows the app down
Confusing monitoring with testing
2. Which Spring Boot dependency is commonly used to add monitoring features?
easy
A. spring-boot-starter-web
B. spring-boot-starter-actuator
C. spring-boot-starter-data-jpa
D. spring-boot-starter-security
Solution
Step 1: Identify monitoring-related starter
Spring Boot Actuator provides built-in monitoring endpoints.
Step 2: Match dependency to monitoring
spring-boot-starter-actuator adds monitoring and health check features.
Selecting security starter unrelated to monitoring
3. Given this Spring Boot Actuator endpoint configuration: management.endpoints.web.exposure.include=health,info What will be the output when accessing /actuator/health?
medium
A. {"status":"DOWN"}
B. {"error":"Endpoint not found"}
C. {"status":"UP"}
D. {"info":"No data"}
Solution
Step 1: Check exposed endpoints
Only 'health' and 'info' endpoints are exposed via web.
Step 2: Understand default health output
By default, health endpoint returns {"status":"UP"} if app is running well.
Final Answer:
{"status":"UP"} -> Option C
Quick Check:
Health endpoint = {"status":"UP"} when healthy [OK]
Hint: Health endpoint shows status UP if app is healthy [OK]
Common Mistakes:
Expecting error when endpoint is exposed
Confusing info endpoint output with health
Assuming status DOWN without failure
4. You added Spring Boot Actuator but /actuator/health returns 404 error. What is the likely cause?
medium
A. The endpoint is disabled or not exposed in configuration.
B. The URL path is incorrect; it should be /health.
C. The application is not running.
D. Actuator dependency is missing in the project.
Solution
Step 1: Confirm Actuator dependency presence
If dependency was missing, app wouldn't compile or actuator endpoints wouldn't exist.
Step 2: Check endpoint exposure settings
By default, some actuator endpoints are disabled or not exposed over web, causing 404.
Final Answer:
The endpoint is disabled or not exposed in configuration. -> Option A
Quick Check:
Disabled endpoint = 404 error [OK]
Hint: Check actuator endpoint exposure settings if 404 occurs [OK]
Common Mistakes:
Assuming missing dependency without checking config
Thinking app not running causes 404 instead of connection error
Using wrong URL path for actuator endpoints
5. You want to monitor custom metrics in your Spring Boot app. Which approach best fits this need?
hard
A. Add custom metrics using Micrometer and expose them via Actuator endpoints.
B. Write logs manually and parse them later for metrics.
C. Use only the default health endpoint without changes.
D. Disable Actuator and rely on external monitoring tools only.
Solution
Step 1: Understand custom metrics requirement
Custom metrics need to be collected and exposed in a structured way.
Step 2: Identify Spring Boot recommended method
Micrometer integrates with Actuator to add and expose custom metrics easily.
Final Answer:
Add custom metrics using Micrometer and expose them via Actuator endpoints. -> Option A
Quick Check:
Micrometer + Actuator = custom metrics [OK]
Hint: Use Micrometer with Actuator for custom metrics [OK]
Common Mistakes:
Relying only on logs for metrics
Using default health endpoint for custom metrics
Disabling Actuator loses monitoring benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.