Monitoring is essential in modern applications. It helps track performance, diagnose issues, and understand system behavior. In Micronaut, Micrometer is the default metrics library that integrates easily with tools like Prometheus and Grafana.
This post teaches you how to:
- Integrate Micrometer with Micronaut
- Collect app metrics
- Expose Prometheus-compatible endpoints
- Visualize data in Grafana
Weβll use Maven and the package com.kscodes.micronaut.metrics
throughout the project.

π§° Prerequisites
- Java 17+
- Maven 3.6+
- Micronaut CLI (optional)
- Docker (for Prometheus/Grafana)
- cURL or a browser to test endpoints
π¦ Step 1: Create a Micronaut Project
Generate the app:
1 2 3 4 5 6 7 |
mn create-app com.kscodes.micronaut.metrics \ --features micrometer-prometheus \ --build maven \ --jdk 17 |
This adds Micrometer and configures Prometheus support out-of-the-box.
π¨βπ» Step 2: Add a Sample Controller
File: HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes.micronaut.metrics; import io.micronaut.http.annotation.*; @Controller("/hello") public class HelloController { @Get public String greet() { return "Hello from Micronaut + Micrometer!"; } } |
βοΈ Step 3: Update Configuration
File: src/main/resources/application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
micronaut: application: name: micronaut-metrics micronaut.metrics: enabled: true export: prometheus: enabled: true step: PT1M endpoints: prometheus: enabled: true sensitive: false |
Explanation:
enabled: true
β Enables Micrometer.export.prometheus
β Allows Prometheus to scrape data.endpoints.prometheus
β Exposes/prometheus
endpoint publicly.
π Step 4: Build and Run the App
Use Maven to build the app:
1 2 3 4 |
./mvnw clean package |
Run it:
1 2 3 4 |
java -jar target/micronaut-metrics-0.1.jar |
π Step 5: Check Metrics Endpoint
Navigate to:
1 2 3 4 |
http://localhost:8080/prometheus |
Youβll see Prometheus-formatted metrics like:
1 2 3 4 5 6 7 |
# HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{area="heap",id="PS Eden Space",} 1562520.0 ... |
These include:
- JVM memory usage
- Garbage collection stats
- HTTP request metrics
π Step 6: Monitor with Prometheus and Grafana
Letβs connect this to a monitoring dashboard using Docker.
1. Docker Compose File
Create docker-compose.yml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
version: '3' services: prometheus: image: prom/prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml grafana: image: grafana/grafana ports: - "3000:3000" |
2. Prometheus Config (prometheus.yml
)
1 2 3 4 5 6 7 8 9 10 |
global: scrape_interval: 15s scrape_configs: - job_name: 'micronaut' static_configs: - targets: ['host.docker.internal:8080'] |
π
host.docker.internal
works on macOS/Windows. Use172.17.0.1
on Linux.
βΆοΈ Step 7: Start Monitoring Stack
1 2 3 4 |
docker-compose up |
Open:
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000 (login: admin / admin)
In Grafana:
- Add Prometheus as a data source.
- Create dashboards for JVM, HTTP, etc.
π§ͺ Optional: Add Custom Metrics
You can add custom counters and timers:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import io.micrometer.core.instrument.MeterRegistry; @Inject MeterRegistry meterRegistry; @Get("/custom") public String customMetrics() { meterRegistry.counter("custom.endpoint.hit.count").increment(); return "Custom metric recorded!"; } |
π External Resources
β Conclusion
Micronaut and Micrometer together offer a powerful way to observe and monitor Java applications. With minimal setup, you can collect, export, and visualize metrics in real-time.
Using the com.kscodes.micronaut.metrics
package and Maven-based project, your application is production-ready with robust observability built-in.