Skip to content

Commit c700f68

Browse files
jonatan-ivanovsnicoll
authored andcommitted
Expose OS information as an InfoContributor
See gh-28907
1 parent b9a7c2f commit c700f68

File tree

8 files changed

+210
-2
lines changed

8 files changed

+210
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.actuate.info.GitInfoContributor;
2222
import org.springframework.boot.actuate.info.InfoContributor;
2323
import org.springframework.boot.actuate.info.JavaInfoContributor;
24+
import org.springframework.boot.actuate.info.OsInfoContributor;
2425
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2526
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -86,4 +87,11 @@ public JavaInfoContributor javaInfoContributor() {
8687
return new JavaInfoContributor();
8788
}
8889

90+
@Bean
91+
@ConditionalOnEnabledInfoContributor(value = "os", fallback = InfoContributorFallback.DISABLE)
92+
@Order(DEFAULT_ORDER)
93+
public OsInfoContributor osInfoContributor() {
94+
return new OsInfoContributor();
95+
}
96+
8997
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@
279279
"description": "Whether to enable Java info.",
280280
"defaultValue": false
281281
},
282+
{
283+
"name": "management.info.os.enabled",
284+
"type": "java.lang.Boolean",
285+
"description": "Whether to enable OS info.",
286+
"defaultValue": false
287+
},
282288
{
283289
"name": "management.metrics.binders.files.enabled",
284290
"type": "java.lang.Boolean",

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import org.springframework.boot.actuate.info.Info;
2828
import org.springframework.boot.actuate.info.InfoContributor;
2929
import org.springframework.boot.actuate.info.JavaInfoContributor;
30+
import org.springframework.boot.actuate.info.OsInfoContributor;
3031
import org.springframework.boot.autoconfigure.AutoConfigurations;
3132
import org.springframework.boot.info.BuildProperties;
3233
import org.springframework.boot.info.GitProperties;
3334
import org.springframework.boot.info.JavaInfo;
35+
import org.springframework.boot.info.OsInfo;
3436
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3537
import org.springframework.context.annotation.Bean;
3638
import org.springframework.context.annotation.Configuration;
@@ -151,6 +153,16 @@ void javaInfoContributor() {
151153
});
152154
}
153155

156+
@Test
157+
void osInfoContributor() {
158+
this.contextRunner.withPropertyValues("management.info.os.enabled=true").run((context) -> {
159+
assertThat(context).hasSingleBean(OsInfoContributor.class);
160+
Map<String, Object> content = invokeContributor(context.getBean(OsInfoContributor.class));
161+
assertThat(content).containsKey("os");
162+
assertThat(content.get("os")).isInstanceOf(OsInfo.class);
163+
});
164+
}
165+
154166
private Map<String, Object> invokeContributor(InfoContributor contributor) {
155167
Info.Builder builder = new Info.Builder();
156168
contributor.contribute(builder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.info;
18+
19+
import org.springframework.boot.info.OsInfo;
20+
21+
/**
22+
* An {@link InfoContributor} that exposes {@link OsInfo}.
23+
*
24+
* @author Jonatan Ivanov
25+
* @since 2.7.0
26+
*/
27+
public class OsInfoContributor implements InfoContributor {
28+
29+
private final OsInfo osInfo;
30+
31+
public OsInfoContributor() {
32+
this.osInfo = new OsInfo();
33+
}
34+
35+
@Override
36+
public void contribute(Info.Builder builder) {
37+
builder.withDetail("os", this.osInfo);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.info;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.info.JavaInfo;
22+
import org.springframework.boot.info.OsInfo;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link OsInfoContributor}
28+
*
29+
* @author Jonatan Ivanov
30+
*/
31+
public class OsInfoContributorTests {
32+
33+
@Test
34+
void osInfoShouldBeAdded() {
35+
OsInfoContributor osInfoContributor = new OsInfoContributor();
36+
Info.Builder builder = new Info.Builder();
37+
osInfoContributor.contribute(builder);
38+
Info info = builder.build();
39+
assertThat(info.getDetails().get("os")).isInstanceOf(OsInfo.class);
40+
}
41+
42+
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,13 +1169,18 @@ When appropriate, Spring auto-configures the following `InfoContributor` beans:
11691169
| Exposes Java runtime information.
11701170
| None.
11711171

1172+
| `os`
1173+
| {spring-boot-actuator-module-code}/info/OsInfoContributor.java[`OsInfoContributor`]
1174+
| Exposes Operating System information.
1175+
| None.
1176+
11721177
|===
11731178

11741179
Whether or not an individual contributor is enabled is controlled by its `management.info.<id>.enabled` property.
11751180
Different contributors have different defaults for this property, depending on their prerequisites and the nature of the information that they expose.
11761181

1177-
With no prerequisites to indicate that they should be enabled, the `env` and `java` contributors are disabled by default.
1178-
You can enable them by setting the configprop:management.info.env.enabled[] or configprop:management.info.java.enabled[] properties to `true`.
1182+
With no prerequisites to indicate that they should be enabled, the `env`, `java` and `os` contributors are disabled by default.
1183+
You can enable them by setting the configprop:management.info.env.enabled[], configprop:management.info.java.enabled[] or configprop:management.info.os.enabled[] properties to `true`.
11791184

11801185
The `build` and `git` info contributors are enabled by default.
11811186
Each can be disabled by setting its `management.info.<id>.enabled` property to `false`.
@@ -1266,6 +1271,12 @@ The `info` endpoint publishes information about your Java runtime environment, s
12661271

12671272

12681273

1274+
[[actuator.endpoints.info.os-information]]
1275+
==== OS Information
1276+
The `info` endpoint publishes information about your Operating System, see {spring-boot-module-api}/info/OsInfo.html[`OsInfo`] for more details.
1277+
1278+
1279+
12691280
[[actuator.endpoints.info.writing-custom-info-contributors]]
12701281
==== Writing Custom InfoContributors
12711282
To provide custom application information, you can register Spring beans that implement the {spring-boot-actuator-module-code}/info/InfoContributor.java[`InfoContributor`] interface.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.info;
18+
19+
/**
20+
* Information about the Operating System the application is running on.
21+
*
22+
* @author Jonatan Ivanov
23+
* @since 2.7.0
24+
*/
25+
public class OsInfo {
26+
27+
private final String name;
28+
29+
private final String version;
30+
31+
private final String arch;
32+
33+
public OsInfo() {
34+
this.name = System.getProperty("os.name");
35+
this.version = System.getProperty("os.version");
36+
this.arch = System.getProperty("os.arch");
37+
}
38+
39+
public String getName() {
40+
return this.name;
41+
}
42+
43+
public String getVersion() {
44+
return this.version;
45+
}
46+
47+
public String getArch() {
48+
return this.arch;
49+
}
50+
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.info;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link OsInfo}.
25+
*
26+
* @author Jonatan Ivanov
27+
*/
28+
public class OsInfoTests {
29+
30+
@Test
31+
void osInfoIsAvailable() {
32+
OsInfo osInfo = new OsInfo();
33+
assertThat(osInfo.getName()).isEqualTo(System.getProperty("os.name"));
34+
assertThat(osInfo.getVersion()).isEqualTo(System.getProperty("os.version"));
35+
assertThat(osInfo.getArch()).isEqualTo(System.getProperty("os.arch"));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)