Commit 6cb331ed authored by Phillip Webb's avatar Phillip Webb

Cache endpoint enabled-by-default result

Update `OnEnabledEndpointCondition` so that the result of
`management.endpoints.enabled-by-default` is cached between calls.

Fixes gh-11407
parent 7f0048a8
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.endpoint.condition; package org.springframework.boot.actuate.autoconfigure.endpoint.condition;
import java.util.Optional;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.EndpointExtension; import org.springframework.boot.actuate.endpoint.annotation.EndpointExtension;
import org.springframework.boot.autoconfigure.condition.ConditionMessage; import org.springframework.boot.autoconfigure.condition.ConditionMessage;
...@@ -25,10 +27,12 @@ import org.springframework.context.annotation.Bean; ...@@ -25,10 +27,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.MethodMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
/** /**
* A condition that checks if an endpoint is enabled. * A condition that checks if an endpoint is enabled.
...@@ -42,22 +46,23 @@ class OnEnabledEndpointCondition extends SpringBootCondition { ...@@ -42,22 +46,23 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
private static final String ENABLED_BY_DEFAULT_KEY = "management.endpoints.enabled-by-default"; private static final String ENABLED_BY_DEFAULT_KEY = "management.endpoints.enabled-by-default";
private static final ConcurrentReferenceHashMap<Environment, Optional<Boolean>> enabledByDefaultCache = new ConcurrentReferenceHashMap<>();
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
AnnotationAttributes attributes = getEndpointAttributes(context, metadata); AnnotationAttributes attributes = getEndpointAttributes(context, metadata);
String id = attributes.getString("id"); String id = attributes.getString("id");
String key = "management.endpoint." + id + ".enabled"; String key = "management.endpoint." + id + ".enabled";
Boolean userDefinedEnabled = context.getEnvironment().getProperty(key, Boolean userDefinedEnabled = environment.getProperty(key, Boolean.class);
Boolean.class);
if (userDefinedEnabled != null) { if (userDefinedEnabled != null) {
return new ConditionOutcome(userDefinedEnabled, return new ConditionOutcome(userDefinedEnabled,
ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class) ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class)
.because("found property " + key + " with value " .because("found property " + key + " with value "
+ userDefinedEnabled)); + userDefinedEnabled));
} }
Boolean userDefinedDefault = context.getEnvironment() Boolean userDefinedDefault = isEnabledByDefault(environment);
.getProperty(ENABLED_BY_DEFAULT_KEY, Boolean.class);
if (userDefinedDefault != null) { if (userDefinedDefault != null) {
return new ConditionOutcome(userDefinedDefault, return new ConditionOutcome(userDefinedDefault,
ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class) ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class)
...@@ -71,6 +76,16 @@ class OnEnabledEndpointCondition extends SpringBootCondition { ...@@ -71,6 +76,16 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
"no property " + key + " found so using endpoint default")); "no property " + key + " found so using endpoint default"));
} }
private Boolean isEnabledByDefault(Environment environment) {
Optional<Boolean> enabledByDefault = enabledByDefaultCache.get(environment);
if (enabledByDefault == null) {
enabledByDefault = Optional.ofNullable(
environment.getProperty(ENABLED_BY_DEFAULT_KEY, Boolean.class));
enabledByDefaultCache.put(environment, enabledByDefault);
}
return enabledByDefault.orElse(null);
}
private AnnotationAttributes getEndpointAttributes(ConditionContext context, private AnnotationAttributes getEndpointAttributes(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
Assert.state( Assert.state(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment