Bird
Raised Fist0
Spring Bootframework~20 mins

Metrics with Micrometer in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Micrometer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Micrometer counter increment?
Consider this Spring Boot code snippet using Micrometer:
Counter counter = meterRegistry.counter("my.counter");
counter.increment();
counter.increment(2);

What is the value of counter.count() after these calls?
Spring Boot
Counter counter = meterRegistry.counter("my.counter");
counter.increment();
counter.increment(2);
double value = counter.count();
A0.0
B2.0
C1.0
D3.0
Attempts:
2 left
💡 Hint
Remember that each increment adds to the counter's total count.
📝 Syntax
intermediate
2:00remaining
Which option correctly registers a Gauge metric in Micrometer?
You want to register a Gauge metric named "queue.size" that tracks the size of a queue. Which code snippet is correct?
AGauge gauge = Gauge.builder("queue.size", queue, q -> q.size()).register(meterRegistry);
BGauge gauge = meterRegistry.gauge("queue.size", queue.size());
CGauge gauge = Gauge.builder("queue.size", () -> queue.size()).register(meterRegistry);
DGauge gauge = Gauge.builder("queue.size", queue).register(meterRegistry);
Attempts:
2 left
💡 Hint
The builder requires the object and a function to get the value.
🔧 Debug
advanced
2:00remaining
Why does this Timer metric not record durations correctly?
Given this code:
Timer timer = meterRegistry.timer("process.time");
timer.record(() -> {
  // some process
  Thread.sleep(100);
});
System.out.println(timer.count());

The printed count is always 0. What is the cause?
Spring Boot
Timer timer = meterRegistry.timer("process.time");
timer.record(() -> {
  Thread.sleep(100);
});
System.out.println(timer.count());
AThread.sleep throws InterruptedException which is not handled, so the lambda never completes.
BThe timer is not registered properly with meterRegistry.
CThe record method requires a Callable, not a Runnable.
DThe timer.count() returns the number of recorded events, so 0 means no record was made.
Attempts:
2 left
💡 Hint
Check if the lambda handles checked exceptions properly.
❓ state_output
advanced
2:00remaining
What is the value of the Gauge after updating the tracked object?
Consider this code:
AtomicInteger value = new AtomicInteger(5);
Gauge gauge = Gauge.builder("current.value", value, AtomicInteger::get).register(meterRegistry);
value.set(10);
double gaugeValue = gauge.value();

What is the value of gaugeValue?
Spring Boot
AtomicInteger value = new AtomicInteger(5);
Gauge gauge = Gauge.builder("current.value", value, AtomicInteger::get).register(meterRegistry);
value.set(10);
double gaugeValue = gauge.value();
A5.0
B0.0
C10.0
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Gauge reads the current value from the object each time it is queried.
🧠 Conceptual
expert
2:00remaining
Which Micrometer meter type is best to measure the rate of events per second?
You want to monitor how many requests your service receives per second. Which Micrometer meter type should you use?
ADistributionSummary
BCounter
CGauge
DTimer
Attempts:
2 left
💡 Hint
Think about a metric that only increases and can be used to calculate rates.

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

  1. Step 1: Understand Micrometer's role

    Micrometer is designed to collect metrics such as counters, timers, and gauges to monitor app performance.
  2. Step 2: Compare with other options

    Options B, C, and D relate to database, security, and web content, which are not Micrometer's focus.
  3. Final Answer:

    To collect and expose application metrics like counters and timers -> Option B
  4. 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

  1. Step 1: Recall Micrometer counter creation syntax

    Micrometer uses builder pattern: Counter.builder(name).register(meterRegistry) to create counters.
  2. Step 2: Check other options for correctness

    Options B, C, and D use incorrect constructors or methods not available in Micrometer.
  3. Final Answer:

    Counter counter = Counter.builder("my.counter").register(meterRegistry); -> Option A
  4. 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:
Counter counter = Counter.builder("requests.count").register(meterRegistry);
counter.increment();
counter.increment(2);
What will be the value of the counter after these calls?
medium
A. 3
B. 2
C. 0
D. 1

Solution

  1. Step 1: Analyze the increment calls

    The first call increments the counter by 1, the second increments by 2.
  2. Step 2: Calculate total increments

    Total increments = 1 + 2 = 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    1 + 2 = 3 increments [OK]
Hint: Add all increment values to get total count [OK]
Common Mistakes:
  • Ignoring the second increment call
  • Assuming increment() resets counter
  • Confusing increment() with set()
4. Consider this Spring Boot code snippet using Micrometer:
Counter counter = Counter.builder("api.calls").register(meterRegistry);
counter.increment(-1);
What is the issue with this code?
medium
A. meterRegistry is not initialized properly
B. Counter names cannot contain dots
C. Counters cannot be incremented by negative values; this will throw an exception
D. increment() method does not exist on Counter

Solution

  1. Step 1: Understand Counter increment rules

    Counters only allow positive increments; negative values cause errors.
  2. Step 2: Validate other options

    Counter names with dots are allowed, meterRegistry is assumed valid, and increment() exists.
  3. Final Answer:

    Counters cannot be incremented by negative values; this will throw an exception -> Option C
  4. 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

  1. Step 1: Identify correct Timer usage in Micrometer

    Micrometer uses Timer.builder(name).register() and timer.record(Runnable) to measure durations.
  2. Step 2: Check other options for errors

    Options A, C, and D use invalid constructors or methods not supported by Micrometer.
  3. Final Answer:

    Timer timer = Timer.builder("process.time").register(meterRegistry); timer.record(() -> processData()); -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard