org.springframework.boot.actuate.health.HealthContributor Java Examples

The following examples show how to use org.springframework.boot.actuate.health.HealthContributor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiscoveryCompositeHealthContributor.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
private NamedContributor<HealthContributor> asNamedContributor(
		DiscoveryHealthIndicator indicator) {
	return new NamedContributor<HealthContributor>() {

		@Override
		public String getName() {
			return indicator.getName();
		}

		@Override
		public HealthIndicator getContributor() {
			return asHealthIndicator(indicator);
		}

	};
}
 
Example #2
Source File: DiscoveryCompositeHealthContributorTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void iteratorIteratesNamedContributors() throws Exception {
	TestDiscoveryHealthIndicator indicator1 = new TestDiscoveryHealthIndicator(
			"test1", Health.up().build());
	TestDiscoveryHealthIndicator indicator2 = new TestDiscoveryHealthIndicator(
			"test2", Health.down().build());
	DiscoveryCompositeHealthContributor composite = new DiscoveryCompositeHealthContributor(
			Arrays.asList(indicator1, indicator2));
	List<NamedContributor<HealthContributor>> contributors = new ArrayList<>();
	for (NamedContributor<HealthContributor> contributor : composite) {
		contributors.add(contributor);
	}
	assertThat(contributors).hasSize(2);
	assertThat(contributors).extracting("name").containsExactlyInAnyOrder("test1",
			"test2");
	assertThat(contributors).extracting("contributor").extracting("health")
			.containsExactlyInAnyOrder(indicator1.health(), indicator2.health());
}
 
Example #3
Source File: BindersHealthIndicatorAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
public void afterBinderContextInitialized(String binderConfigurationName,
		ConfigurableApplicationContext binderContext) {
	if (this.bindersHealthContributor != null) {
		this.bindersHealthContributor.add(binderConfigurationName,
				binderContext.getBeansOfType(HealthContributor.class));
	}
}
 
Example #4
Source File: BindersHealthIndicatorAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private HealthContributor getContributor(Map<String, HealthContributor> binderHealthContributors) {
	if (binderHealthContributors.isEmpty()) {
		return UNKNOWN;
	}
	if (binderHealthContributors.size() == 1) {
		return binderHealthContributors.values().iterator().next();
	}
	return CompositeHealthContributor.fromMap(binderHealthContributors);
}
 
Example #5
Source File: HealthIndicatorsConfigurationTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheck() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"--spring.jmx.enabled=false");
	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	CompositeHealthContributor bindersHealthContributor = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthContributor).isNotNull();
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();

	assertThat(bindersHealthContributor.stream().map(NamedContributor::getName)).contains("binder1", "binder2");
	assertThat(bindersHealthContributor.getContributor("binder1")).extracting("health").extracting("status")
			.isEqualTo(Status.UP);
	assertThat(bindersHealthContributor.getContributor("binder2")).extracting("health").extracting("status")
			.isEqualTo(Status.UNKNOWN);

	context.close();
}
 
Example #6
Source File: HealthIndicatorsConfigurationTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheckWhenDisabled() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"management.health.binders.enabled:false", "--spring.jmx.enabled=false");

	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	try {
		context.getBean("bindersHealthContributor", CompositeHealthContributor.class);
		fail("The 'bindersHealthContributor' bean should have not been defined");
	}
	catch (NoSuchBeanDefinitionException e) {
	}
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();
	context.close();
}
 
Example #7
Source File: VaultHealthIndicatorConfiguration.java    From spring-cloud-vault with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = { "vaultHealthIndicator" })
public HealthContributor vaultHealthIndicator() {
	return createContributor(this.vaultTemplates);
}
 
Example #8
Source File: BindersHealthIndicatorAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
void add(String binderConfigurationName, Map<String, HealthContributor> binderHealthContributors) {
	// if there are no health contributors in the child context, we just mark
	// the binder's health as unknown
	// this can happen due to the fact that configuration is inherited
	this.contributors.put(binderConfigurationName, getContributor(binderHealthContributors));
}
 
Example #9
Source File: BindersHealthIndicatorAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
public HealthContributor getContributor(String name) {
	return contributors.get(name);
}
 
Example #10
Source File: BindersHealthIndicatorAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
	return contributors.entrySet().stream()
			.map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
}
 
Example #11
Source File: DiscoveryCompositeHealthContributor.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public HealthContributor getContributor(String name) {
	return asHealthIndicator(this.indicators.get(name));
}
 
Example #12
Source File: DiscoveryCompositeHealthContributor.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
	return this.indicators.values().stream().map(this::asNamedContributor).iterator();
}
 
Example #13
Source File: DiscoveryClientHealthIndicatorTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
private Health getHealth(String name) {
	HealthContributor delegate = ((CompositeHealthContributor) this.healthContributor)
			.getContributor(name);
	return ((HealthIndicator) delegate).health();
}