Skip to content

Commit fc87da7

Browse files
jonatan-ivanovsnicoll
authored andcommitted
Add Java InfoContributor
See gh-28136
1 parent 0b58d48 commit fc87da7

File tree

8 files changed

+311
-1
lines changed

8 files changed

+311
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
2121
import org.springframework.boot.actuate.info.GitInfoContributor;
2222
import org.springframework.boot.actuate.info.InfoContributor;
23+
import org.springframework.boot.actuate.info.java.JavaInfoContributor;
2324
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2425
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -40,6 +41,7 @@
4041
*
4142
* @author Meang Akira Tanaka
4243
* @author Stephane Nicoll
44+
* @author Jonatan Ivanov
4345
* @since 2.0.0
4446
*/
4547
@Configuration(proxyBeanMethods = false)
@@ -77,4 +79,11 @@ public InfoContributor buildInfoContributor(BuildProperties buildProperties) {
7779
return new BuildInfoContributor(buildProperties);
7880
}
7981

82+
@Bean
83+
@ConditionalOnEnabledInfoContributor("java")
84+
@Order(DEFAULT_ORDER)
85+
public InfoContributor javaInfoContributor() {
86+
return new JavaInfoContributor();
87+
}
88+
8089
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.boot.actuate.info.BuildInfoContributor;
26+
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
2627
import org.springframework.boot.actuate.info.GitInfoContributor;
2728
import org.springframework.boot.actuate.info.Info;
2829
import org.springframework.boot.actuate.info.InfoContributor;
30+
import org.springframework.boot.actuate.info.java.JavaInfo;
31+
import org.springframework.boot.actuate.info.java.JavaInfoContributor;
2932
import org.springframework.boot.info.BuildProperties;
3033
import org.springframework.boot.info.GitProperties;
3134
import org.springframework.boot.test.util.TestPropertyValues;
@@ -39,6 +42,7 @@
3942
* Tests for {@link InfoContributorAutoConfiguration}.
4043
*
4144
* @author Stephane Nicoll
45+
* @author Jonatan Ivanov
4246
*/
4347
class InfoContributorAutoConfigurationTests {
4448

@@ -55,7 +59,26 @@ void close() {
5559
void disableEnvContributor() {
5660
load("management.info.env.enabled:false");
5761
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
58-
assertThat(beans).hasSize(0);
62+
assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy(
63+
(infoContributor) -> assertThat(infoContributor).isNotInstanceOf(EnvironmentInfoContributor.class));
64+
}
65+
66+
@Test
67+
void disableJavaContributor() {
68+
load("management.info.java.enabled:false");
69+
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
70+
assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy(
71+
(infoContributor) -> assertThat(infoContributor).isNotInstanceOf(JavaInfoContributor.class));
72+
}
73+
74+
@Test
75+
void defaultInfoContributorsEnabled() {
76+
load();
77+
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
78+
assertThat(beans).hasSize(2).extractingFromEntries(Map.Entry::getValue)
79+
.anySatisfy(
80+
(infoContributor) -> assertThat(infoContributor).isInstanceOf(EnvironmentInfoContributor.class))
81+
.anySatisfy((infoContributor) -> assertThat(infoContributor).isInstanceOf(JavaInfoContributor.class));
5982
}
6083

6184
@Test
@@ -129,6 +152,17 @@ void customBuildInfoContributor() {
129152
.isSameAs(this.context.getBean("customBuildInfoContributor"));
130153
}
131154

155+
@Test
156+
void javaInfoContributor() {
157+
load();
158+
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
159+
assertThat(beans).containsKeys("javaInfoContributor");
160+
Map<String, Object> content = invokeContributor(
161+
this.context.getBean("javaInfoContributor", InfoContributor.class));
162+
Object javaInfo = content.get("java");
163+
assertThat(javaInfo).isInstanceOf(JavaInfo.class);
164+
}
165+
132166
private Map<String, Object> invokeContributor(InfoContributor contributor) {
133167
Info.Builder builder = new Info.Builder();
134168
contributor.contribute(builder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2021-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.java;
18+
19+
/**
20+
* A simple DTO that holds information about the Java environment the application is
21+
* running in.
22+
*
23+
* @author Jonatan Ivanov
24+
* @since 2.6.0
25+
*/
26+
public class JavaInfo {
27+
28+
private final String vendor;
29+
30+
private final String version;
31+
32+
private final JreInfo runtime;
33+
34+
private final VmInfo vm;
35+
36+
public JavaInfo() {
37+
this.vendor = System.getProperty("java.vendor");
38+
this.version = System.getProperty("java.version");
39+
this.runtime = new JreInfo();
40+
this.vm = new VmInfo();
41+
}
42+
43+
public String getVendor() {
44+
return this.vendor;
45+
}
46+
47+
public String getVersion() {
48+
return this.version;
49+
}
50+
51+
public JreInfo getRuntime() {
52+
return this.runtime;
53+
}
54+
55+
public VmInfo getVm() {
56+
return this.vm;
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2021-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.java;
18+
19+
import org.springframework.boot.actuate.info.Info.Builder;
20+
import org.springframework.boot.actuate.info.InfoContributor;
21+
22+
/**
23+
* An {@link InfoContributor} that exposes {@link JavaInfo}.
24+
*
25+
* @author Jonatan Ivanov
26+
* @since 2.6.0
27+
*/
28+
public class JavaInfoContributor implements InfoContributor {
29+
30+
private final JavaInfo javaInfo;
31+
32+
public JavaInfoContributor() {
33+
this.javaInfo = new JavaInfo();
34+
}
35+
36+
@Override
37+
public void contribute(Builder builder) {
38+
builder.withDetail("java", this.javaInfo);
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021-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.java;
18+
19+
/**
20+
* A simple DTO that holds information about the JRE (Java Runtime Environment) the
21+
* application is running in.
22+
*
23+
* @author Jonatan Ivanov
24+
* @since 2.6.0
25+
*/
26+
public class JreInfo {
27+
28+
private final String name;
29+
30+
private final String version;
31+
32+
public JreInfo() {
33+
this.name = System.getProperty("java.runtime.name");
34+
this.version = System.getProperty("java.runtime.version");
35+
}
36+
37+
public String getName() {
38+
return this.name;
39+
}
40+
41+
public String getVersion() {
42+
return this.version;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2021-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.java;
18+
19+
/**
20+
* A simple DTO that holds information about the JVM (Java Virtual Machine) the
21+
* application is running in.
22+
*
23+
* @author Jonatan Ivanov
24+
* @since 2.6.0
25+
*/
26+
public class VmInfo {
27+
28+
private final String name;
29+
30+
private final String vendor;
31+
32+
private final String version;
33+
34+
public VmInfo() {
35+
this.name = System.getProperty("java.vm.name");
36+
this.vendor = System.getProperty("java.vm.vendor");
37+
this.version = System.getProperty("java.vm.version");
38+
}
39+
40+
public String getName() {
41+
return this.name;
42+
}
43+
44+
public String getVendor() {
45+
return this.vendor;
46+
}
47+
48+
public String getVersion() {
49+
return this.version;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2021-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+
/**
18+
* Classes for java info.
19+
*/
20+
package org.springframework.boot.actuate.info.java;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021-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.java;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.info.Info;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link JavaInfoContributor}
27+
*
28+
* @author Jonatan Ivanov
29+
*/
30+
class JavaInfoContributorTests {
31+
32+
@Test
33+
void javaInfoShouldBeAdded() {
34+
JavaInfoContributor javaInfoContributor = new JavaInfoContributor();
35+
Info.Builder builder = new Info.Builder();
36+
javaInfoContributor.contribute(builder);
37+
38+
assertThat(builder.build().getDetails().get("java")).isInstanceOf(JavaInfo.class);
39+
JavaInfo javaInfo = (JavaInfo) builder.build().getDetails().get("java");
40+
41+
assertThat(javaInfo.getVendor()).isEqualTo(System.getProperty("java.vendor"));
42+
assertThat(javaInfo.getVersion()).isEqualTo(System.getProperty("java.version"));
43+
assertThat(javaInfo.getRuntime().getName()).isEqualTo(System.getProperty("java.runtime.name"));
44+
assertThat(javaInfo.getRuntime().getVersion()).isEqualTo(System.getProperty("java.runtime.version"));
45+
assertThat(javaInfo.getVm().getName()).isEqualTo(System.getProperty("java.vm.name"));
46+
assertThat(javaInfo.getVm().getVendor()).isEqualTo(System.getProperty("java.vm.vendor"));
47+
assertThat(javaInfo.getVm().getVersion()).isEqualTo(System.getProperty("java.vm.version"));
48+
}
49+
50+
}

0 commit comments

Comments
 (0)