Skip to content

Commit

Permalink
Introduce HealthIndicatorRegistry
Browse files Browse the repository at this point in the history
This commit introduces HealthIndicatorRegistry which handles registration of HealthIndicator instances. Registering new HealthIndicator instances is now possible in runtime.
  • Loading branch information
vpavic committed Jan 19, 2016
1 parent 112b707 commit c166a8a
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 41 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
Expand All @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,8 +45,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.health.DefaultHealthIndicatorRegistry;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
import org.springframework.boot.actuate.trace.TraceRepository;
Expand Down Expand Up @@ -81,6 +81,7 @@
* @author Christian Dupuis
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Vedran Pavic
*/
@Configuration
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
Expand All @@ -94,7 +95,7 @@ public class EndpointAutoConfiguration {
private HealthAggregator healthAggregator = new OrderedHealthAggregator();

@Autowired(required = false)
private Map<String, HealthIndicator> healthIndicators = new HashMap<String, HealthIndicator>();
private HealthIndicatorRegistry healthIndicatorRegistry = new DefaultHealthIndicatorRegistry();

@Autowired(required = false)
private Collection<PublicMetrics> publicMetrics;
Expand All @@ -111,7 +112,7 @@ public EnvironmentEndpoint environmentEndpoint() {
@Bean
@ConditionalOnMissingBean
public HealthEndpoint healthEndpoint() {
return new HealthEndpoint(this.healthAggregator, this.healthIndicators);
return new HealthEndpoint(this.healthAggregator, this.healthIndicatorRegistry);
}

@Bean
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
Expand Down Expand Up @@ -33,12 +33,14 @@
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicator;
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicatorProperties;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.JmsHealthIndicator;
import org.springframework.boot.actuate.health.MailHealthIndicator;
import org.springframework.boot.actuate.health.MongoHealthIndicator;
Expand Down Expand Up @@ -84,6 +86,7 @@
* @author Stephane Nicoll
* @author Phillip Webb
* @author Tommy Ludwig
* @author Vedran Pavic
* @since 1.1.0
*/
@Configuration
Expand All @@ -110,6 +113,19 @@ public OrderedHealthAggregator healthAggregator() {
return healthAggregator;
}

@Bean
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
public HealthIndicatorRegistry healthIndicatorRegistry(
Map<String, HealthIndicator> healthIndicators) {
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
String name = entry.getKey();
int index = name.toLowerCase().indexOf("healthindicator");
registry.register(index > 0 ? name.substring(0, index) : name, entry.getValue());
}
return registry;
}

@Bean
@ConditionalOnMissingBean(HealthIndicator.class)
public ApplicationHealthIndicator applicationHealthIndicator() {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
Expand All @@ -16,12 +16,11 @@

package org.springframework.boot.actuate.endpoint;

import java.util.Map;

import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;

Expand All @@ -31,11 +30,13 @@
* @author Dave Syer
* @author Christian Dupuis
* @author Andy Wilkinson
* @author Vedran Pavic
*/
@ConfigurationProperties(prefix = "endpoints.health", ignoreUnknownFields = true)
public class HealthEndpoint extends AbstractEndpoint<Health> {

private final HealthIndicator healthIndicator;
private final HealthAggregator healthAggregator;
private final HealthIndicatorRegistry healthIndicatorRegistry;

/**
* Time to live for cached result, in milliseconds.
Expand All @@ -45,19 +46,16 @@ public class HealthEndpoint extends AbstractEndpoint<Health> {
/**
* Create a new {@link HealthIndicator} instance.
* @param healthAggregator the health aggregator
* @param healthIndicators the health indicators
* @param healthIndicatorRegistry the health indicator registry
*/
public HealthEndpoint(HealthAggregator healthAggregator,
Map<String, HealthIndicator> healthIndicators) {
HealthIndicatorRegistry healthIndicatorRegistry) {
super("health", false);
Assert.notNull(healthAggregator, "HealthAggregator must not be null");
Assert.notNull(healthIndicators, "HealthIndicators must not be null");
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(
healthAggregator);
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
}
this.healthIndicator = healthIndicator;
Assert.notNull(healthIndicatorRegistry,
"healthIndicatorRegistry must not be null");
this.healthAggregator = healthAggregator;
this.healthIndicatorRegistry = healthIndicatorRegistry;
}

/**
Expand All @@ -78,19 +76,9 @@ public void setTimeToLive(long ttl) {
*/
@Override
public Health invoke() {
return this.healthIndicator.health();
HealthIndicator indicator = new CompositeHealthIndicator(
this.healthAggregator, this.healthIndicatorRegistry.getAll());
return indicator.health();
}

/**
* Turns the bean name into a key that can be used in the map of health information.
* @param name the bean name
* @return the key
*/
private String getKey(String name) {
int index = name.toLowerCase().indexOf("healthindicator");
if (index > 0) {
return name.substring(0, index);
}
return name;
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 2012-2016 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.health;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.Assert;

/**
* Default implementation of {@link HealthIndicatorRegistry}.
*
* @author Vedran Pavic
*/
public class DefaultHealthIndicatorRegistry implements HealthIndicatorRegistry {

private final Map<String, HealthIndicator> healthIndicators =
new HashMap<String, HealthIndicator>();

@Override
public void register(String name, HealthIndicator healthIndicator) {
Assert.notNull(healthIndicator, "HealthIndicator must not be null");
synchronized (this.healthIndicators) {
if (this.healthIndicators.get(name) != null) {
throw new IllegalStateException(
"HealthIndicator with name '" + name + "' already registered");
}
this.healthIndicators.put(name, healthIndicator);
}
}

@Override
public HealthIndicator unregister(String name) {
synchronized (this.healthIndicators) {
return this.healthIndicators.remove(name);
}
}

@Override
public HealthIndicator get(String name) {
synchronized (this.healthIndicators) {
return this.healthIndicators.get(name);
}
}

@Override
public Map<String, HealthIndicator> getAll() {
synchronized (this.healthIndicators) {
return Collections.unmodifiableMap(
new HashMap<String, HealthIndicator>(this.healthIndicators));
}
}

}
@@ -0,0 +1,65 @@
/*
* Copyright 2012-2016 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.health;

import java.util.Map;

/**
* A registry of {@link HealthIndicator}s.
* <p>
* Implementations <strong>must</strong> be thread-safe.
*
* @author Andy Wilkinson
* @author Vedran Pavic
*/
public interface HealthIndicatorRegistry {

/**
* Registers the given {@code healthIndicator}, associating it with the given
* {@code name}.
* @param name the name of the indicator
* @param healthIndicator the indicator
* @throws IllegalStateException if an indicator with the given {@code name} is
* already registered.
*/
void register(String name, HealthIndicator healthIndicator);

/**
* Unregisters the {@code HealthIndicator} previously registered with the given
* {@code name}.
* @param name the name of the indicator
* @return the unregistered indicator, or {@code null} if no indicator was found in
* the registry for the given {@code name}.
*/
HealthIndicator unregister(String name);

/**
* Returns the health indicator registered with the given {@code name}.
* @param name the name of the indicator
* @return the health indicator, or {@code null} if no indicator was registered with
* the given {@code name}.
*/
HealthIndicator get(String name);

/**
* Returns a snapshot of the registered health indicators and their names. The
* contents of the map do not reflect subsequent changes to the registry.
* @return the snapshot of registered health indicators
*/
Map<String, HealthIndicator> getAll();

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
Expand All @@ -21,8 +21,10 @@

import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
Expand All @@ -42,6 +44,7 @@
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Vedran Pavic
*/
public class HealthMvcEndpointAutoConfigurationTests {

Expand Down Expand Up @@ -94,6 +97,13 @@ public TestHealthIndicator testHealthIndicator() {
return new TestHealthIndicator();
}

@Bean
public HealthIndicatorRegistry healthIndicatorRegistry() {
DefaultHealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
registry.register("test", testHealthIndicator());
return registry;
}

}

static class TestHealthIndicator extends AbstractHealthIndicator {
Expand Down

0 comments on commit c166a8a

Please sign in to comment.