Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
fe1b9c95
Commit
fe1b9c95
authored
Jul 25, 2019
by
Dmytro Nosan
Committed by
Stephane Nicoll
Jul 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Log health check failure with Reactive health indicators
See gh-17635
parent
b79de1e7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
145 additions
and
0 deletions
+145
-0
CassandraReactiveHealthIndicator.java
...t/actuate/cassandra/CassandraReactiveHealthIndicator.java
+1
-0
CouchbaseReactiveHealthIndicator.java
...t/actuate/couchbase/CouchbaseReactiveHealthIndicator.java
+1
-0
AbstractReactiveHealthIndicator.java
.../boot/actuate/health/AbstractReactiveHealthIndicator.java
+49
-0
MongoReactiveHealthIndicator.java
...work/boot/actuate/mongo/MongoReactiveHealthIndicator.java
+1
-0
RedisReactiveHealthIndicator.java
...work/boot/actuate/redis/RedisReactiveHealthIndicator.java
+1
-0
AbstractReactiveHealthIndicatorTests.java
.../actuate/health/AbstractReactiveHealthIndicatorTests.java
+92
-0
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cassandra/CassandraReactiveHealthIndicator.java
View file @
fe1b9c95
...
@@ -40,6 +40,7 @@ public class CassandraReactiveHealthIndicator extends AbstractReactiveHealthIndi
...
@@ -40,6 +40,7 @@ public class CassandraReactiveHealthIndicator extends AbstractReactiveHealthIndi
* @param reactiveCassandraOperations the Cassandra operations
* @param reactiveCassandraOperations the Cassandra operations
*/
*/
public
CassandraReactiveHealthIndicator
(
ReactiveCassandraOperations
reactiveCassandraOperations
)
{
public
CassandraReactiveHealthIndicator
(
ReactiveCassandraOperations
reactiveCassandraOperations
)
{
super
(
"Cassandra health check failed"
);
Assert
.
notNull
(
reactiveCassandraOperations
,
"ReactiveCassandraOperations must not be null"
);
Assert
.
notNull
(
reactiveCassandraOperations
,
"ReactiveCassandraOperations must not be null"
);
this
.
reactiveCassandraOperations
=
reactiveCassandraOperations
;
this
.
reactiveCassandraOperations
=
reactiveCassandraOperations
;
}
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/couchbase/CouchbaseReactiveHealthIndicator.java
View file @
fe1b9c95
...
@@ -39,6 +39,7 @@ public class CouchbaseReactiveHealthIndicator extends AbstractReactiveHealthIndi
...
@@ -39,6 +39,7 @@ public class CouchbaseReactiveHealthIndicator extends AbstractReactiveHealthIndi
* @param cluster the Couchbase cluster
* @param cluster the Couchbase cluster
*/
*/
public
CouchbaseReactiveHealthIndicator
(
Cluster
cluster
)
{
public
CouchbaseReactiveHealthIndicator
(
Cluster
cluster
)
{
super
(
"Couchbase health check failed"
);
this
.
cluster
=
cluster
;
this
.
cluster
=
cluster
;
}
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractReactiveHealthIndicator.java
View file @
fe1b9c95
...
@@ -16,8 +16,15 @@
...
@@ -16,8 +16,15 @@
package
org
.
springframework
.
boot
.
actuate
.
health
;
package
org
.
springframework
.
boot
.
actuate
.
health
;
import
java.util.function.Function
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
/**
/**
* Base {@link ReactiveHealthIndicator} implementations that encapsulates creation of
* Base {@link ReactiveHealthIndicator} implementations that encapsulates creation of
* {@link Health} instance and error handling.
* {@link Health} instance and error handling.
...
@@ -28,6 +35,44 @@ import reactor.core.publisher.Mono;
...
@@ -28,6 +35,44 @@ import reactor.core.publisher.Mono;
*/
*/
public
abstract
class
AbstractReactiveHealthIndicator
implements
ReactiveHealthIndicator
{
public
abstract
class
AbstractReactiveHealthIndicator
implements
ReactiveHealthIndicator
{
private
static
final
String
NO_MESSAGE
=
null
;
private
static
final
String
DEFAULT_MESSAGE
=
"Health check failed"
;
private
final
Log
logger
=
LogFactory
.
getLog
(
getClass
());
private
final
Function
<
Throwable
,
String
>
healthCheckFailedMessage
;
/**
* Create a new {@link AbstractReactiveHealthIndicator} instance with a default
* {@code healthCheckFailedMessage}.
* @since 2.1.7
*/
protected
AbstractReactiveHealthIndicator
()
{
this
(
NO_MESSAGE
);
}
/**
* Create a new {@link AbstractReactiveHealthIndicator} instance with a specific
* message to log when the health check fails.
* @param healthCheckFailedMessage the message to log on health check failure
* @since 2.1.7
*/
protected
AbstractReactiveHealthIndicator
(
String
healthCheckFailedMessage
)
{
this
.
healthCheckFailedMessage
=
(
ex
)
->
healthCheckFailedMessage
;
}
/**
* Create a new {@link AbstractReactiveHealthIndicator} instance with a specific
* message to log when the health check fails.
* @param healthCheckFailedMessage the message to log on health check failure
* @since 2.1.7
*/
protected
AbstractReactiveHealthIndicator
(
Function
<
Throwable
,
String
>
healthCheckFailedMessage
)
{
Assert
.
notNull
(
healthCheckFailedMessage
,
"HealthCheckFailedMessage must not be null"
);
this
.
healthCheckFailedMessage
=
healthCheckFailedMessage
;
}
@Override
@Override
public
final
Mono
<
Health
>
health
()
{
public
final
Mono
<
Health
>
health
()
{
try
{
try
{
...
@@ -39,6 +84,10 @@ public abstract class AbstractReactiveHealthIndicator implements ReactiveHealthI
...
@@ -39,6 +84,10 @@ public abstract class AbstractReactiveHealthIndicator implements ReactiveHealthI
}
}
private
Mono
<
Health
>
handleFailure
(
Throwable
ex
)
{
private
Mono
<
Health
>
handleFailure
(
Throwable
ex
)
{
if
(
this
.
logger
.
isWarnEnabled
())
{
String
message
=
this
.
healthCheckFailedMessage
.
apply
(
ex
);
this
.
logger
.
warn
(
StringUtils
.
hasText
(
message
)
?
message
:
DEFAULT_MESSAGE
,
ex
);
}
return
Mono
.
just
(
new
Health
.
Builder
().
down
(
ex
).
build
());
return
Mono
.
just
(
new
Health
.
Builder
().
down
(
ex
).
build
());
}
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicator.java
View file @
fe1b9c95
...
@@ -36,6 +36,7 @@ public class MongoReactiveHealthIndicator extends AbstractReactiveHealthIndicato
...
@@ -36,6 +36,7 @@ public class MongoReactiveHealthIndicator extends AbstractReactiveHealthIndicato
private
final
ReactiveMongoTemplate
reactiveMongoTemplate
;
private
final
ReactiveMongoTemplate
reactiveMongoTemplate
;
public
MongoReactiveHealthIndicator
(
ReactiveMongoTemplate
reactiveMongoTemplate
)
{
public
MongoReactiveHealthIndicator
(
ReactiveMongoTemplate
reactiveMongoTemplate
)
{
super
(
"Mongo health check failed"
);
Assert
.
notNull
(
reactiveMongoTemplate
,
"ReactiveMongoTemplate must not be null"
);
Assert
.
notNull
(
reactiveMongoTemplate
,
"ReactiveMongoTemplate must not be null"
);
this
.
reactiveMongoTemplate
=
reactiveMongoTemplate
;
this
.
reactiveMongoTemplate
=
reactiveMongoTemplate
;
}
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicator.java
View file @
fe1b9c95
...
@@ -40,6 +40,7 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
...
@@ -40,6 +40,7 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
private
final
ReactiveRedisConnectionFactory
connectionFactory
;
private
final
ReactiveRedisConnectionFactory
connectionFactory
;
public
RedisReactiveHealthIndicator
(
ReactiveRedisConnectionFactory
connectionFactory
)
{
public
RedisReactiveHealthIndicator
(
ReactiveRedisConnectionFactory
connectionFactory
)
{
super
(
"Redis health check failed"
);
this
.
connectionFactory
=
connectionFactory
;
this
.
connectionFactory
=
connectionFactory
;
}
}
...
...
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractReactiveHealthIndicatorTests.java
0 → 100644
View file @
fe1b9c95
/*
* Copyright 2012-2019 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
*
* https://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
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
reactor.core.publisher.Mono
;
import
org.springframework.boot.actuate.health.Health.Builder
;
import
org.springframework.boot.test.system.CapturedOutput
;
import
org.springframework.boot.test.system.OutputCaptureExtension
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Tests for {@link AbstractReactiveHealthIndicator}.
*
* @author Dmytro Nosan
*/
@ExtendWith
(
OutputCaptureExtension
.
class
)
class
AbstractReactiveHealthIndicatorTests
{
@Test
void
doHealth
()
{
Health
health
=
new
SimpleReactiveHealthIndicator
().
health
().
block
();
assertThat
(
health
).
isEqualTo
(
Health
.
up
().
build
());
}
@Test
void
doHealthCustomErrorMessage
(
CapturedOutput
output
)
{
Health
health
=
new
ErrorMessageReactiveHealthIndicator
().
health
().
block
();
assertThat
(
health
).
isEqualTo
(
Health
.
down
(
new
UnsupportedOperationException
()).
build
());
assertThat
(
output
).
contains
(
"Unsupported Operation"
);
}
@Test
void
doHealthCustomErrorMessageFunction
(
CapturedOutput
output
)
{
Health
health
=
new
CustomErrorFunctionReactiveHealthIndicator
().
health
().
block
();
assertThat
(
health
).
isEqualTo
(
Health
.
down
(
new
RuntimeException
()).
build
());
assertThat
(
output
).
contains
(
"Runtime Exception"
);
}
private
static
final
class
SimpleReactiveHealthIndicator
extends
AbstractReactiveHealthIndicator
{
@Override
protected
Mono
<
Health
>
doHealthCheck
(
Builder
builder
)
{
return
Mono
.
just
(
builder
.
up
().
build
());
}
}
private
static
final
class
ErrorMessageReactiveHealthIndicator
extends
AbstractReactiveHealthIndicator
{
ErrorMessageReactiveHealthIndicator
()
{
super
(
"Unsupported Operation"
);
}
@Override
protected
Mono
<
Health
>
doHealthCheck
(
Builder
builder
)
{
return
Mono
.
error
(
new
UnsupportedOperationException
());
}
}
private
static
final
class
CustomErrorFunctionReactiveHealthIndicator
extends
AbstractReactiveHealthIndicator
{
CustomErrorFunctionReactiveHealthIndicator
()
{
super
((
ex
)
->
"Runtime Exception"
);
}
@Override
protected
Mono
<
Health
>
doHealthCheck
(
Builder
builder
)
{
throw
new
RuntimeException
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment