📖 Scenario: You are building a simple Spring Boot application that tracks how many times a greeting endpoint is called. This helps you understand how often users access your service.
🎯 Goal: Create a Spring Boot application that uses Micrometer to count the number of times the /greet endpoint is accessed.
📋 What You'll Learn
Create a Spring Boot application with a REST controller
Add a Micrometer counter metric to track endpoint calls
Configure a counter variable to hold the metric
Increment the counter each time the endpoint is called
💡 Why This Matters
🌍 Real World
Tracking how often endpoints are called helps monitor application usage and detect issues early.
💼 Career
Understanding Micrometer metrics is essential for backend developers working on Spring Boot applications to build observable and maintainable services.
Progress0 / 4 steps
1
Create a REST controller with a greeting endpoint
Create a Spring Boot REST controller class named GreetingController with a method greet mapped to /greet that returns the string "Hello, World!".
Spring Boot
Hint
Use @RestController on the class and @GetMapping("/greet") on the method.
2
Add a Micrometer Counter variable
Add a private final Counter variable named greetCounter to the GreetingController class. Inject a MeterRegistry in the constructor and initialize greetCounter with Counter.builder("greet.calls").description("Number of greet calls").register(meterRegistry).
Spring Boot
Hint
Use constructor injection to get MeterRegistry and build the counter.
3
Increment the counter in the greet method
Inside the greet method, add a line to increment the greetCounter by calling greetCounter.increment() before returning the greeting string.
Spring Boot
Hint
Call greetCounter.increment() at the start of the greet method.
4
Add Micrometer dependency and enable metrics endpoint
Add the spring-boot-starter-actuator and micrometer-core dependencies to your pom.xml or build.gradle. Then, in application.properties, enable the metrics endpoint by adding management.endpoints.web.exposure.include=metrics.
Spring Boot
Hint
Make sure to add both dependencies and enable metrics endpoint in properties.
Practice
(1/5)
1. What is the primary purpose of Micrometer in a Spring Boot application?
easy
A. To manage database connections efficiently
B. To collect and expose application metrics like counters and timers
C. To handle user authentication and authorization
D. To serve static web content like images and CSS
Solution
Step 1: Understand Micrometer's role
Micrometer is designed to collect metrics such as counters, timers, and gauges to monitor app performance.
Step 2: Compare with other options
Options B, C, and D relate to database, security, and web content, which are not Micrometer's focus.
Final Answer:
To collect and expose application metrics like counters and timers -> Option B
Quick Check:
Micrometer = Metrics collection [OK]
Hint: Micrometer is for metrics, not security or database [OK]
Common Mistakes:
Confusing Micrometer with security or database tools
Thinking Micrometer serves static files
Assuming Micrometer manages app logic
2. Which of the following is the correct way to create a counter metric using Micrometer in Spring Boot?
easy
A. Counter counter = Counter.builder("my.counter").register(meterRegistry);
B. Counter counter = new Counter("my.counter");
C. Counter counter = meterRegistry.createCounter("my.counter");
D. Counter counter = Counter.create("my.counter");
Solution
Step 1: Recall Micrometer counter creation syntax
Micrometer uses builder pattern: Counter.builder(name).register(meterRegistry) to create counters.
Step 2: Check other options for correctness
Options B, C, and D use incorrect constructors or methods not available in Micrometer.
Final Answer:
Counter counter = Counter.builder("my.counter").register(meterRegistry); -> Option A
Quick Check:
Use builder().register() to create counters [OK]
Hint: Use builder() and register() to create metrics [OK]
Common Mistakes:
Trying to instantiate Counter with new keyword
Using non-existent createCounter method
Missing register() call after builder()
3. Given the following code snippet in a Spring Boot app using Micrometer:
C. Counters cannot be incremented by negative values; this will throw an exception
D. increment() method does not exist on Counter
Solution
Step 1: Understand Counter increment rules
Counters only allow positive increments; negative values cause errors.
Step 2: Validate other options
Counter names with dots are allowed, meterRegistry is assumed valid, and increment() exists.
Final Answer:
Counters cannot be incremented by negative values; this will throw an exception -> Option C
Quick Check:
Counter increments must be positive [OK]
Hint: Counters only accept positive increments [OK]
Common Mistakes:
Trying to decrement counters with negative increments
Thinking dots in metric names are invalid
Assuming increment() method is missing
5. You want to measure the duration of a method execution in a Spring Boot app using Micrometer. Which approach correctly uses a Timer to record the time taken by a method processData()?
hard
A. Timer timer = new Timer("process.time");
timer.start();
processData();
timer.stop();
B. Timer timer = Timer.create("process.time");
timer.record(processData());
C. meterRegistry.timer("process.time").start();
processData();
meterRegistry.timer("process.time").stop();
D. Timer timer = Timer.builder("process.time").register(meterRegistry);
timer.record(() -> processData());
Solution
Step 1: Identify correct Timer usage in Micrometer
Micrometer uses Timer.builder(name).register() and timer.record(Runnable) to measure durations.
Step 2: Check other options for errors
Options A, C, and D use invalid constructors or methods not supported by Micrometer.
Final Answer:
Timer timer = Timer.builder("process.time").register(meterRegistry);
timer.record(() -> processData()); -> Option D
Quick Check:
Use Timer.builder().register() and record() for timing [OK]
Hint: Use timer.record(() -> method()) to measure duration [OK]
Common Mistakes:
Trying to instantiate Timer with new or create()
Calling start() and stop() manually on Timer
Passing method call directly instead of lambda to record()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.