Skip to content

Commit ad7c69a

Browse files
onobcsnicoll
authored andcommitted
Filter properties with a particular prefix
This commit improves the configprops endpoint to allow filtering properties based on a particular prefix See gh-24718
1 parent 0f9fb13 commit ad7c69a

File tree

7 files changed

+391
-11
lines changed

7 files changed

+391
-11
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ The `configprops` endpoint provides information about the application's `@Config
66

77

88
[[configprops-retrieving]]
9-
== Retrieving the @ConfigurationProperties Bean
9+
== Retrieving All @ConfigurationProperties Beans
1010

11-
To retrieve the `@ConfigurationProperties` beans, make a `GET` request to `/actuator/configprops`, as shown in the following curl-based example:
11+
To retrieve all of the `@ConfigurationProperties` beans, make a `GET` request to `/actuator/configprops`, as shown in the following curl-based example:
1212

13-
include::{snippets}/configprops/curl-request.adoc[]
13+
include::{snippets}/configprops/all/curl-request.adoc[]
1414

1515
The resulting response is similar to the following:
1616

17-
include::{snippets}/configprops/http-response.adoc[]
17+
include::{snippets}/configprops/all/http-response.adoc[]
1818

1919

2020

@@ -26,3 +26,26 @@ The following table describes the structure of the response:
2626

2727
[cols="2,1,3"]
2828
include::{snippets}/configprops/response-fields.adoc[]
29+
30+
31+
[[configprops-retrieving-by-prefix]]
32+
== Retrieving @ConfigurationProperties Beans By Prefix
33+
34+
To retrieve the `@ConfigurationProperties` beans mapped under a certain prefix, make a `GET` request to `/actuator/configprops/{prefix}`, as shown in the following curl-based example:
35+
36+
include::{snippets}/configprops/prefixed/curl-request.adoc[]
37+
38+
The resulting response is similar to the following:
39+
40+
include::{snippets}/configprops/prefixed/http-response.adoc[]
41+
42+
NOTE: The `{prefix}` does not need to be exact, a more general prefix will return all beans mapped under that prefix stem.
43+
44+
[[configprops-retrieving-by-prefix-response-structure]]
45+
=== Response Structure
46+
47+
The response contains details of the application's `@ConfigurationProperties` beans.
48+
The following table describes the structure of the response:
49+
50+
[cols="2,1,3"]
51+
include::{snippets}/configprops/prefixed/response-fields.adoc[]

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
2020
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint;
21+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpointWebExtension;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2325
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2426
import org.springframework.context.annotation.Bean;
@@ -30,6 +32,7 @@
3032
*
3133
* @author Phillip Webb
3234
* @author Stephane Nicoll
35+
* @author Chris Bono
3336
* @since 2.0.0
3437
*/
3538
@Configuration(proxyBeanMethods = false)
@@ -49,4 +52,12 @@ public ConfigurationPropertiesReportEndpoint configurationPropertiesReportEndpoi
4952
return endpoint;
5053
}
5154

55+
@Bean
56+
@ConditionalOnMissingBean
57+
@ConditionalOnBean(ConfigurationPropertiesReportEndpoint.class)
58+
public ConfigurationPropertiesReportEndpointWebExtension configurationPropertiesReportEndpointWebExtension(
59+
ConfigurationPropertiesReportEndpoint configurationPropertiesReportEndpoint) {
60+
return new ConfigurationPropertiesReportEndpointWebExtension(configurationPropertiesReportEndpoint);
61+
}
62+
5263
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/ConfigurationPropertiesReportEndpointDocumentationTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,31 @@
3636
* {@link ConfigurationPropertiesReportEndpoint}.
3737
*
3838
* @author Andy Wilkinson
39+
* @author Chris Bono
3940
*/
4041
class ConfigurationPropertiesReportEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
4142

4243
@Test
4344
void configProps() throws Exception {
4445
this.mockMvc.perform(get("/actuator/configprops")).andExpect(status().isOk())
45-
.andDo(MockMvcRestDocumentation.document("configprops",
46+
.andDo(MockMvcRestDocumentation.document("configprops/all",
47+
preprocessResponse(limit("contexts", getApplicationContext().getId(), "beans")),
48+
responseFields(fieldWithPath("contexts").description("Application contexts keyed by id."),
49+
fieldWithPath("contexts.*.beans.*")
50+
.description("`@ConfigurationProperties` beans keyed by bean name."),
51+
fieldWithPath("contexts.*.beans.*.prefix")
52+
.description("Prefix applied to the names of the bean's properties."),
53+
subsectionWithPath("contexts.*.beans.*.properties")
54+
.description("Properties of the bean as name-value pairs."),
55+
subsectionWithPath("contexts.*.beans.*.inputs").description(
56+
"Origin and value of the configuration property used when binding to this bean."),
57+
parentIdField())));
58+
}
59+
60+
@Test
61+
void configPropsFilterByPrefix() throws Exception {
62+
this.mockMvc.perform(get("/actuator/configprops/spring.resources")).andExpect(status().isOk())
63+
.andDo(MockMvcRestDocumentation.document("configprops/prefixed",
4664
preprocessResponse(limit("contexts", getApplicationContext().getId(), "beans")),
4765
responseFields(fieldWithPath("contexts").description("Application contexts keyed by id."),
4866
fieldWithPath("contexts.*.beans.*")

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashMap;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.function.Predicate;
2930
import java.util.stream.Collectors;
3031

3132
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -55,6 +56,7 @@
5556
import org.springframework.boot.actuate.endpoint.Sanitizer;
5657
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
5758
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
59+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
5860
import org.springframework.boot.context.properties.BoundConfigurationProperties;
5961
import org.springframework.boot.context.properties.ConfigurationProperties;
6062
import org.springframework.boot.context.properties.ConfigurationPropertiesBean;
@@ -90,6 +92,7 @@
9092
* @author Stephane Nicoll
9193
* @author Madhura Bhave
9294
* @author Andy Wilkinson
95+
* @author Chris Bono
9396
* @since 2.0.0
9497
*/
9598
@Endpoint(id = "configprops")
@@ -114,15 +117,21 @@ public void setKeysToSanitize(String... keysToSanitize) {
114117

115118
@ReadOperation
116119
public ApplicationConfigurationProperties configurationProperties() {
117-
return extract(this.context);
120+
return extract(this.context, (bean) -> true);
118121
}
119122

120-
private ApplicationConfigurationProperties extract(ApplicationContext context) {
123+
@ReadOperation
124+
public ApplicationConfigurationProperties configurationProperties(@Selector String prefix) {
125+
return extract(this.context, (bean) -> bean.getAnnotation().prefix().startsWith(prefix));
126+
}
127+
128+
private ApplicationConfigurationProperties extract(ApplicationContext context,
129+
Predicate<ConfigurationPropertiesBean> beanFilterPredicate) {
121130
ObjectMapper mapper = getObjectMapper();
122131
Map<String, ContextConfigurationProperties> contexts = new HashMap<>();
123132
ApplicationContext target = context;
124133
while (target != null) {
125-
contexts.put(target.getId(), describeBeans(mapper, target));
134+
contexts.put(target.getId(), describeBeans(mapper, target, beanFilterPredicate));
126135
target = target.getParent();
127136
}
128137
return new ApplicationConfigurationProperties(contexts);
@@ -169,10 +178,14 @@ private void applySerializationModifier(ObjectMapper mapper) {
169178
mapper.setSerializerFactory(factory);
170179
}
171180

172-
private ContextConfigurationProperties describeBeans(ObjectMapper mapper, ApplicationContext context) {
181+
private ContextConfigurationProperties describeBeans(ObjectMapper mapper, ApplicationContext context,
182+
Predicate<ConfigurationPropertiesBean> beanFilterPredicate) {
173183
Map<String, ConfigurationPropertiesBean> beans = ConfigurationPropertiesBean.getAll(context);
174-
Map<String, ConfigurationPropertiesBeanDescriptor> descriptors = new HashMap<>();
175-
beans.forEach((beanName, bean) -> descriptors.put(beanName, describeBean(mapper, bean)));
184+
185+
Map<String, ConfigurationPropertiesBeanDescriptor> descriptors = beans.values().stream()
186+
.filter(beanFilterPredicate::test)
187+
.collect(Collectors.toMap((bean) -> bean.getName(), (bean) -> describeBean(mapper, bean)));
188+
176189
return new ContextConfigurationProperties(descriptors,
177190
(context.getParent() != null) ? context.getParent().getId() : null);
178191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2020 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.context.properties;
18+
19+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ApplicationConfigurationProperties;
20+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ContextConfigurationProperties;
21+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
22+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
23+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
24+
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
25+
26+
/**
27+
* {@link EndpointWebExtension @EndpointWebExtension} for the
28+
* {@link ConfigurationPropertiesReportEndpoint}.
29+
*
30+
* @author Chris Bono
31+
* @since 2.4
32+
*/
33+
@EndpointWebExtension(endpoint = ConfigurationPropertiesReportEndpoint.class)
34+
public class ConfigurationPropertiesReportEndpointWebExtension {
35+
36+
private final ConfigurationPropertiesReportEndpoint delegate;
37+
38+
public ConfigurationPropertiesReportEndpointWebExtension(ConfigurationPropertiesReportEndpoint delegate) {
39+
this.delegate = delegate;
40+
}
41+
42+
@ReadOperation
43+
public WebEndpointResponse<ApplicationConfigurationProperties> configurationProperties(@Selector String prefix) {
44+
ApplicationConfigurationProperties configurationProperties = this.delegate.configurationProperties(prefix);
45+
boolean foundMatchingBeans = configurationProperties.getContexts().values().stream()
46+
.map(ContextConfigurationProperties::getBeans).anyMatch((beans) -> !beans.isEmpty());
47+
return (foundMatchingBeans) ? new WebEndpointResponse<>(configurationProperties, WebEndpointResponse.STATUS_OK)
48+
: new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND);
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2012-2019 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.context.properties;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ApplicationConfigurationProperties;
22+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ContextConfigurationProperties;
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link ConfigurationPropertiesReportEndpoint} when filtering by prefix.
33+
*
34+
* @author Chris Bono
35+
*/
36+
class ConfigurationPropertiesReportEndpointFilteringTests {
37+
38+
@Test
39+
void filterByPrefixSingleMatch() {
40+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
41+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
42+
contextRunner.run((context) -> {
43+
ConfigurationPropertiesReportEndpoint endpoint = context
44+
.getBean(ConfigurationPropertiesReportEndpoint.class);
45+
ApplicationConfigurationProperties applicationProperties = endpoint.configurationProperties("only.bar");
46+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
47+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
48+
assertThat(contextProperties.getBeans().values()).hasSize(1).first().hasFieldOrPropertyWithValue("prefix",
49+
"only.bar");
50+
});
51+
}
52+
53+
@Test
54+
void filterByPrefixMultipleMatches() {
55+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
56+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
57+
contextRunner.run((context) -> {
58+
ConfigurationPropertiesReportEndpoint endpoint = context
59+
.getBean(ConfigurationPropertiesReportEndpoint.class);
60+
ApplicationConfigurationProperties applicationProperties = endpoint.configurationProperties("foo.");
61+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
62+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
63+
assertThat(contextProperties.getBeans()).containsOnlyKeys("primaryFoo", "secondaryFoo");
64+
});
65+
}
66+
67+
@Test
68+
void filterByPrefixNoMatches() {
69+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
70+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
71+
contextRunner.run((context) -> {
72+
ConfigurationPropertiesReportEndpoint endpoint = context
73+
.getBean(ConfigurationPropertiesReportEndpoint.class);
74+
ApplicationConfigurationProperties applicationProperties = endpoint.configurationProperties("foo.third");
75+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
76+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
77+
assertThat(contextProperties.getBeans()).isEmpty();
78+
});
79+
}
80+
81+
@Configuration(proxyBeanMethods = false)
82+
@EnableConfigurationProperties(Bar.class)
83+
static class Config {
84+
85+
@Bean
86+
ConfigurationPropertiesReportEndpoint endpoint() {
87+
return new ConfigurationPropertiesReportEndpoint();
88+
}
89+
90+
@Bean
91+
@ConfigurationProperties(prefix = "foo.primary")
92+
Foo primaryFoo() {
93+
return new Foo();
94+
}
95+
96+
@Bean
97+
@ConfigurationProperties(prefix = "foo.secondary")
98+
Foo secondaryFoo() {
99+
return new Foo();
100+
}
101+
102+
}
103+
104+
public static class Foo {
105+
106+
private String name = "5150";
107+
108+
public String getName() {
109+
return this.name;
110+
}
111+
112+
public void setName(String name) {
113+
this.name = name;
114+
}
115+
116+
}
117+
118+
@ConfigurationProperties(prefix = "only.bar")
119+
public static class Bar {
120+
121+
private String name = "123456";
122+
123+
public String getName() {
124+
return this.name;
125+
}
126+
127+
public void setName(String name) {
128+
this.name = name;
129+
}
130+
131+
}
132+
133+
}

0 commit comments

Comments
 (0)