🏠Spring BootSpring Boot Actuator Info endpoint – Complete Guide

Spring Boot Actuator Info endpoint – Complete Guide

Introduction

In this post, we will take a look at the spring boot actuator info endpoint. And also how to customize it to show build info, git commit info and extra info from the environment.

Spring Boot Actuator Dependency

First, you need to add the dependency for actuator.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Code language: HTML, XML (xml)

Once added, you can open the /actuator/info endpoint and you should ideally see an empty JSON response.

Spring boot actuator info endpoint showing no data by default
by default, the endpoint doesn’t show any information

Adding data to Info Endpoint

The info endpoint is public by default and hence you should only expose the details that are not sensitive. Let’s see how you can add data to this info API endpoint.

Adding application info

Also known as Environment info, you can pass info to this endpoint directly from application configuration files. And here is a straightforward example.

info.application.name=HelloWorld
info.application.description=A Demo for Spring Boot hello World!
info.application.author.name= Homer Simpson
info.application.author.bio= Nuclear Safety Inspector, Former Technical SupervisorCode language: Properties (properties)
info endpoint showing data from application.properties

Note that the JSON is a nested object as we defined them like an object with dots.

Adding Build info

If you want to add the build version to the info endpoint, then the application.properties won’t cut it. Also, changing the application.properties on each build takes effort. Spring boot handles this by loading data from META-INF/build-info.properties file if it is present in the JAR file.

To create the build-info.properties file, you should first change the spring-boot-maven-plugin settings as shown below.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Code language: HTML, XML (xml)

Here, the build-info goal will generate the META-INF/build-info.properties. Subsequently, the BuildInfoContributor will load info from the file.

actuator info endpoint showing build related information.
build information in actuator endpoint

The build info will only show up if you run the application as a JAR file. This is due to the META-INF/build-info.properties only available after maven build.

Adding GIT Commit info

To generate git related data, we need to use git-commit-id-plugin. And, This is how you should configure the plugin.

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <configuration>
        <failOnNoGitDirectory>false</failOnNoGitDirectory>
    </configuration>
</plugin>Code language: HTML, XML (xml)

This plugin will create a git.properties file under classpath. Later when the application starts, GitInfoContributor will load data from this file.

As you see in this screenshot, the info endpoint now includes VCS info.

Here, the failOnNoGitDirectory parameter is important otherwise you will get the following error.

.git directory could not be found! Please specify a valid [dotGitDirectory] in your pom.xml

You can control how much git info is shown using the following setting. However, I advise you not to show full info as they might contain sensitive info like author email, hostname etc.

management.info.git.mode=fullCode language: Properties (properties)

Providing Custom Info Contributor

Importantly, You can provide your InfoContributor bean and it will be picked by spring boot.

@Bean
InfoContributor getInfoContributor() {
    Map<String, Object> details = new HashMap<>();
    details.put("name", "SpringHow");
    details.put("website", "https://springhow.com/");
    Map<String, Object> wrapper = new HashMap<>();
    wrapper.put("org", details);
    return new MapInfoContributor(wrapper);
}Code language: Java (java)
Custom data in actuator info endpoint

Note that I wrapped the details in another map to organize them in an object structure. Similarly, you can provide as many InfoContributor as you want.

Controlling info endpoint

To disable the endpoint completely, you should use the following configuration.

management.endpoint.info.enabled=falseCode language: Properties (properties)

Also, you can disable individual contributors as shown below.

management.info.build.enabled=true
management.info.git.enabled=true
management.info.env.enabled=trueCode language: Properties (properties)

Along with these, the following will disable all default contributors. However, the custom info contributors will still be available.

management.info.defaults.enabled=falseCode language: Properties (properties)

Build and Git info as Beans

The InfoContributorAutoConfiguration exposes the build and git info as beans. Thus we can autowire them wherever we want and use them as normal beans.

@Autowired
private  BuildProperties buildProperties;

@Autowired
private  GitProperties gitProperties;

@GetMapping("/build-info")
public BuildProperties getBuildProperties(){
    return buildProperties;
}

@GetMapping("/git-info")
public GitProperties getGitProperties(){
    return gitProperties;
}Code language: Java (java)

Conclusion

So far, We now learned about the spring boot actuator info endpoint. And also, You can find the reference project at this GitHub repository.

If you liked this article, you may also like to read the following write-ups.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *