Micronaut and Micrometer: Metrics and Monitoring Made Simple

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.

Micronaut and Micrometer: Metrics and Monitoring Made Simple

🧰 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:

This adds Micrometer and configures Prometheus support out-of-the-box.

πŸ‘¨β€πŸ’» Step 2: Add a Sample Controller

File: HelloController.java

βš™οΈ Step 3: Update Configuration

File: src/main/resources/application.yml

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:

Run it:

πŸ” Step 5: Check Metrics Endpoint

Navigate to:

You’ll see Prometheus-formatted metrics like:

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:

2. Prometheus Config (prometheus.yml)

πŸ“ host.docker.internal works on macOS/Windows. Use 172.17.0.1 on Linux.

▢️ Step 7: Start Monitoring Stack

Open:

  • Prometheus: http://localhost:9090
  • Grafana: http://localhost:3000 (login: admin / admin)

In Grafana:

  1. Add Prometheus as a data source.
  2. Create dashboards for JVM, HTTP, etc.

πŸ§ͺ Optional: Add Custom Metrics

You can add custom counters and timers:

πŸ“š 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.