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 18, 2017
1 parent 58740d7 commit 8364464
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 48 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
Expand Down Expand Up @@ -43,8 +43,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.info.InfoContributor;
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
Expand Down Expand Up @@ -77,6 +78,7 @@
* @author Eddú Meléndez
* @author Meang Akira Tanaka
* @author Ben Hale
* @author Vedran Pavic
*/
@Configuration
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
Expand All @@ -85,7 +87,7 @@ public class EndpointAutoConfiguration {

private final HealthAggregator healthAggregator;

private final Map<String, HealthIndicator> healthIndicators;
private final HealthIndicatorRegistry healthIndicatorRegistry;

private final List<InfoContributor> infoContributors;

Expand All @@ -94,12 +96,12 @@ public class EndpointAutoConfiguration {
private final TraceRepository traceRepository;

public EndpointAutoConfiguration(ObjectProvider<HealthAggregator> healthAggregator,
ObjectProvider<Map<String, HealthIndicator>> healthIndicators,
ObjectProvider<HealthIndicatorRegistry> healthIndicatorRegistry,
ObjectProvider<List<InfoContributor>> infoContributors,
ObjectProvider<Collection<PublicMetrics>> publicMetrics,
ObjectProvider<TraceRepository> traceRepository) {
this.healthAggregator = healthAggregator.getIfAvailable();
this.healthIndicators = healthIndicators.getIfAvailable();
this.healthIndicatorRegistry = healthIndicatorRegistry.getIfAvailable();
this.infoContributors = infoContributors.getIfAvailable();
this.publicMetrics = publicMetrics.getIfAvailable();
this.traceRepository = traceRepository.getIfAvailable();
Expand All @@ -117,9 +119,9 @@ public HealthEndpoint healthEndpoint() {
return new HealthEndpoint(
this.healthAggregator == null ? new OrderedHealthAggregator()
: this.healthAggregator,
this.healthIndicators == null
? Collections.<String, HealthIndicator>emptyMap()
: this.healthIndicators);
this.healthIndicatorRegistry == null
? new DefaultHealthIndicatorRegistry()
: this.healthIndicatorRegistry);
}

@Bean
Expand Down
Expand Up @@ -34,10 +34,12 @@
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
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.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.LdapHealthIndicator;
import org.springframework.boot.actuate.health.MailHealthIndicator;
Expand Down Expand Up @@ -91,6 +93,7 @@
* @author Phillip Webb
* @author Tommy Ludwig
* @author Eddú Meléndez
* @author Vedran Pavic
* @since 1.1.0
*/
@Configuration
Expand Down Expand Up @@ -125,6 +128,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-2016 the original author or authors.
* 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.
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,14 @@
* @author Dave Syer
* @author Christian Dupuis
* @author Andy Wilkinson
* @author Vedran Pavic
*/
@ConfigurationProperties(prefix = "endpoints.health")
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 +47,16 @@ public class HealthEndpoint extends AbstractEndpoint<Health> {
/**
* Create a new {@link HealthEndpoint} 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,20 +77,9 @@ public void setTimeToLive(long ttl) {
*/
@Override
public Health invoke() {
return this.healthIndicator.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;
HealthIndicator indicator = new CompositeHealthIndicator(
this.healthAggregator, this.healthIndicatorRegistry.getAll());
return indicator.health();
}

}
@@ -0,0 +1,68 @@
/*
* 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.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
* @since 2.0.0
*/
public class DefaultHealthIndicatorRegistry implements HealthIndicatorRegistry {

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

@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<>(this.healthIndicators));
}
}

}
@@ -0,0 +1,66 @@
/*
* 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.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
* @since 2.0.0
*/
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-2016 the original author or authors.
* 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.
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.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
Expand All @@ -43,6 +45,7 @@
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Vedran Pavic
*/
public class HealthMvcEndpointAutoConfigurationTests {

Expand Down Expand Up @@ -96,6 +99,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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
Expand Down Expand Up @@ -28,7 +28,7 @@
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void registersCloudFoundryDiscoveryEndpoint() throws Exception {
public void registersCloudFoundryHealthEndpoint() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
HealthEndpoint delegate = new HealthEndpoint(new OrderedHealthAggregator(),
Collections.<String, HealthIndicator>emptyMap());
new DefaultHealthIndicatorRegistry());
CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(
Collections.singleton(new TestHealthMvcEndpoint(delegate)), null, null);
handlerMapping.setPrefix("/test");
Expand Down

0 comments on commit 8364464

Please sign in to comment.