0
0
Spring Bootframework~30 mins

Metrics with Micrometer in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Metrics with Micrometer in Spring Boot
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Make sure to add both dependencies and enable metrics endpoint in properties.