Working with Actuator Endpoints

1. Adding Spring Boot Actuator Dependency

Example: Actuator dependency

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

2. Exposing Actuator Endpoints

Example: Expose and configure actuator endpoints

management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus,loggers
        exclude: env
      base-path: /actuator
  endpoint:
    health:
      show-details: when_authorized
      probes:
        enabled: true
  server:
    port: 9090  # separate management port

3. Using Health Endpoint (/actuator/health)

Indicator Auto-registered
db JDBC/JPA
diskSpace Always
redis Spring Data Redis
mongo Spring Data Mongo
kafka Spring Kafka
readinessState / livenessState K8s probes

4. Using Info Endpoint (/actuator/info)

Example: Info endpoint with git and build metadata

management:
  info:
    git: { mode: full }
    build: { enabled: true }
    env: { enabled: true }
info:
  app:
    name: ${spring.application.name}
    version: @project.version@

5. Using Metrics Endpoint (/actuator/metrics)

Path Returns
/metrics List of metric names
/metrics/{name} Value + tags
/metrics/jvm.memory.used?tag=area:heap Filtered tag

6. Using Env Endpoint (/actuator/env)

Endpoint Use
/env All property sources
/env/{name} Single property + source
show-values: when_authorized Mask sensitive in unauth
Warning: Never expose /env publicly — it leaks secrets.

7. Using Loggers Endpoint (/actuator/loggers)

Example: Change log level at runtime via actuator

# GET current levels
curl http://localhost:8080/actuator/loggers/com.example
# Change level at runtime (POST)
curl -X POST -H "Content-Type: application/json" \
  -d '{"configuredLevel":"DEBUG"}' \
  http://localhost:8080/actuator/loggers/com.example

8. Using Thread Dump Endpoint

Endpoint Format
/threaddump JSON or text/plain (Accept header)
Use Diagnose deadlocks, stuck threads

9. Using Heap Dump Endpoint

Aspect Detail
Format HPROF binary
Analysis VisualVM, MAT, JProfiler
Performance Pauses JVM — use with care

10. Using Beans Endpoint

Note: Returns ALL beans in context with their dependencies — useful for debugging wiring issues.

11. Creating Custom Health Indicators

Example: Custom health indicator with latency detail

@Component
public class PaymentGatewayHealth implements HealthIndicator {
  private final PaymentClient client;
  public PaymentGatewayHealth(PaymentClient client) { this.client = client; }
  @Override public Health health() {
    try {
      var status = client.ping();
      return Health.up().withDetail("latencyMs", status.latency()).build();
    } catch (Exception ex) {
      return Health.down(ex).build();
    }
  }
}

12. Creating Custom Metrics (MeterRegistry)

Example: Custom counter and timer metrics

@Service
public class OrderService {
  private final Counter placed;
  private final Timer processTime;
  public OrderService(MeterRegistry reg) {
    this.placed = Counter.builder("orders.placed").tag("region", "us").register(reg);
    this.processTime = Timer.builder("orders.process").register(reg);
  }
  public Order place(NewOrder o) {
    return processTime.record(() -> { placed.increment(); return repo.save(/*…*/); });
  }
}

13. Securing Actuator Endpoints

Example: Restrict actuator to ADMIN role

@Bean
SecurityFilterChain actuatorSecurity(HttpSecurity http) throws Exception {
  return http
    .securityMatcher(EndpointRequest.toAnyEndpoint())
    .authorizeHttpRequests(a -> a
      .requestMatchers(EndpointRequest.to("health", "info")).permitAll()
      .anyRequest().hasRole("ADMIN"))
    .httpBasic(Customizer.withDefaults())
    .build();
}