Polish
This commit is contained in:
@@ -30,6 +30,9 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Simple implementation of {@link HealthIndicator} that returns a status and also
|
||||
* attempts a simple database test.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object>> {
|
||||
@@ -53,8 +56,8 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
|
||||
|
||||
@Override
|
||||
public Map<String, Object> health() {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
map.put("status", "ok");
|
||||
LinkedHashMap<String, Object> health = new LinkedHashMap<String, Object>();
|
||||
health.put("status", "ok");
|
||||
String product = "unknown";
|
||||
if (this.dataSource != null) {
|
||||
try {
|
||||
@@ -65,25 +68,25 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
|
||||
return connection.getMetaData().getDatabaseProductName();
|
||||
}
|
||||
});
|
||||
map.put("database", product);
|
||||
health.put("database", product);
|
||||
}
|
||||
catch (DataAccessException ex) {
|
||||
map.put("status", "error");
|
||||
map.put("error", ex.getClass().getName() + ": " + ex.getMessage());
|
||||
health.put("status", "error");
|
||||
health.put("error", ex.getClass().getName() + ": " + ex.getMessage());
|
||||
}
|
||||
String query = detectQuery(product);
|
||||
if (StringUtils.hasText(query)) {
|
||||
try {
|
||||
map.put("hello",
|
||||
health.put("hello",
|
||||
this.jdbcTemplate.queryForObject(query, String.class));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
map.put("status", "error");
|
||||
map.put("error", ex.getClass().getName() + ": " + ex.getMessage());
|
||||
health.put("status", "error");
|
||||
health.put("error", ex.getClass().getName() + ": " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
return health;
|
||||
}
|
||||
|
||||
protected String detectQuery(String product) {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.metrics;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Immutable class that can be used to hold any arbitrary system measurement value (a
|
||||
@@ -33,7 +34,7 @@ public class Metric<T extends Number> {
|
||||
|
||||
private final T value;
|
||||
|
||||
private Date timestamp;
|
||||
private final Date timestamp;
|
||||
|
||||
/**
|
||||
* Create a new {@link Metric} instance for the current time.
|
||||
@@ -104,41 +105,29 @@ public class Metric<T extends Number> {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
|
||||
result = prime * result
|
||||
+ ((this.timestamp == null) ? 0 : this.timestamp.hashCode());
|
||||
result = prime * result + ((this.value == null) ? 0 : this.value.hashCode());
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.name);
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.timestamp);
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.value);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
if (this == obj) {
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Metric<?> other = (Metric<?>) obj;
|
||||
if (this.name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
}
|
||||
else if (!this.name.equals(other.name))
|
||||
if (obj == null) {
|
||||
return false;
|
||||
if (this.timestamp == null) {
|
||||
if (other.timestamp != null)
|
||||
return false;
|
||||
}
|
||||
else if (!this.timestamp.equals(other.timestamp))
|
||||
return false;
|
||||
if (this.value == null) {
|
||||
if (other.value != null)
|
||||
return false;
|
||||
if (obj instanceof Metric) {
|
||||
Metric<?> other = (Metric<?>) obj;
|
||||
boolean rtn = true;
|
||||
rtn &= ObjectUtils.nullSafeEquals(this.name, other.name);
|
||||
rtn &= ObjectUtils.nullSafeEquals(this.timestamp, other.timestamp);
|
||||
rtn &= ObjectUtils.nullSafeEquals(this.value, other.value);
|
||||
return rtn;
|
||||
}
|
||||
else if (!this.value.equals(other.value))
|
||||
return false;
|
||||
return true;
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,12 +22,15 @@ package org.springframework.boot.actuate.metrics.export;
|
||||
* across a cluster), so this is the marker interface for those operations. The trigger of
|
||||
* an export operation might be periodic or even driven, but it remains outside the scope
|
||||
* of this interface. You might for instance create an instance of an Exporter and trigger
|
||||
* it using a <code>@Scheduled</code> annotation in a Spring ApplicationContext.
|
||||
* it using a {@code @Scheduled} annotation in a Spring ApplicationContext.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public interface Exporter {
|
||||
|
||||
/**
|
||||
* Export metric data.
|
||||
*/
|
||||
void export();
|
||||
|
||||
}
|
||||
|
||||
@@ -23,11 +23,15 @@ import org.springframework.boot.actuate.metrics.reader.MetricReader;
|
||||
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
|
||||
|
||||
/**
|
||||
* {@link Exporter} that "exports" by copying metric data from a source
|
||||
* {@link MetricReader} to a destination {@link MetricWriter}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class MetricCopyExporter extends AbstractMetricExporter {
|
||||
|
||||
private final MetricReader reader;
|
||||
|
||||
private final MetricWriter writer;
|
||||
|
||||
public MetricCopyExporter(MetricReader reader, MetricWriter writer) {
|
||||
|
||||
@@ -31,7 +31,9 @@ import org.springframework.boot.actuate.metrics.writer.MetricWriter;
|
||||
public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||
|
||||
private final PrefixMetricReader reader;
|
||||
|
||||
private final MetricWriter writer;
|
||||
|
||||
private Set<String> groups = new HashSet<String>();
|
||||
|
||||
public PrefixMetricGroupExporter(PrefixMetricReader reader, MetricWriter writer) {
|
||||
@@ -53,13 +55,13 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterable<Metric<?>> next(String group) {
|
||||
return this.reader.findAll(group);
|
||||
protected Iterable<String> groups() {
|
||||
return this.groups;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterable<String> groups() {
|
||||
return this.groups;
|
||||
protected Iterable<Metric<?>> next(String group) {
|
||||
return this.reader.findAll(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -26,12 +26,16 @@ import org.springframework.boot.actuate.metrics.repository.InMemoryMetricReposit
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link PrefixMetricGroupExporter}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class PrefixMetricGroupExporterTests {
|
||||
|
||||
private InMemoryMetricRepository writer = new InMemoryMetricRepository();
|
||||
private InMemoryMetricRepository reader = new InMemoryMetricRepository();
|
||||
|
||||
private InMemoryMetricRepository writer = new InMemoryMetricRepository();
|
||||
|
||||
private PrefixMetricGroupExporter exporter = new PrefixMetricGroupExporter(
|
||||
this.reader, this.writer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user