Skip to content

Commit b9728d7

Browse files
Add SpringInfoContributor
This InfoContributor exposes information related to Spring projects.
1 parent 4c98cf8 commit b9728d7

File tree

8 files changed

+321
-2
lines changed

8 files changed

+321
-2
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.actuate.info.InfoContributor;
2323
import org.springframework.boot.actuate.info.JavaInfoContributor;
2424
import org.springframework.boot.actuate.info.OsInfoContributor;
25+
import org.springframework.boot.actuate.info.SpringInfoContributor;
2526
import org.springframework.boot.autoconfigure.AutoConfiguration;
2627
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -34,6 +35,7 @@
3435
import org.springframework.core.Ordered;
3536
import org.springframework.core.annotation.Order;
3637
import org.springframework.core.env.ConfigurableEnvironment;
38+
import org.springframework.core.env.Environment;
3739

3840
/**
3941
* {@link EnableAutoConfiguration Auto-configuration} for standard
@@ -92,4 +94,11 @@ public OsInfoContributor osInfoContributor() {
9294
return new OsInfoContributor();
9395
}
9496

97+
@Bean
98+
@ConditionalOnEnabledInfoContributor(value = "spring", fallback = InfoContributorFallback.DISABLE)
99+
@Order(DEFAULT_ORDER)
100+
public SpringInfoContributor springInfoContributor(Environment environment) {
101+
return new SpringInfoContributor(environment);
102+
}
103+
95104
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@
309309
"description": "Whether to enable Operating System info.",
310310
"defaultValue": false
311311
},
312+
{
313+
"name": "management.info.spring.enabled",
314+
"type": "java.lang.Boolean",
315+
"description": "Whether to enable Spring-related info.",
316+
"defaultValue": false
317+
},
312318
{
313319
"name": "management.metrics.binders.files.enabled",
314320
"type": "java.lang.Boolean",

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

+12
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import org.springframework.boot.actuate.info.InfoContributor;
2929
import org.springframework.boot.actuate.info.JavaInfoContributor;
3030
import org.springframework.boot.actuate.info.OsInfoContributor;
31+
import org.springframework.boot.actuate.info.SpringInfoContributor;
3132
import org.springframework.boot.autoconfigure.AutoConfigurations;
3233
import org.springframework.boot.info.BuildProperties;
3334
import org.springframework.boot.info.GitProperties;
3435
import org.springframework.boot.info.JavaInfo;
3536
import org.springframework.boot.info.OsInfo;
37+
import org.springframework.boot.info.SpringInfo;
3638
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3739
import org.springframework.context.annotation.Bean;
3840
import org.springframework.context.annotation.Configuration;
@@ -164,6 +166,16 @@ void osInfoContributor() {
164166
});
165167
}
166168

169+
@Test
170+
void springInfoContributor() {
171+
this.contextRunner.withPropertyValues("management.info.spring.enabled=true").run((context) -> {
172+
assertThat(context).hasSingleBean(SpringInfoContributor.class);
173+
Map<String, Object> content = invokeContributor(context.getBean(SpringInfoContributor.class));
174+
assertThat(content).containsKey("spring");
175+
assertThat(content.get("spring")).isInstanceOf(SpringInfo.class);
176+
});
177+
}
178+
167179
private Map<String, Object> invokeContributor(InfoContributor contributor) {
168180
Info.Builder builder = new Info.Builder();
169181
contributor.contribute(builder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2023 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.aot.hint.BindingReflectionHintsRegistrar;
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
22+
import org.springframework.boot.actuate.info.Info.Builder;
23+
import org.springframework.boot.actuate.info.SpringInfoContributor.SpringInfoContributorRuntimeHints;
24+
import org.springframework.boot.info.SpringInfo;
25+
import org.springframework.context.annotation.ImportRuntimeHints;
26+
import org.springframework.core.env.Environment;
27+
28+
/**
29+
* An {@link InfoContributor} that exposes info related to Spring projects.
30+
*
31+
* @author Jonatan Ivanov
32+
* @since 3.3.0
33+
*/
34+
@ImportRuntimeHints(SpringInfoContributorRuntimeHints.class)
35+
public class SpringInfoContributor implements InfoContributor {
36+
37+
private final SpringInfo springInfo;
38+
39+
public SpringInfoContributor(Environment environment) {
40+
this.springInfo = new SpringInfo(environment);
41+
}
42+
43+
@Override
44+
public void contribute(Builder builder) {
45+
builder.withDetail("spring", this.springInfo);
46+
}
47+
48+
static class SpringInfoContributorRuntimeHints implements RuntimeHintsRegistrar {
49+
50+
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
51+
52+
@Override
53+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
54+
this.bindingRegistrar.registerReflectionHints(hints.reflection(), SpringInfo.class);
55+
}
56+
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2023 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.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.boot.actuate.info.SpringInfoContributor.SpringInfoContributorRuntimeHints;
25+
import org.springframework.boot.info.SpringInfo;
26+
import org.springframework.mock.env.MockEnvironment;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link SpringInfoContributor}.
32+
*
33+
* @author Jonatan Ivanov
34+
*/
35+
class SpringInfoContributorTests {
36+
37+
@Test
38+
void springInfoShouldBeAdded() {
39+
SpringInfoContributor springInfoContributor = new SpringInfoContributor(new MockEnvironment());
40+
Info.Builder builder = new Info.Builder();
41+
springInfoContributor.contribute(builder);
42+
Info info = builder.build();
43+
assertThat(info.getDetails().get("spring")).isInstanceOf(SpringInfo.class);
44+
}
45+
46+
@Test
47+
void shouldRegisterHints() {
48+
RuntimeHints runtimeHints = new RuntimeHints();
49+
new SpringInfoContributorRuntimeHints().registerHints(runtimeHints, getClass().getClassLoader());
50+
assertThat(RuntimeHintsPredicates.reflection()
51+
.onType(SpringInfo.class)
52+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS))
53+
.accepts(runtimeHints);
54+
}
55+
56+
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,17 @@ When appropriate, Spring auto-configures the following `InfoContributor` beans:
10871087
| Exposes Operating System information.
10881088
| None.
10891089

1090+
| `spring`
1091+
| {spring-boot-actuator-module-code}/info/SpringInfoContributor.java[`SpringInfoContributor`]
1092+
| Exposes Spring-related information.
1093+
| None.
1094+
10901095
|===
10911096

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

1095-
With no prerequisites to indicate that they should be enabled, the `env`, `java`, and `os` contributors are disabled by default.
1100+
With no prerequisites to indicate that they should be enabled, the `env`, `java`, `os`, and `spring` contributors are disabled by default.
10961101
Each can be enabled by setting its `management.info.<id>.enabled` property to `true`.
10971102

10981103
The `build` and `git` info contributors are enabled by default.
@@ -1190,6 +1195,12 @@ The `info` endpoint publishes information about your Operating System, see {spri
11901195

11911196

11921197

1198+
[[actuator.endpoints.info.spring-information]]
1199+
==== Spring Information
1200+
The `info` endpoint publishes Spring-related information, see {spring-boot-module-api}/info/SpringInfo.html[`SpringInfo`] for more details.
1201+
1202+
1203+
11931204
[[actuator.endpoints.info.writing-custom-info-contributors]]
11941205
==== Writing Custom InfoContributors
11951206
To provide custom application information, you can register Spring beans that implement the {spring-boot-actuator-module-code}/info/InfoContributor.java[`InfoContributor`] interface.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2023 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.springframework.boot.SpringBootVersion;
20+
import org.springframework.core.SpringVersion;
21+
import org.springframework.core.env.Environment;
22+
import org.springframework.util.ObjectUtils;
23+
24+
/**
25+
* Information related to Spring projects.
26+
*
27+
* @author Jonatan Ivanov
28+
* @since 3.3.0
29+
*/
30+
public class SpringInfo {
31+
32+
private final SpringFrameworkInfo framework;
33+
34+
private final SpringBootInfo boot;
35+
36+
public SpringInfo(Environment environment) {
37+
this.framework = new SpringFrameworkInfo(environment);
38+
this.boot = new SpringBootInfo();
39+
}
40+
41+
public SpringFrameworkInfo getFramework() {
42+
return this.framework;
43+
}
44+
45+
public SpringBootInfo getBoot() {
46+
return this.boot;
47+
}
48+
49+
/**
50+
* Information about Spring Framework.
51+
*/
52+
public static class SpringFrameworkInfo {
53+
54+
private final String version;
55+
56+
private final String[] profiles;
57+
58+
public SpringFrameworkInfo(Environment environment) {
59+
this.version = SpringVersion.getVersion();
60+
this.profiles = ObjectUtils.isEmpty(environment.getActiveProfiles()) ? environment.getDefaultProfiles()
61+
: environment.getActiveProfiles();
62+
}
63+
64+
public String getVersion() {
65+
return this.version;
66+
}
67+
68+
public String[] getProfiles() {
69+
return this.profiles;
70+
}
71+
72+
}
73+
74+
/**
75+
* Information about Spring Boot.
76+
*/
77+
public static class SpringBootInfo {
78+
79+
private final String version;
80+
81+
public SpringBootInfo() {
82+
this.version = SpringBootVersion.getVersion();
83+
}
84+
85+
public String getVersion() {
86+
return this.version;
87+
}
88+
89+
}
90+
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2023 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.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.mockito.junit.jupiter.MockitoExtension;
23+
24+
import org.springframework.boot.SpringBootVersion;
25+
import org.springframework.core.SpringVersion;
26+
import org.springframework.mock.env.MockEnvironment;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link SpringInfo}.
32+
*
33+
* @author Jonatan Ivanov
34+
*/
35+
@ExtendWith(MockitoExtension.class)
36+
class SpringInfoTests {
37+
38+
private MockEnvironment environment;
39+
40+
@BeforeEach
41+
void setUp() {
42+
this.environment = new MockEnvironment();
43+
}
44+
45+
@Test
46+
void springInfoIsAvailableWithoutExplicitProfiles() {
47+
SpringInfo info = new SpringInfo(this.environment);
48+
assertThat(info.getFramework().getVersion()).isEqualTo(SpringVersion.getVersion());
49+
assertThat(info.getFramework().getProfiles()).containsExactly("default");
50+
assertThat(info.getBoot().getVersion()).isEqualTo(SpringBootVersion.getVersion());
51+
}
52+
53+
@Test
54+
void springInfoContainsDefaultProfilesIfDefaultProfileIsSet() {
55+
this.environment.setDefaultProfiles("test-default-profile");
56+
SpringInfo info = new SpringInfo(this.environment);
57+
assertThat(info.getFramework().getProfiles()).containsExactly("test-default-profile");
58+
}
59+
60+
@Test
61+
void springInfoContainsActiveProfilesIfActiveProfilesIsSet() {
62+
this.environment.setActiveProfiles("test-active-profile");
63+
SpringInfo info = new SpringInfo(this.environment);
64+
assertThat(info.getFramework().getProfiles()).containsExactly("test-active-profile");
65+
}
66+
67+
@Test
68+
void springInfoContainsActiveProfilesIfBothDefaultAndActiveProfilesAreSet() {
69+
this.environment.setDefaultProfiles("test-default-profile");
70+
this.environment.setActiveProfiles("test-active-profile");
71+
SpringInfo info = new SpringInfo(this.environment);
72+
assertThat(info.getFramework().getProfiles()).containsExactly("test-active-profile");
73+
}
74+
75+
}

0 commit comments

Comments
 (0)