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:


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 /prometheus endpoint 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.internal works on macOS/Windows. Use 172.17.0.1 on Linux.

▢️ Step 7: Start Monitoring Stack


docker-compose up

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:


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.