Performance: Custom actuator endpoints
This affects the server response time and the size of monitoring data sent to clients, impacting backend performance and network load.
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import java.util.concurrent.atomic.AtomicReference; import java.util.Map; @Endpoint(id = "cachedMetrics") public class CachedMetricsEndpoint { private final AtomicReference<Map<String, Object>> cache = new AtomicReference<>(); public CachedMetricsEndpoint() { // Precompute or update cache asynchronously cache.set(Map.of("data", "precomputed result")); } @ReadOperation public Map<String, Object> metrics() { return cache.get(); } }
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import java.util.Map; @Endpoint(id = "heavyMetrics") public class HeavyMetricsEndpoint { @ReadOperation public Map<String, Object> metrics() throws InterruptedException { // Simulate expensive operation Thread.sleep(5000); return Map.of("data", "heavy computation result"); } }
| Pattern | Server Processing | Response Delay | Network Payload | Verdict |
|---|---|---|---|---|
| Blocking heavy computation in endpoint | High CPU and blocking | 5 seconds delay | Small JSON | [X] Bad |
| Cached or precomputed endpoint data | Minimal CPU, non-blocking | Instant response | Small JSON | [OK] Good |
@Endpoint annotation is specifically designed to mark a class as a custom actuator endpoint.@ReadOperation annotation marks a method as a read-only operation accessible via HTTP GET.@Endpoint(id = "status")
public class StatusEndpoint {
@ReadOperation
public String getStatus() {
return "UP";
}
}@Endpoint(id = "status") and has a method with @ReadOperation returning "UP".getStatus() method, returning the string "UP" as the response body.@Endpoint(id = "metrics")
public class MetricsEndpoint {
@ReadOperation
public String metrics() {
return 12345;
}
}metrics() returns an integer literal 12345 but declares return type String, causing a type mismatch.@ReadOperation annotation is present, and @Endpoint is correctly on the class. Method name can be arbitrary.@Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public String updateConfig(String key, String value) {
// update logic
return "Updated";
}
}
B) @Endpoint(id = "config")
public class ConfigEndpoint {
@ReadOperation
public void updateConfig(String key, String value) {
// update logic
}
}
C) @Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public void updateConfig() {
// update logic
}
}
D) @Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public String updateConfig() {
// update logic
return "Updated";
}
}@WriteOperation with String key and String value parameters and returns a String response, which is valid for POST updates. Using @ReadOperation is wrong for update operations. The other snippets lack parameters needed to receive key and value.Pass the test and this pattern moves unknown