Move endpoints.health.mapping to management.health.status.http-mapping

Closes gh-10052
This commit is contained in:
Stephane Nicoll 2017-08-22 11:11:22 +02:00
parent f2a74946af
commit 3087514b79
12 changed files with 40 additions and 73 deletions

View File

@ -1,49 +0,0 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.endpoint.web;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.endpoint.web.HealthWebEndpointExtension;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for the {@link HealthWebEndpointExtension}.
*
* @author Christian Dupuis
* @author Andy Wilkinson
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "endpoints.health")
public class HealthWebEndpointExtensionProperties {
/**
* Mapping of health statuses to HttpStatus codes. By default, registered health
* statuses map to sensible defaults (i.e. UP maps to 200).
*/
private Map<String, Integer> mapping = new HashMap<>();
public Map<String, Integer> getMapping() {
return this.mapping;
}
public void setMapping(Map<String, Integer> mapping) {
this.mapping = mapping;
}
}

View File

@ -18,15 +18,16 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web;
import org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorProperties;
import org.springframework.boot.actuate.endpoint.AuditEventsEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.StatusEndpoint;
import org.springframework.boot.actuate.endpoint.web.AuditEventsWebEndpointExtension;
import org.springframework.boot.actuate.endpoint.web.HealthStatusHttpMapper;
import org.springframework.boot.actuate.endpoint.web.HealthWebEndpointExtension;
import org.springframework.boot.actuate.endpoint.web.HeapDumpWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.LogFileWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.StatusWebEndpointExtension;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -48,7 +49,7 @@ import org.springframework.util.StringUtils;
* @since 2.0.0
*/
@ManagementContextConfiguration
@EnableConfigurationProperties(HealthWebEndpointExtensionProperties.class)
@EnableConfigurationProperties(HealthIndicatorProperties.class)
public class WebEndpointManagementContextConfiguration {
@Bean
@ -63,9 +64,8 @@ public class WebEndpointManagementContextConfiguration {
@ConditionalOnEnabledEndpoint
@ConditionalOnBean(value = HealthEndpoint.class, search = SearchStrategy.CURRENT)
public HealthWebEndpointExtension healthWebEndpointExtension(HealthEndpoint delegate,
HealthWebEndpointExtensionProperties extensionProperties) {
return new HealthWebEndpointExtension(delegate,
createHealthStatusHttpMapper(extensionProperties));
HealthStatusHttpMapper healthStatusHttpMapper) {
return new HealthWebEndpointExtension(delegate, healthStatusHttpMapper);
}
@Bean
@ -73,16 +73,17 @@ public class WebEndpointManagementContextConfiguration {
@ConditionalOnEnabledEndpoint
@ConditionalOnBean(value = StatusEndpoint.class, search = SearchStrategy.CURRENT)
public StatusWebEndpointExtension statusWebEndpointExtension(StatusEndpoint delegate,
HealthWebEndpointExtensionProperties extensionProperties) {
return new StatusWebEndpointExtension(delegate,
createHealthStatusHttpMapper(extensionProperties));
HealthStatusHttpMapper healthStatusHttpMapper) {
return new StatusWebEndpointExtension(delegate, healthStatusHttpMapper);
}
private HealthStatusHttpMapper createHealthStatusHttpMapper(
HealthWebEndpointExtensionProperties extensionProperties) {
@Bean
@ConditionalOnMissingBean
public HealthStatusHttpMapper createHealthStatusHttpMapper(
HealthIndicatorProperties healthIndicatorProperties) {
HealthStatusHttpMapper statusHttpMapper = new HealthStatusHttpMapper();
if (extensionProperties.getMapping() != null) {
statusHttpMapper.addStatusMapping(extensionProperties.getMapping());
if (healthIndicatorProperties.getHttpMapping() != null) {
statusHttpMapper.addStatusMapping(healthIndicatorProperties.getHttpMapping());
}
return statusHttpMapper;
}

View File

@ -16,7 +16,9 @@
package org.springframework.boot.actuate.autoconfigure.health;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -34,6 +36,12 @@ public class HealthIndicatorProperties {
*/
private List<String> order = null;
/**
* Mapping of health statuses to HttpStatus codes. By default, registered health
* statuses map to sensible defaults (i.e. UP maps to 200).
*/
private final Map<String, Integer> httpMapping = new HashMap<>();
public List<String> getOrder() {
return this.order;
}
@ -44,4 +52,8 @@ public class HealthIndicatorProperties {
}
}
public Map<String, Integer> getHttpMapping() {
return this.httpMapping;
}
}

View File

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.web;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.endpoint.ReadOperation;
import org.springframework.boot.endpoint.web.WebEndpointExtension;
import org.springframework.boot.endpoint.web.WebEndpointResponse;

View File

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.web;
import org.springframework.boot.actuate.endpoint.StatusEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.endpoint.ReadOperation;
import org.springframework.boot.endpoint.web.WebEndpointExtension;
import org.springframework.boot.endpoint.web.WebEndpointResponse;

View File

@ -14,13 +14,12 @@
* limitations under the License.
*/
package org.springframework.boot.actuate.endpoint.web;
package org.springframework.boot.actuate.health;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.health.Status;
import org.springframework.util.Assert;
/**
@ -108,8 +107,6 @@ public class HealthStatusHttpMapper {
return 200;
}
private String getUniformValue(String code) {
if (code == null) {
return null;

View File

@ -25,12 +25,12 @@ import org.springframework.boot.actuate.endpoint.AuditEventsEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.StatusEndpoint;
import org.springframework.boot.actuate.endpoint.web.AuditEventsWebEndpointExtension;
import org.springframework.boot.actuate.endpoint.web.HealthStatusHttpMapper;
import org.springframework.boot.actuate.endpoint.web.HealthWebEndpointExtension;
import org.springframework.boot.actuate.endpoint.web.HeapDumpWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.LogFileWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.StatusWebEndpointExtension;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
@ -68,7 +68,7 @@ public class WebEndpointManagementContextConfigurationTests {
@Test
public void healthStatusMappingCanBeCustomized() {
ApplicationContextRunner contextRunner = contextRunner()
.withPropertyValues("endpoints.health.mapping.CUSTOM=500")
.withPropertyValues("management.health.status.http-mapping.CUSTOM=500")
.withUserConfiguration(HealthEndpointConfiguration.class);
contextRunner.run((context) -> {
HealthWebEndpointExtension extension = context
@ -97,7 +97,7 @@ public class WebEndpointManagementContextConfigurationTests {
@Test
public void statusMappingCanBeCustomized() {
ApplicationContextRunner contextRunner = contextRunner()
.withPropertyValues("endpoints.health.mapping.CUSTOM=500")
.withPropertyValues("management.health.status.http-mapping.CUSTOM=500")
.withUserConfiguration(StatusEndpointConfiguration.class);
contextRunner.run((context) -> {
StatusWebEndpointExtension extension = context

View File

@ -26,11 +26,11 @@ import org.springframework.boot.actuate.autoconfigure.ManagementContextAutoConfi
import org.springframework.boot.actuate.autoconfigure.endpoint.infrastructure.EndpointInfrastructureAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.infrastructure.ServletEndpointAutoConfiguration;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.web.HealthStatusHttpMapper;
import org.springframework.boot.actuate.endpoint.web.HealthWebEndpointExtension;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorFactory;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;

View File

@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorFactory;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

View File

@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.StatusEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorFactory;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

View File

@ -1139,7 +1139,6 @@ content into your application; rather pick only the properties that you need.
endpoints.health.cache.time-to-live=0 # Maximum time in milliseconds that a response can be cached.
endpoints.health.enabled=true # Enable the health endpoint.
endpoints.health.jmx.enabled=true # Expose the health endpoint as a JMX MBean.
endpoints.health.mapping= # Mapping of health statuses to HttpStatus codes. By default, registered health statuses map to sensible defaults (i.e. UP maps to 200).
endpoints.health.web.enabled=true # Expose the health endpoint as a Web endpoint.
# HEAP DUMP ENDPOINT ({sc-spring-boot-actuator}/endpoint/HeapDumpWebEndpoint.{sc-ext}[HeapDumpWebEndpoint])
@ -1258,6 +1257,7 @@ content into your application; rather pick only the properties that you need.
management.health.rabbit.enabled=true # Enable RabbitMQ health check.
management.health.redis.enabled=true # Enable Redis health check.
management.health.solr.enabled=true # Enable Solr health check.
management.health.status.http-mapping= # Mapping of health statuses to HttpStatus codes. By default, registered health statuses map to sensible defaults (i.e. UP maps to 200).
management.health.status.order=DOWN, OUT_OF_SERVICE, UP, UNKNOWN # Comma-separated list of health statuses in order of severity.
# INFO CONTRIBUTORS ({sc-spring-boot-actuator}/autoconfigure/InfoContributorProperties.{sc-ext}[InfoContributorProperties])

View File

@ -383,14 +383,16 @@ to your application properties:
The HTTP status code in the response reflects the overall health status (e.g. `UP`
maps to 200, `OUT_OF_SERVICE` or `DOWN` to 503). You might also want to register custom
status mappings with the `HealthMvcEndpoint` if you access the health endpoint over HTTP.
For example, the following maps `FATAL` to `HttpStatus.SERVICE_UNAVAILABLE`:
status mappings if you access the health endpoint over HTTP. For example, the following
maps `FATAL` to `HttpStatus.SERVICE_UNAVAILABLE`:
[source,properties,indent=0]
----
endpoints.health.mapping.FATAL=503
management.health.status.http-mapping.FATAL=503
----
TIP: If you need more control you can define your own `HealthStatusHttpMapper` bean.
The default status mappings for the built-in statuses are:
[cols="1,3"]