Make Java Apps Shine: Visualize and Monitor with Micronaut, Prometheus, and Grafana

Effortlessly Enhanced Monitoring: Java Apps with Micronaut, Prometheus, and Grafana

Make Java Apps Shine: Visualize and Monitor with Micronaut, Prometheus, and Grafana

Monitoring the performance and health of Java applications is no small feat, but when using Micronaut, Prometheus, and Grafana, it gets a lot simpler. This trio keeps your applications running smoothly and efficiently, ensuring you can keep an eye on what’s happening under the hood without breaking a sweat. Let’s dive in and see how you can set this up.

Understanding Micronaut

Picture this: a framework that makes building modular, testable applications a breeze. That’s Micronaut in a nutshell. It supports Java, Kotlin, and Groovy, and it’s a big hit for microservices and cloud-native apps. One of the best parts? It’s designed to have fast startup times and a minimal memory footprint. This makes it awesome for scenarios like serverless functions and low-memory microservices.

Setting Up Micronaut with Micrometer

To keep tabs on your application’s performance and health metrics, you need Micrometer. It’s a handy library for collecting metrics and exporting them to various monitoring systems, including Prometheus.

Start by adding the necessary dependencies to your build.gradle file. It’s as simple as dropping in:

dependencies {
    implementation 'io.micronaut.micrometer:micronaut-micrometer-core'
    implementation 'io.micronaut.micrometer:micronaut-micrometer-registry-prometheus'
}

Next, head to your application.yml file and flick the switch to enable metrics:

micronaut:
  metrics:
    enabled: true
    export:
      prometheus:
        enabled: true
        step: 1m

Boom! You’ve enabled metrics collection and set Prometheus as the export registry.

Configuring Web Metrics

Micronaut comes with a default web filter for collecting web metrics, but you can tweak it to capture specific metrics. Here’s a cool configuration you can use:

micronaut:
  metrics:
    binders:
      web:
        server:
          percentiles: 0.95,0.99
          histogram: true
          slos: 0.1,0.4,0.5,2
          min: 0.1
          max: 60
        client:
          percentiles: 0.95,0.99
          histogram: true
          slos: 0.1,0.4,0.5,2
          min: 0.1
          max: 60

This config sets up percentiles, histograms, and service-level objectives (SLOs) for both server and client metrics.

Exposing Metrics to Prometheus

Next step? Expose these metrics to Prometheus. When you enable the Prometheus registry, Micronaut automatically exposes metrics at the /prometheus endpoint.

You can check this out by navigating to http://localhost:8080/prometheus in your browser. You should see all your collected metrics in that sweet Prometheus format.

Setting Up Prometheus

Prometheus is your metrics collector here. You need to set it up to scrape the metrics endpoint exposed by your Micronaut application. Here’s an example prometheus.yml configuration that’ll do the trick:

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'micronaut'
    static_configs:
      - targets: ['localhost:8080']

This config tells Prometheus to scrape metrics from http://localhost:8080/prometheus every 10 seconds.

Visualizing Metrics with Grafana

Grafana is your visualization buddy. It lets you create dashboards to visualize those metrics you’re collecting.

  1. Add a Prometheus Data Source:

    • Navigate to Configuration > Data Sources in your Grafana instance.
    • Click Add data source and go with Prometheus.
    • Pop in the URL of your Prometheus instance (e.g., http://localhost:9090) and save.
  2. Create a Dashboard:

    • Head over to Dashboards > New dashboard.
    • Add a new panel, selecting Prometheus as the data source.
    • Use PromQL to query your metrics. For instance, to show JVM memory usage, use: jvm_memory_used_bytes{area="heap"}.

We’ll have something like this:

jvm_memory_used_bytes{area="heap"}

You can craft more complex queries for detailed metrics, such as CPU usage or request latency.

Advanced Metrics and Dashboards

Fancy diving deeper? You can create custom dashboards to monitor metrics like CPU usage, thread states, and garbage collection. Here’s an example configuration for tracking JVM memory pools:

micronaut:
  metrics:
    binders:
      jvm:
        enabled: true
        memory:
          enabled: true
          pools:
            - heap
            - non-heap

In Grafana, create panels to display these metrics. To show heap memory usage, for instance, you’ll use the query jvm_memory_used_bytes{area="heap"}.

Example Dashboard

To give you an idea of what a solid dashboard might look like, here are some queries for different metrics:

  • JVM Memory:
    • Heap Memory Usage: jvm_memory_used_bytes{area="heap"}
    • Non-Heap Memory Usage: jvm_memory_used_bytes{area="non-heap"}
  • CPU Usage:
    • CPU Usage: process_cpu_usage
  • Thread States:
    • Thread Count: jvm_threads_current
    • Thread States: jvm_threads_states{state="runnable"} (for other states too)
  • Garbage Collection:
    • GC Count: jvm_gc_collection_count
    • GC Time: jvm_gc_collection_time

Wrapping It Up

Setting up Micronaut with Prometheus and Grafana to monitor your Java applications is a game-changer. This setup not only provides real-time insights into your app’s performance but also helps you quickly spot and fix issues. With Micronaut, Prometheus, and Grafana working together, you’ve got a robust monitoring solution that ensures your applications are always alive and kicking. Trust me, once you get this going, you’ll wonder how you ever managed without it.



Similar Posts
Blog Image
Unleashing Java Magic with JUnit 5: Your Secret Weapon for Effortless Test Mastery

Discovering the Test Wizardry of JUnit 5: A Java Adventure into Seamless Parameterized Testing Excellence

Blog Image
Are You Making These Common Java Dependency Management Mistakes?

Weaving Order from Chaos: The Art of Dependency Management in Java Apps

Blog Image
Why Java Will Be the Most In-Demand Skill in 2025

Java's versatility, extensive ecosystem, and constant evolution make it a crucial skill for 2025. Its ability to run anywhere, handle complex tasks, and adapt to emerging technologies ensures its continued relevance in software development.

Blog Image
Orchestrating Microservices: The Spring Boot and Kubernetes Symphony

Orchestrating Microservices: An Art of Symphony with Spring Boot and Kubernetes

Blog Image
Unleashing the Power of Vaadin’s Custom Components for Enterprise Applications

Vaadin's custom components: reusable, efficient UI elements. Encapsulate logic, boost performance, and integrate seamlessly. Create modular, expressive code for responsive enterprise apps. Encourage good practices and enable powerful, domain-specific interfaces.

Blog Image
Unlock the Magic of Microservices with Spring Boot

Harnessing the Elusive Magic of Spring Boot for Effortless Microservices Creation