Bird
0
0

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📝 Application Q15 of 15
Spring Boot - Actuator
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()?
ATimer timer = new Timer("process.time"); timer.start(); processData(); timer.stop();
BTimer timer = Timer.create("process.time"); timer.record(processData());
CmeterRegistry.timer("process.time").start(); processData(); meterRegistry.timer("process.time").stop();
DTimer timer = Timer.builder("process.time").register(meterRegistry); timer.record(() -> processData());
Step-by-Step Solution
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]
Quick Trick: 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()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes