25

How do I add things to the /info endpoint in Spring Boot programmatically? The documentation states that this is possible for the /health endpoint through the use of HealthIndicator interface. Is there something for the /info endpoint as well?

I would like to add operating system name and version and other runtime info there.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • Although it's not the answer you are looking for, the OS info along with a ton of other runtime info are present in the `/env` mapping – geoand May 26 '14 at 07:05
  • To add to my previous comment, if you put properties in application.properties like `info.os = ${os.name}` the expression **will** be evaluated and show up correctly in the info endpoint. – geoand May 26 '14 at 07:13
  • I found this link very helpful: http://mrhaki.blogspot.ie/2016/12/spring-sweets-add-extra-build.html. No custom logic, just few lines of configuration. – Daniele Feb 21 '18 at 16:45

4 Answers4

31

In Spring Boot 1.4, you are able to declare InfoContributer beans to make this a whole lot easier:

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example",
                Collections.singletonMap("key", "value"));
    }

}

See http://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/htmlsingle/#production-ready-application-info-custom for more info.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
10

The accepted answer actually clobbers the InfoEndpoint and does not add to it.

One way I found to add to the info is, in a @Configuration class, add an @Autowired method that adds extra properties following the info.* conventions to the environment. Then InfoEndpoint will pick them up when its invoked.

You can do something like the following:

@Autowired
public void setInfoProperties(ConfigurableEnvironment env) {
    /* These properties will show up in the Spring Boot Actuator /info endpoint */
    Properties props = new Properties();

    props.put("info.timeZone", ZoneId.systemDefault().toString());

    env.getPropertySources().addFirst(new PropertiesPropertySource("extra-info-props", props));
}
Erin Drummond
  • 5,347
  • 6
  • 35
  • 41
  • Thanks for the idea but this didn't work for me, these additional properties are visible on /env but not on /info. Also I noticed you are putting the time zone there, I am also resetting my default time zones, but found that this code that put them in the properties was called before I reset them. – Uncle Long Hair Mar 07 '16 at 22:46
  • Are you sure you prefixed them with "info." as described here? https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-application-info . Also, since Properties can take an Object as a value, you can set it as an instance of class that overrides toString() to provide whatever dynamic information you want – Erin Drummond Mar 08 '16 at 01:42
  • 1
    only this one worked for me! wasnt able to get the accepted answer working – Marquis Blount Jun 09 '16 at 07:03
9

One way to do what you want (in the event that you have totally custom properties you need to display) is to declare a bean of type InfoEndpoint which will override the default.

@Bean
public InfoEndpoint infoEndpoint() {
     final LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
     map.put("test", "value"); //put whatever other values you need here
     return new InfoEndpoint(map);
}

As you can see from the code above, the map can contain whatever info you need.

In the event that the data you want to show can be retrieved by the environment and is not custom, you do not need to override the InfoEndpoint bean, but you can simply add properties to the properties file with a prefix of info. One example where the OS name is evaluated is the following:

info.os = ${os.name}

In the code above, Spring Boot will evaluate the right-hand expression before returning the property in the /info endpoint.

A final note is that there is a ton of environment information that is already available in the /env endpoint

Update

As pointed out by @shabinjo, in newer Spring Boot versions there is no InfoEndpoint constructor that accepts a map. You can however use the following snippet:

@Bean
public InfoEndpoint infoEndpoint() {
     final Map<String, Object> map = new LinkedHashMap<String, Object>();
     map.put("test", "value"); //put whatever other values you need here
     return new InfoEndpoint(new MapInfoContributor(map));
}

The code above will completely override the default info that would end-up in /info. To overcome this issue one could add the following bean

@Bean
public MapInfoContributor mapInfoContributor() {
    return new MapInfoContributor(new HashMap<String, Object>() {{
        put("test", "value");
    }});
}
geoand
  • 60,071
  • 24
  • 172
  • 190
  • Thanks for these very useful tips. I want to _add_ stuff to the default /info (since that already contains useful info like GIT commit id, Maven version, etc..). Returning my own bean would probably not include the defaults? – Wim Deblauwe May 26 '14 at 10:23
  • @WimDeblauwe You are correct, if you define your own bean you will effectively be excluding the other information. To see how you can re-enable it, take a look at the source code of ` org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration` – geoand May 26 '14 at 10:44
  • I checked the source, but I cannot `@Autowire` the `InfoPropertiesConfiguration` class because it is protected. :( – Wim Deblauwe May 26 '14 at 12:15
  • @WimDeblauwe That's bad... It seems like you will have to duplicate the code if you want the same handling but also to be able to add stuff – geoand May 26 '14 at 12:17
  • @geoand I faced with strange /info behavior. I have declared info.os = ${os.name} in my properties but sometimes /info and /env shows different values for os.name and info one is wrong. Do you know something about it? – Normal May 15 '15 at 18:03
  • I don't have any specific insights i'm afraid... What values are being displayed? – geoand May 16 '15 at 09:18
  • i am using springboot starter 1.5.2,new InfoEndpoint(map) is not accepting map as a constructor parameter instead of InfoEndpoint(List infoContributors).However i could make it with new InfoEndpoint (infoContributors(new MapInfoContributor(map)).But it is overriding all other info properties and showing onlythis particular info and /info – shabinjo Jul 28 '17 at 12:20
  • @shabinjo Thanks for pointing this out! I added your code in the updated answer – geoand Jul 28 '17 at 15:03
  • @shabinjo I also added a solution that would overcome the issue you mentioned – geoand Jul 28 '17 at 15:09
0

It should be possible to add a custom PropertySource inside an ApplicationListener to add custom info.* properties to the environment (see this answer for an example: How to Override Spring-boot application.properties programmatically)

Community
  • 1
  • 1
Lukas Hinsch
  • 1,840
  • 14
  • 8