Skip to content

marioalvial/kealth

Repository files navigation

Kealth

Kealth is a health check library for external dependencies in Kotlin

CircleCI Known Vulnerabilities Maven Central Codacy Badge ktlint License

Installation

Maven

<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>

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation "io.github.marioalvial:kealth-core:$kealth_version"
}    

Getting Started

  1. 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()
    }        
}
  1. Instantiate HealthAggregator:
val aggregator = HealthAggregator(listOf(HealthComponentA()))
  1. 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" } 

Handle Failure

handleFailure() will be trigger only if healthCheck() throws exception (This method has a default implementation that can be override anytime)

Handle Unhealthy Status

handleUnhealthy() will be trigger only if healthCheck() returns HealthStatus.UNHEALTHY (This method has a default implementation that can be override anytime)

Share context

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()

How it works

When aggregator.aggregate() is called it will execute health() of each component in parallel and create a HealthComponentResult.

Modules

Module Description Artifacts
kealth-jdbc Health Component for JDBC jar javadoc sources
kealth-http Health Component for HTTP Request jar javadoc sources

Testing

./gradlew test

Built With

Changelog

For latest updates see CHANGELOG.md file.

Contributing

Please read CONTRIBUTING.md for more details, and the process for submitting pull requests to us.

Authors

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

About

Health check for external dependencies in Kotlin

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages