Tuesday, 24 June 2025

Spring Boot Actuator

 Spring Boot Actuator is a powerful module that provides production-ready features to help you monitor and manage your Spring Boot application.


📌 What is Actuator?

Spring Boot Actuator exposes several built-in endpoints that give insights into your application:

  • Health

  • Metrics

  • Environment

  • Beans

  • Thread dumps

  • HTTP trace

  • Custom endpoints


✅ Step-by-Step Setup

🧱 1. Add Dependency

Maven:

xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

Gradle:

groovy
implementation 'org.springframework.boot:spring-boot-starter-actuator'

⚙️ 2. Configuration (application.yml or .properties)

application.yml (Recommended)

yaml
management: endpoints: web: exposure: include: "*" # Expose all endpoints endpoint: health: show-details: always # Show health details (disk, db, etc.) info: env: enabled: true

If using .properties

properties
management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

🚀 3. Access Endpoints

Once Actuator is enabled and running (on default port 8080), you can hit these endpoints:

EndpointDescriptionURL Example
/actuatorList all enabled endpointshttp://localhost:8080/actuator
/actuator/healthApplication health statushttp://localhost:8080/actuator/health
/actuator/metricsPerformance metrics (memory, etc.)http://localhost:8080/actuator/metrics
/actuator/beansAll Spring beanshttp://localhost:8080/actuator/beans
/actuator/envAll environment variableshttp://localhost:8080/actuator/env
/actuator/infoProject info (from info.*)http://localhost:8080/actuator/info
/actuator/loggersLog levels (change at runtime)http://localhost:8080/actuator/loggers

🔐 4. Security with Spring Security (Optional)

By default, Actuator endpoints are secured if Spring Security is on the classpath.

To allow all endpoints without auth (not for production):

yaml
management: endpoints: web: exposure: include: "*" spring: security: user: name: admin password: admin123

Login using admin/admin123 in browser when accessing endpoints.


➕ Custom Info Endpoint

Add this to application.yml:

yaml
info: app: name: MyApp description: Custom Spring Boot app version: 1.0.0

Then check: http://localhost:8080/actuator/info


➕ Custom Health Indicator

java
import org.springframework.boot.actuate.health.*; import org.springframework.stereotype.Component; @Component public class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { boolean appIsUp = true; // your custom logic if (appIsUp) { return Health.up().withDetail("custom", "Everything is OK!").build(); } return Health.down().withDetail("custom", "Something is wrong").build(); } }

🛠 Expose Actuator on a Different Port (Optional)

yaml
management: server: port: 9090

Now actuator is accessible at http://localhost:9090/actuator.


📦 Use Cases

Use CaseBenefit
Kubernetes liveness/readinessCheck app status automatically
Prometheus/GrafanaIntegrate metrics and monitoring
DebuggingView beans, thread dumps, env vars
DevOps FriendlyHealth, metrics, restart, shutdown

📌 Summary

FeatureDefault StatusConfigurable
/actuator/health✅ EnabledYes
/actuator/info✅ EnabledYes
Others like /metrics, /beans❌ Disabled by default✅ Enable via config

✅ 2. Expose Custom Metrics using Micrometer

📌 Custom Metric Example

java
import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Component; @Component public class OrderMetrics { private final Counter orderCounter; public OrderMetrics(MeterRegistry registry) { this.orderCounter = registry.counter("orders.created.count"); } public void incrementOrderCount() { orderCounter.increment(); } }

Use orderMetrics.incrementOrderCount() inside your service when an order is created.


✅ 3. Write a Custom Actuator Endpoint

📦 Step-by-Step Custom Endpoint

a. Add a dependency for custom actuator endpoint support (optional)

No extra dependency needed unless you're using older Spring Boot (<2.0)


b. Custom Endpoint Code

import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; import java.util.Map; @Component @Endpoint(id = "customstatus") public class CustomStatusEndpoint { @ReadOperation public Map<String, String> customHealth() { return Map.of("app-status", "running", "uptime", "5h 23m"); } }

This exposes: http://localhost:8080/actuator/customstatus

You must explicitly expose it:

yaml
management: endpoints: web: exposure: include: health, prometheus, customstatus

✅ Bonus: Custom HealthIndicator

java
import org.springframework.boot.actuate.health.*; import org.springframework.stereotype.Component; @Component public class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { boolean appHealthy = true; // your logic if (appHealthy) { return Health.up().withDetail("custom-check", "All good").build(); } else { return Health.down().withDetail("custom-check", "Something failed").build(); } } }

It becomes part of /actuator/health.

No comments:

Post a Comment