Learnitweb

Monitoring Spring Boot Applications with Spring Boot Actuator and Micrometer – an Introduction

What is Spring Boot Actuator?

Spring Boot Actuator is a project designed to help you monitor and manage your Spring Boot application in production.
With Actuator, you can get valuable insights and operational information about your application without writing any additional code or manually implementing any monitoring classes.

By simply adding Actuator to your Spring Boot application, you can expose:

  • Application health status
  • Metrics like memory usage, CPU usage, active threads, HTTP request statistics
  • Environment details (properties, configurations)
  • Custom application metrics (if needed)

Spring Boot Actuator gives you production-ready features with minimal effort.

How Does Spring Boot Actuator Work?

A common question is:
How does Spring Boot Actuator collect all this information so effortlessly?

The answer lies in an underlying library called Micrometer.

Starting from Spring Boot 2.0, Actuator internally uses Micrometer to collect, manage, and export metrics.

What is Micrometer?

  • Micrometer is a metrics collection library designed for JVM-based applications.
  • It acts as a facade (a simplified interface) for different monitoring systems like:
    • Prometheus
    • New Relic
    • Dynatrace
    • Datadog
    • Splunk
    • And others
  • Vendor-neutral metrics:
    Micrometer collects metrics in a common, vendor-neutral format. This means your application does not need to change when you decide to send metrics to a new monitoring tool.

Micrometer uses the Facade Design Pattern.
This design pattern hides complex implementation details and exposes a simple interface that applications interact with.
You don’t have to worry about how the data is collected or how it is formatted — Micrometer handles everything behind the scenes.

Key Concepts: Instrumentation and Metrics Collection

  • Instrumentation refers to the process of adding monitoring hooks into your application without affecting its normal operation.
  • Micrometer automatically instruments your application to collect metrics like:
    • JVM memory usage
    • Garbage collection statistics
    • HTTP request and response timings
    • Database call durations
  • It collects these metrics with virtually no performance overhead, ensuring your application continues to run efficiently.

How Metrics Are Exposed

Spring Boot Actuator allows you to access metrics in two main ways:

  1. HTTP Endpoints
    • Metrics are exposed via web endpoints like /actuator/metrics.
    • You can interact with these endpoints using any HTTP client (browser, Postman, curl, etc.).
    • You might need to enable or customize specific endpoints depending on your needs.
  2. JMX (Java Management Extensions)
    • Metrics are exposed as JMX MBeans.
    • JMX provides a standardized way to monitor and manage Java applications.
    • By default, Spring Boot Actuator exposes management endpoints via JMX even without additional configuration.

Thus, you can choose the method that suits your monitoring setup best — HTTP for easier external integrations or JMX for deeper JVM-level management.

Introduction to Micrometer

Micrometer is a metrics collection library for JVM-based applications like Spring Boot, Micronaut, Quarkus, and more.
It provides a simple, unified API to collect and export metrics to external monitoring systems — such as Prometheus, New Relic, Datadog, Dynatrace, and others.

Micrometer focuses on making applications observable by exposing essential operational data without adding performance overhead.

Why Micrometer?

Before Micrometer, each monitoring tool required its own custom metrics collection code.
Micrometer solves this by providing a vendor-neutral way to collect metrics once, and export them to many different backends without changing your application code.

Key features of Micrometer

  • Vendor-Neutral Interface
    Collect metrics once and send them to multiple monitoring systems.
  • Low Overhead
    Designed for production — minimal impact on application performance.
  • Built-In Instrumentation
    Pre-built metrics for:
    • JVM memory usage
    • CPU metrics
    • Garbage collection
    • Thread count
    • HTTP server request metrics
    • Database connection pool metrics
  • Support for Custom Metrics
    You can easily create and register your own application-specific counters, timers, and gauges.

How Micrometer Meters Work

  • Registration: You create a meter and register it to a MeterRegistry.
  • Collection: Metrics are automatically collected at runtime.
  • Export: Collected data is formatted and exported to monitoring systems (Prometheus, Datadog, etc.).
  • Naming and Tags:
    Every metric has:
    • Name (like http.server.requests)
    • Tags (key-value pairs) to add dimensions (like method=GET, status=200).

Example:

http.server.requests{method="POST", status="201", uri="/users"} 12.0

Here:

  • Metric name = http.server.requests
  • Tags = method, status, uri
  • Value = 12 requests

Core Concepts of Micrometer

In Micrometer, all metrics are collected and represented using objects called Meters.
Different types of meters serve different purposes based on what kind of data you want to measure.

Here are the main types of meters you will work with:

Meter TypePurposeExample UsageAdditional Details
CounterMeasures a value that only increases (it can never decrease).Counting number of API calls, number of errors, number of items processed.Counters are monotonic, meaning the value starts at 0 and only grows over time. You can only increment a counter, not decrement it.
GaugeMeasures a value that can go up and down.Current number of active sessions, size of a queue, CPU usage.A Gauge captures a snapshot of a value at a specific point in time. Gauges should be used for values that fluctuate.
TimerMeasures both the count and the total time taken for short events.HTTP request duration, database query execution time.Timers automatically record the number of times an event occurred and the total time spent, so you can calculate average time, maximum time, etc.
Distribution SummaryMeasures the distribution of numerical samples (size, amount, etc.).Size of HTTP response payloads, size of files uploaded.A Distribution Summary records the count, total amount, maximum, minimum, and mean of measured values. Useful for size-based metrics.
Long Task TimerMeasures duration of tasks that are long-running.Background job execution, batch job processing, file downloads that last minutes.Unlike normal Timers (which assume short events), Long Task Timers allow you to track tasks that stay open over long periods (minutes or hours). Each task can be individually started and stopped.
Function CounterLike a Counter, but reads a value from a function or object rather than manually incrementing.Counting the number of cached items by reading the cache directly.You don’t manually increment this type of counter; instead, Micrometer reads the value from your code (a method reference or lambda).
Function GaugeLike a Gauge, but reads a value dynamically from an object.CPU load, memory consumption, queue size.Automatically reads the current value from an object or method whenever it is needed.

Important Properties of Meters

  • Monotonic: Counters always grow.
  • Snapshot vs. Cumulative:
    • Gauges represent snapshots.
    • Counters and Timers are cumulative.
  • Dimensionality:
    Metrics can be sliced across multiple dimensions using tags. This is a major advantage over flat metric models.
  • Low-overhead Collection:
    Micrometer ensures minimal performance penalty even when many meters are being tracked.