Spring Boot Actuator Health Checks, Metrics & Monitoring

Modern applications need to be observable, monitorable, and resilient. That’s where Spring Boot Actuator shines β€” it exposes production-ready features like health checks, metrics, auditing, info endpoints, and more.

In this guide, we’ll explore how to configure and use Spring Boot Actuator Health Checks, Metrics, and Monitoring endpoints in a typical application.

Spring Boot Actuator Health Checks, Metrics & Monitoring

πŸ“¦ What is Spring Boot Actuator?

Spring Boot Actuator is a subproject that adds a set of REST endpoints to your application to help you monitor and manage it in production.

With a simple dependency, you gain access to:

  • /actuator/health – check if your app is alive
  • /actuator/metrics – runtime metrics
  • /actuator/env, /actuator/info, and more

🧰 Step 1: Add Actuator to Your Project

βœ… Maven Dependency



    org.springframework.boot
    spring-boot-starter-actuator


βš™οΈ Step 2: Basic Configuration

πŸ“ application.yml


management:
  endpoints:
    web:
      exposure:
        include: health, metrics, info, env, prometheus
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true

You can choose to expose only specific endpoints or all (include: '*' for development).

🌑️ Health Checks

πŸš‘ Endpoint: /actuator/health

The health endpoint reports whether the application is healthy. It integrates with:

  • DB
  • Disk space
  • Redis
  • RabbitMQ
  • Custom checks

Example Output:


{
  "status": "UP",
  "components": {
    "db": { "status": "UP" },
    "diskSpace": { "status": "UP" }
  }
}

βœ… Add DB Health Indicator

If using a datasource, Spring Boot will auto-configure a DataSourceHealthIndicator.

You can also create your own:


package com.kscodes.springboot.monitoring.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthCheck implements HealthIndicator {

    @Override
    public Health health() {
        boolean serviceUp = checkMyService();
        return serviceUp ? Health.up().withDetail("custom", "Service OK").build()
                         : Health.down().withDetail("custom", "Service FAILED").build();
    }

    private boolean checkMyService() {
        // Ping or validate logic
        return true;
    }
}

πŸ“Š Metrics Endpoint

πŸ“ˆ Endpoint: /actuator/metrics

Gives runtime statistics like:

  • JVM memory usage
  • CPU
  • HTTP request counts and timing
  • Garbage collection
  • Thread states

To view metric names:


GET /actuator/metrics

To get details of one:


GET /actuator/metrics/jvm.memory.used

Sample Output:


{
  "name": "jvm.memory.used",
  "measurements": [;
    { "statistic": "VALUE", "value": 16570000.0 }
  ],
  "availableTags": [;
    { "tag": "area", "values": ["heap", "nonheap"] },
    { "tag": "id", "values": ["G1 Old Gen", "Code Cache"] }
  ]
}

πŸ–₯️ Info Endpoint

🧾 /actuator/info

Enrich this with build or environment data.

Example:


info:
  app:
    name: Spring Boot Monitoring App
    version: 1.0.1
    description: App to demonstrate actuator


info:
  app:
    name: Spring Boot Monitoring App
    version: 1.0.1
    description: App to demonstrate actuator

πŸ“¦ Step 3: Integrate with Prometheus & Grafana

Add Micrometer Prometheus:



    io.micrometer
    micrometer-registry-prometheus


Enable /actuator/prometheus


management:
  endpoints:
    web:
      exposure:
        include: prometheus

Prometheus Setup

Add this scrape config in your prometheus.yml:


scrape_configs:
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']

Now you can visualize metrics in Grafana using Prometheus as data source.

πŸ›‘οΈ Securing Actuator Endpoints

By default, most endpoints are secure. Use Spring Security to restrict access.


@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
      .authorizeHttpRequests(auth -> auth
          .requestMatchers("/actuator/health").permitAll()
          .requestMatchers("/actuator/**").hasRole("ADMIN")
          .anyRequest().authenticated()
      );
    return http.build();
}

πŸ§ͺ Bonus: Enable HTTP Trace



    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.boot
    spring-boot-actuator-autoconfigure




management:
  endpoint:
    httptrace:
      enabled: true
  endpoints:
    web:
      exposure:
        include: httptrace

Access /actuator/httptrace to see incoming HTTP requests for diagnostics.

βœ… Summary

  • Spring Boot Actuator Health Checks are key to reliable applications.
  • Use /actuator/health, /actuator/metrics, /actuator/info, and /actuator/prometheus for complete monitoring.
  • Integrate with Prometheus & Grafana for visualization.
  • Use custom health indicators for app-specific checks.

πŸ”— References