Log a warning when a health indicator takes too long to run

Update `HealthEndpointSupport` so that it logs a warning if a health
indicator takes too long to respond.

Fixes gh-31231
This commit is contained in:
Phillip Webb
2022-06-14 09:30:13 -07:00
parent 2094722e5d
commit 9f8a262e6b
16 changed files with 192 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.health;
import java.time.Duration;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -34,13 +35,14 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
/**
* Base class for {@link HealthEndpointSupport} tests.
*
* @param <S> the support type
* @param <R> the registry type
* @param <C> the contributor type
* @param <T> the contributed health component type
* @author Phillip Webb
* @author Madhura Bhave
*/
abstract class HealthEndpointSupportTests<R extends ContributorRegistry<C>, C, T> {
abstract class HealthEndpointSupportTests<S extends HealthEndpointSupport<C, T>, R extends ContributorRegistry<C>, C, T> {
final R registry;
@@ -352,7 +354,11 @@ abstract class HealthEndpointSupportTests<R extends ContributorRegistry<C>, C, T
assertThat(result).isEqualTo(null);
}
protected abstract HealthEndpointSupport<C, T> create(R registry, HealthEndpointGroups groups);
protected final S create(R registry, HealthEndpointGroups groups) {
return create(registry, groups, null);
}
protected abstract S create(R registry, HealthEndpointGroups groups, Duration slowIndicatorLoggingThreshold);
protected abstract R createRegistry();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 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.
@@ -16,12 +16,16 @@
package org.springframework.boot.actuate.health;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.actuate.health.HealthEndpointSupport.HealthResult;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -32,8 +36,9 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb
* @author Scott Frederick
*/
class HealthEndpointTests
extends HealthEndpointSupportTests<HealthContributorRegistry, HealthContributor, HealthComponent> {
@ExtendWith(OutputCaptureExtension.class)
class HealthEndpointTests extends
HealthEndpointSupportTests<HealthEndpoint, HealthContributorRegistry, HealthContributor, HealthComponent> {
@Test
void healthReturnsSystemHealth() {
@@ -66,9 +71,27 @@ class HealthEndpointTests
assertThat(health).isEqualTo(this.up);
}
@Test
void healthWhenIndicatorIsSlow(CapturedOutput output) {
HealthIndicator indicator = () -> {
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
}
return this.up;
};
this.registry.registerContributor("test", indicator);
create(this.registry, this.groups, Duration.ofMillis(10)).health();
assertThat(output).contains("Health contributor");
assertThat(output).contains("to respond");
}
@Override
protected HealthEndpoint create(HealthContributorRegistry registry, HealthEndpointGroups groups) {
return new HealthEndpoint(registry, groups);
protected HealthEndpoint create(HealthContributorRegistry registry, HealthEndpointGroups groups,
Duration slowIndicatorLoggingThreshold) {
return new HealthEndpoint(registry, groups, slowIndicatorLoggingThreshold);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.health;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
@@ -36,8 +37,8 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb
* @author Scott Frederick
*/
class HealthEndpointWebExtensionTests
extends HealthEndpointSupportTests<HealthContributorRegistry, HealthContributor, HealthComponent> {
class HealthEndpointWebExtensionTests extends
HealthEndpointSupportTests<HealthEndpointWebExtension, HealthContributorRegistry, HealthContributor, HealthComponent> {
@Test
void healthReturnsSystemHealth() {
@@ -81,8 +82,9 @@ class HealthEndpointWebExtensionTests
}
@Override
protected HealthEndpointWebExtension create(HealthContributorRegistry registry, HealthEndpointGroups groups) {
return new HealthEndpointWebExtension(registry, groups);
protected HealthEndpointWebExtension create(HealthContributorRegistry registry, HealthEndpointGroups groups,
Duration slowIndicatorLoggingThreshold) {
return new HealthEndpointWebExtension(registry, groups, slowIndicatorLoggingThreshold);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -188,14 +188,14 @@ class HealthEndpointWebIntegrationTests {
@Bean
HealthEndpoint healthEndpoint(HealthContributorRegistry healthContributorRegistry,
HealthEndpointGroups healthEndpointGroups) {
return new HealthEndpoint(healthContributorRegistry, healthEndpointGroups);
return new HealthEndpoint(healthContributorRegistry, healthEndpointGroups, null);
}
@Bean
@ConditionalOnWebApplication(type = Type.SERVLET)
HealthEndpointWebExtension healthWebEndpointExtension(HealthContributorRegistry healthContributorRegistry,
HealthEndpointGroups healthEndpointGroups) {
return new HealthEndpointWebExtension(healthContributorRegistry, healthEndpointGroups);
return new HealthEndpointWebExtension(healthContributorRegistry, healthEndpointGroups, null);
}
@Bean
@@ -203,7 +203,8 @@ class HealthEndpointWebIntegrationTests {
ReactiveHealthEndpointWebExtension reactiveHealthWebEndpointExtension(
ReactiveHealthContributorRegistry reactiveHealthContributorRegistry,
HealthEndpointGroups healthEndpointGroups) {
return new ReactiveHealthEndpointWebExtension(reactiveHealthContributorRegistry, healthEndpointGroups);
return new ReactiveHealthEndpointWebExtension(reactiveHealthContributorRegistry, healthEndpointGroups,
null);
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.health;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
@@ -37,7 +38,7 @@ import static org.mockito.Mockito.mock;
* @author Scott Frederick
*/
class ReactiveHealthEndpointWebExtensionTests extends
HealthEndpointSupportTests<ReactiveHealthContributorRegistry, ReactiveHealthContributor, Mono<? extends HealthComponent>> {
HealthEndpointSupportTests<ReactiveHealthEndpointWebExtension, ReactiveHealthContributorRegistry, ReactiveHealthContributor, Mono<? extends HealthComponent>> {
@Test
void healthReturnsSystemHealth() {
@@ -82,8 +83,8 @@ class ReactiveHealthEndpointWebExtensionTests extends
@Override
protected ReactiveHealthEndpointWebExtension create(ReactiveHealthContributorRegistry registry,
HealthEndpointGroups groups) {
return new ReactiveHealthEndpointWebExtension(registry, groups);
HealthEndpointGroups groups, Duration slowIndicatorLoggingThreshold) {
return new ReactiveHealthEndpointWebExtension(registry, groups, slowIndicatorLoggingThreshold);
}
@Override