Kealth is a health check library for external dependencies in Kotlin
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependency>
<groupId>io.github.marioalvial</groupId>
<artifactId>kealth-core</artifactId>
<version>${kealth-version}</version>
</dependency>
repositories {
mavenCentral()
}
dependencies {
implementation "io.github.marioalvial:kealth-core:$kealth_version"
}
- Create your component:
class HealthComponentA : HealthComponent() {
override val name = "component A"
override val criticalLevel = CriticalLevel.HIGH
override fun healthCheck(): HealthStatus {
val result = doHealthCheckCallToComponentAService()
return if(result) HealthStatus.HEALTHY else HealthStatus.UNHEALTHY
}
override fun handleFailure(throwable: Throwable) {
sendAThrowableAlert(throwable)
}
override fun handleUnhealthy(){
sendUnhealthyAlert()
}
}
- Instantiate
HealthAggregator
:
val aggregator = HealthAggregator(listOf(HealthComponentA()))
- Execute
aggregate()
:
val results: List<HealthComponentResult> = aggregator.aggregate()
If you prefer you can also use aggregateWithFilter()
. This method will only execute the health()
function of components that matched the given predicate:
val results: List<HealthComponentResult> = aggregator.aggregateWithFilter{ name, criticalLevel -> name == "Component A" && criticalLevel == "HIGH" }
handleFailure()
will be trigger only if healthCheck()
throws exception (This method has a default implementation that can be override anytime)
handleUnhealthy()
will be trigger only if healthCheck()
returns HealthStatus.UNHEALTHY (This method has a default implementation that can be override anytime)
You can share some context from thread to any scope that is running your coroutine. Just override context val.
private val threadLocal = ThreadLocal<String>().apply { set("Thread Local $name") }
override var componentContext: CoroutineContext = threadLocal.asContextElement()
When aggregator.aggregate()
is called it will execute health()
of each component in parallel and create a HealthComponentResult
.
Module | Description | Artifacts |
---|---|---|
kealth-jdbc | Health Component for JDBC | |
kealth-http | Health Component for HTTP Request |
./gradlew test
For latest updates see CHANGELOG.md file.
Please read CONTRIBUTING.md for more details, and the process for submitting pull requests to us.
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.