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:
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
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
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/prometheusendpoint publicly.
π Step 4: Build and Run the App
Use Maven to build the app:
./mvnw clean package
Run it:
java -jar target/micronaut-metrics-0.1.jar
π Step 5: Check Metrics Endpoint
Navigate to:
http://localhost:8080/prometheus
Youβll see Prometheus-formatted metrics like:
# 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:
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)
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'micronaut'
static_configs:
- targets: ['host.docker.internal:8080']
π
host.docker.internalworks on macOS/Windows. Use172.17.0.1on Linux.
βΆοΈ Step 7: Start Monitoring Stack
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:
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.