This commit is contained in:
Phillip Webb
2014-05-22 20:32:36 +01:00
parent 94a255074f
commit 1a475102de
43 changed files with 212 additions and 132 deletions

View File

@@ -37,9 +37,9 @@ import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
import org.springframework.boot.actuate.endpoint.VanillaPublicMetrics;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;

View File

@@ -208,7 +208,6 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
if (this.applicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.applicationContext)
.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
if (event.getApplicationContext() == EndpointWebMvcAutoConfiguration.this.applicationContext) {

View File

@@ -54,7 +54,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
*
*
* @author Christian Dupuis
* @author Andy Wilkinson
* @since 1.1.0

View File

@@ -41,4 +41,5 @@ public class HealthMvcEndpointProperties {
public void setMapping(Map<String, HttpStatus> mapping) {
this.mapping = mapping;
}
}
}

View File

@@ -31,22 +31,53 @@ import org.springframework.web.bind.annotation.ResponseBody;
/**
* Adapter to expose {@link HealthEndpoint} as an {@link MvcEndpoint}.
*
*
* @author Christian Dupuis
* @since 1.1.0
*/
public class HealthMvcEndpoint extends EndpointMvcAdapter {
private Map<String, HttpStatus> statusMapping;
private Map<String, HttpStatus> statusMapping = new HashMap<String, HttpStatus>();
public HealthMvcEndpoint(HealthEndpoint delegate) {
super(delegate);
setupDefaultStatusMapping();
}
private void setupDefaultStatusMapping() {
addStatusMapping(Status.DOWN, HttpStatus.SERVICE_UNAVAILABLE);
addStatusMapping(Status.OUT_OF_SERVICE, HttpStatus.SERVICE_UNAVAILABLE);
}
/**
* Set specific status mappings
* @param statusMapping a map of status code to {@link HttpStatus}
*/
public void setStatusMapping(Map<String, HttpStatus> statusMapping) {
Assert.notNull(statusMapping, "StatusMapping must not be null");
this.statusMapping = statusMapping;
this.statusMapping = new HashMap<String, HttpStatus>(statusMapping);
}
/**
* Add a status mapping to the existing set
* @param status the status to map
* @param httpStatus the http status
*/
public void addStatusMapping(Status status, HttpStatus httpStatus) {
Assert.notNull(status, "Status must not be null");
Assert.notNull(httpStatus, "HttpStatus must not be null");
addStatusMapping(status.getCode(), httpStatus);
}
/**
* Add a status mapping to the existing set
* @param statusCode the status code to map
* @param httpStatus the http status
*/
public void addStatusMapping(String statusCode, HttpStatus httpStatus) {
Assert.notNull(statusCode, "StatusCode must not be null");
Assert.notNull(httpStatus, "HttpStatus must not be null");
this.statusMapping.put(statusCode, httpStatus);
}
@RequestMapping
@@ -62,16 +93,10 @@ public class HealthMvcEndpoint extends EndpointMvcAdapter {
Health health = (Health) getDelegate().invoke();
Status status = health.getStatus();
if (this.statusMapping.containsKey(status.getCode())) {
return new ResponseEntity<Health>(health,
this.statusMapping.get(status.getCode()));
return new ResponseEntity<Health>(health, this.statusMapping.get(status
.getCode()));
}
return health;
}
private void setupDefaultStatusMapping() {
this.statusMapping = new HashMap<String, HttpStatus>();
this.statusMapping.put(Status.DOWN.getCode(), HttpStatus.SERVICE_UNAVAILABLE);
this.statusMapping.put(Status.OUT_OF_SERVICE.getCode(),
HttpStatus.SERVICE_UNAVAILABLE);
}
}

View File

@@ -32,15 +32,15 @@ public abstract class AbstractHealthAggregator implements HealthAggregator {
@Override
public final Health aggregate(Map<String, Health> healths) {
Health health = new Health();
List<Status> status = new ArrayList<Status>();
for (Map.Entry<String, Health> h : healths.entrySet()) {
health.withDetail(h.getKey(), h.getValue());
status.add(h.getValue().getStatus());
for (Map.Entry<String, Health> entry : healths.entrySet()) {
health.withDetail(entry.getKey(), entry.getValue());
status.add(entry.getValue().getStatus());
}
health.status(aggregateStatus(status));
return health;
}
protected abstract Status aggregateStatus(List<Status> status);
}

View File

@@ -19,7 +19,6 @@ package org.springframework.boot.actuate.health;
/**
* Base {@link HealthIndicator} implementations that encapsulates creation of
* {@link Health} instance and error handling.
*
* <p>
* This implementation is only suitable if an {@link Exception} raised from
* {@link #doHealthCheck(Health)} should create a {@link Status#DOWN} health status.

View File

@@ -31,27 +31,24 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
/**
* Value object used to carry information about the health information of a component or
* subsystem.
*
* <p>
* {@link Health} contains a {@link Status} to express the state of a component or
* subsystem and some additional details to carry some contextual information.
*
* <p>
* {@link Health} has a fluent API to make it easy to construct instances. Typical usage
* in a {@link HealthIndicator} would be:
*
* <code>
* Health health = new Health();
* try {
* // do some test to determine state of component
*
* health.up().withDetail("version", "1.1.2");
* }
* catch (Exception ex) {
* health.down().withException(ex);
* }
* return health;
* </code>
* <pre class="code">
* Health health = new Health();
* try {
* // do some test to determine state of component
* health.up().withDetail(&quot;version&quot;, &quot;1.1.2&quot;);
* }
* catch (Exception ex) {
* health.down().withException(ex);
* }
* return health;
* </pre>
*
* @author Christian Dupuis
* @since 1.1.0
@@ -64,7 +61,7 @@ public class Health {
private Map<String, Object> details;
public Health() {
this(Status.UNKOWN);
this(Status.UNKNOWN);
}
public Health(Status status) {

View File

@@ -21,13 +21,11 @@ import java.util.Map;
/**
* Strategy interface used by {@link CompositeHealthIndicator} to aggregate {@link Health}
* instances into a final one.
*
* <p>
* This is especially useful to combine subsystem states expressed through
* {@link Health#getStatus()} into one state for the entire system. The default
* implementation {@link OrderedHealthAggregator} sorts {@link Status} instances based on
* a priority list.
*
* <p>
* It is possible to add more complex {@link Status} types to the system. In that case
* either the {@link OrderedHealthAggregator} needs to be properly configured or users

View File

@@ -21,38 +21,64 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.springframework.util.Assert;
/**
* Default {@link HealthAggregator} implementation that aggregates {@link Health}
* instances and determines the final system state based on a simple ordered list.
*
* <p>
* If a different order is required or a new {@link Status} type will be used, the order
* can be set by calling {@link #setStatusOrder(List)}.
*
*
* @author Christian Dupuis
* @since 1.1.0
*/
public class OrderedHealthAggregator extends AbstractHealthAggregator {
private List<String> statusOrder = Arrays.asList("DOWN", "OUT_OF_SERVICE", "UP",
"UNKOWN");
private List<String> statusOrder;
/**
* Create a new {@link OrderedHealthAggregator} instance.
*/
public OrderedHealthAggregator() {
setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN);
}
/**
* Set the ordering of the status.
* @param statusOrder an ordered list of the status
*/
public void setStatusOrder(Status... statusOrder) {
String[] order = new String[statusOrder.length];
for (int i = 0; i < statusOrder.length; i++) {
order[i] = statusOrder[i].getCode();
}
setStatusOrder(Arrays.asList(order));
}
/**
* Set the ordering of the status.
* @param statusOrder an ordered list of the status codes
*/
public void setStatusOrder(List<String> statusOrder) {
Assert.notNull(statusOrder, "StatusOrder must not be null");
this.statusOrder = statusOrder;
}
@Override
protected Status aggregateStatus(List<Status> status) {
// If no status is given return UNKOWN
// If no status is given return UNKNOWN
if (status.size() == 0) {
return Status.UNKOWN;
return Status.UNKNOWN;
}
// Sort given Status instances by configured order
Collections.sort(status, new StatusComparator(this.statusOrder));
return status.get(0);
}
/**
* {@link Comparator} used to order {@link Status}.
*/
private class StatusComparator implements Comparator<Status> {
private final List<String> statusOrder;
@@ -63,8 +89,9 @@ public class OrderedHealthAggregator extends AbstractHealthAggregator {
@Override
public int compare(Status s1, Status s2) {
return Integer.valueOf(this.statusOrder.indexOf(s1.getCode())).compareTo(
Integer.valueOf(this.statusOrder.indexOf(s2.getCode())));
int i1 = this.statusOrder.indexOf(s1.getCode());
int i2 = this.statusOrder.indexOf(s2.getCode());
return (i1 < i2 ? -1 : (i1 == i2 ? s1.getCode().compareTo(s2.getCode()) : 1));
}
}

View File

@@ -25,11 +25,9 @@ import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Value object to express state of a component or subsystem.
*
* <p>
* Status provides convenient constants for commonly used states like {@link #UP},
* {@link #DOWN} or {@link #OUT_OF_SERVICE}.
*
* <p>
* Custom states can also be created and used throughout the Spring Boot Health subsystem.
*
@@ -42,7 +40,7 @@ public class Status {
/**
* Convenient constant value representing unknown state
*/
public static final Status UNKOWN = new Status("UNKOWN");
public static final Status UNKNOWN = new Status("UNKNOWN");
/**
* Convenient constant value representing up state
@@ -63,10 +61,19 @@ public class Status {
private final String description;
/**
* Create a new {@link Status} instance with the given code and an empty description.
* @param code the status code
*/
public Status(String code) {
this(code, "");
}
/**
* Create a new {@link Status} instance with the given code and description.
* @param code the status code
* @param description a description of the status
*/
public Status(String code, String description) {
Assert.notNull(code, "Code must not be null");
Assert.notNull(description, "Description must not be null");
@@ -74,16 +81,32 @@ public class Status {
this.description = description;
}
/**
* @return the code for this status
*/
@JsonProperty("status")
public String getCode() {
return this.code;
}
/**
* @return the description of this status
*/
@JsonInclude(Include.NON_EMPTY)
public String getDescription() {
return this.description;
}
@Override
public String toString() {
return this.code;
}
@Override
public int hashCode() {
return this.code.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
@@ -95,8 +118,4 @@ public class Status {
return false;
}
@Override
public int hashCode() {
return this.code.hashCode();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2014 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.
@@ -29,7 +29,6 @@ import org.springframework.boot.actuate.metrics.repository.MultiMetricRepository
* has been populated using that exporter.
*
* @author Dave Syer
*
* @since 1.1.0
*/
public class MultiMetricRichGaugeReader implements RichGaugeReader {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -25,6 +25,7 @@ import org.springframework.boot.actuate.metrics.Metric;
* name prefix (their group name).
*
* @author Dave Syer
* @since 1.1.0
*/
public interface PrefixMetricWriter {

View File

@@ -33,7 +33,7 @@ import static org.mockito.Mockito.when;
/**
* Tests for {@link HealthMvcEndpoint}.
*
*
* @author Christian Dupuis
*/
public class HealthMvcEndpointTests {

View File

@@ -123,8 +123,9 @@ public class CompositeHealthIndicatorTests {
Health result = composite.health();
ObjectMapper mapper = new ObjectMapper();
assertEquals(
"{\"status\":\"UNKOWN\",\"db\":{\"status\":\"UNKOWN\",\"db1\":{\"status\":\"UNKOWN\",\"1\":\"1\"},\"db2\":{\"status\":\"UNKOWN\",\"2\":\"2\"}}}",
assertEquals("{\"status\":\"UNKNOWN\",\"db\":{\"status\":\"UNKNOWN\""
+ ",\"db1\":{\"status\":\"UNKNOWN\",\"1\":\"1\"},"
+ "\"db2\":{\"status\":\"UNKNOWN\",\"2\":\"2\"}}}",
mapper.writeValueAsString(result));
}

View File

@@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
/**
* Tests for {@link MongoHealthIndicator}.
*
*
* @author Christian Dupuis
*/
public class MongoHealthIndicatorTests {

View File

@@ -27,7 +27,7 @@ import static org.junit.Assert.assertEquals;
/**
* Tests for {@link OrderedHealthAggregator}.
*
*
* @author Christian Dupuis
*/
public class OrderedHealthAggregatorTests {
@@ -40,35 +40,47 @@ public class OrderedHealthAggregatorTests {
}
@Test
public void testDefaultOrdering() {
public void defaultOrder() {
Map<String, Health> healths = new HashMap<String, Health>();
healths.put("h1", new Health(Status.DOWN));
healths.put("h2", new Health(Status.UP));
healths.put("h3", new Health(Status.UNKOWN));
healths.put("h3", new Health(Status.UNKNOWN));
healths.put("h4", new Health(Status.OUT_OF_SERVICE));
assertEquals(Status.DOWN, this.healthAggregator.aggregate(healths).getStatus());
}
@Test
public void testDefaultOrderingWithCustomStatus() {
public void customOrder() {
this.healthAggregator.setStatusOrder(Status.UNKNOWN, Status.UP,
Status.OUT_OF_SERVICE, Status.DOWN);
Map<String, Health> healths = new HashMap<String, Health>();
healths.put("h1", new Health(Status.DOWN));
healths.put("h2", new Health(Status.UP));
healths.put("h3", new Health(Status.UNKOWN));
healths.put("h3", new Health(Status.UNKNOWN));
healths.put("h4", new Health(Status.OUT_OF_SERVICE));
healths.put("h5", new Health(new Status("CUSTOM")));
assertEquals(new Status("CUSTOM"),
this.healthAggregator.aggregate(healths).getStatus());
assertEquals(Status.UNKNOWN, this.healthAggregator.aggregate(healths).getStatus());
}
@Test
public void testDefaultOrderingWithCustomStatusAndOrder() {
this.healthAggregator.setStatusOrder(Arrays.asList("DOWN", "OUT_OF_SERVICE",
"UP", "UNKOWN", "CUSTOM"));
public void defaultOrderWithCustomStatus() {
Map<String, Health> healths = new HashMap<String, Health>();
healths.put("h1", new Health(Status.DOWN));
healths.put("h2", new Health(Status.UP));
healths.put("h3", new Health(Status.UNKOWN));
healths.put("h3", new Health(Status.UNKNOWN));
healths.put("h4", new Health(Status.OUT_OF_SERVICE));
healths.put("h5", new Health(new Status("CUSTOM")));
assertEquals(new Status("CUSTOM"), this.healthAggregator.aggregate(healths)
.getStatus());
}
@Test
public void customOrderWithCustomStatus() {
this.healthAggregator.setStatusOrder(Arrays.asList("DOWN", "OUT_OF_SERVICE",
"UP", "UNKNOWN", "CUSTOM"));
Map<String, Health> healths = new HashMap<String, Health>();
healths.put("h1", new Health(Status.DOWN));
healths.put("h2", new Health(Status.UP));
healths.put("h3", new Health(Status.UNKNOWN));
healths.put("h4", new Health(Status.OUT_OF_SERVICE));
healths.put("h5", new Health(new Status("CUSTOM")));
assertEquals(Status.DOWN, this.healthAggregator.aggregate(healths).getStatus());

View File

@@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
/**
* Tests for {@link RedisHealthIndicator}.
*
*
* @author Christian Dupuis
*/
public class RedisHealthIndicatorTests {

View File

@@ -36,12 +36,13 @@ import static org.mockito.Mockito.when;
/**
* Tests for {@link SimpleDataSourceHealthIndicator}.
*
*
* @author Dave Syer
*/
public class SimpleDataSourceHealthIndicatorTests {
private final SimpleDataSourceHealthIndicator indicator = new SimpleDataSourceHealthIndicator();
private DriverManagerDataSource dataSource;
@Before
@@ -63,7 +64,7 @@ public class SimpleDataSourceHealthIndicatorTests {
public void customQuery() {
this.indicator.setDataSource(this.dataSource);
new JdbcTemplate(this.dataSource)
.execute("CREATE TABLE FOO (id INTEGER IDENTITY PRIMARY KEY)");
.execute("CREATE TABLE FOO (id INTEGER IDENTITY PRIMARY KEY)");
this.indicator.setQuery("SELECT COUNT(*) from FOO");
Health health = this.indicator.health();
System.err.println(health);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals;
/**
* Tests for {@link VanillaHealthIndicator}.
*
*
* @author Phillip Webb
*/
public class VanillaHealthIndicatorTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -40,6 +40,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link RedisMultiMetricRepository}.
*
* @author Dave Syer
*/
@RunWith(Parameterized.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -25,6 +25,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link MultiMetricRichGaugeReader}.
*
* @author Dave Syer
*/
public class MultiMetricRichGaugeReaderTests {