Java > Spring Framework > Spring Boot > Spring Boot Actuator
Spring Boot Actuator: Custom Info Endpoint
This snippet illustrates how to add custom information to the /info endpoint in Spring Boot Actuator. This is useful for displaying application-specific details such as build version, environment details, or any other relevant information that can aid in debugging or monitoring.
Add Custom InfoContributor
Here, a `CustomInfoContributor` is created, implementing the `InfoContributor` interface. The `contribute` method builds a map containing custom information about the application, such as the application name, version, and environment. The `withDetail` method then adds this map to the info endpoint's response.
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class CustomInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
Map<String, Object> details = new HashMap<>();
details.put("app-name", "My Awesome Application");
details.put("version", "1.0.0");
details.put("environment", System.getenv("ENVIRONMENT"));
builder.withDetail("custom-info", details);
}
}
Configuration (application.properties/yml)
Ensure that the `info` endpoint is exposed. Add `management.endpoints.web.exposure.include=info` to your `application.properties` or `application.yml` file. This makes the `/actuator/info` endpoint accessible.
#application.properties
management.endpoints.web.exposure.include=info
Accessing the Info Endpoint
Accessing the `info` endpoint via a GET request to `http://localhost:8080/actuator/info` will now include the custom information provided by the `CustomInfoContributor`.
curl http://localhost:8080/actuator/info
Concepts Behind the Snippet
Spring Boot Actuator's /info endpoint is designed to expose general information about the application. Using InfoContributor allows you to extend this endpoint with custom details.
Real-Life Use Case Section
In a microservices architecture, the /info
endpoint can be used to display which version of the microservice is running. This is crucial during deployments and for debugging issues across multiple services. It helps operators and developers quickly identify the specific version and environment of each service.
Best Practices
/info
endpoint./info
endpoint.
Interview Tip
Be prepared to discuss how you can use the /info
endpoint to expose application-specific information and how it helps in managing and monitoring the application. Explain how InfoContributor
helps add custom details and what kind of information is relevant.
When to use them
Use the custom /info
endpoint when you need to display application-specific details that are not readily available through other Actuator endpoints. This could include build information, environment details, or any other relevant metadata.
Memory Footprint
The memory footprint is minimal, primarily related to the storage of the custom information added via the InfoContributor
. Ensure the custom information added is not excessively large or complex to avoid unnecessary memory consumption.
Alternatives
Instead of using the Actuator /info
endpoint, you could expose a custom endpoint that returns the same information. However, using Actuator provides a standardized way to manage and monitor the application.
Pros
Cons
FAQ
-
How do I secure the /info endpoint?
You can secure the `/info` endpoint along with other Actuator endpoints using Spring Security. Configure authentication and authorization rules for `/actuator/**` to protect sensitive information. -
Can I contribute multiple InfoContributors?
Yes, you can define multiple `InfoContributor` beans, and Spring Boot will automatically collect information from all of them and include it in the `/info` endpoint. -
How do I handle properties files in the custom InfoContributor?
You can inject Spring's `Environment` into the `InfoContributor` and use it to access properties from your `application.properties` or `application.yml` file. This allows you to externalize configuration values.