Health
public class Health: HealthProtocol
The Health class provides a concrete implementation of the HealthProtocol protocol that
applications can instantiate and then register one or more health checks against. Once you
have your health checks in place you can ask your Health instance for its status.
Usage Example:
One common use case for this Swift package is to integrate it into a Kitura-based application. In the code sample below, the health of the application is exposed through the /health endpoint. Cloud environments (e.g. Cloud Foundry, Kubernetes, etc.) can then use the status information returned from the /health endpoint to monitor and manage the Swift application instance.
For further information and example code see our README and the Cloud Foundry documentation for using application health checks.
import Health
let health = Health()
health.addCheck(check: MyCheck1())
health.addCheck(check: myClosureCheck1)
// Define /health endpoint that leverages Health
router.get("/health") { request, response, next in
let status = health.status
if health.status.state == .UP {
try response.status(.OK).send(status).end()
} else {
try response.status(.serviceUnavailable).send(status).end()
}
}
-
Status instance variable.
Declaration
Swift
public var status: Status -
Number of health checks registered.
Usage Example:
let health = Health() let count = health.numberOfChecksDeclaration
Swift
public var numberOfChecks: Int -
Creates an instance of the
Healthclass.Usage Example:
In the example below the
statusExpirationTimedefaults to30000milliseconds, in this case 30 seconds must elapse before theHealthinstance computes its status again by querying each health check that has been registered.let health = Health()Declaration
Swift
public init(statusExpirationTime: Int = 30000)Parameters
statusExpirationTimeOptional. The time window in milliseconds that should elapse before the status cache for this
Healthinstance is considered to be expired and should be recomputed. The default value is30000. -
Registers a health check.
Usage Example:
let health = Health() // Add custom checks health.addCheck(check: MyCheck1())Declaration
Swift
public func addCheck(check: HealthCheck)Parameters
checkAn object that extends the
HealthCheckclass. -
Registers a health check.
Usage Example:
let health = Health() // Add custom checks health.addCheck(check: myClosureCheck1)Declaration
Swift
public func addCheck(check: @escaping HealthCheckClosure)Parameters
checkA closure that conforms to the
HealthCheckClosuretype alias. -
Forces an update to the status of this instance.
Declaration
Swift
public func forceUpdateStatus()
View on GitHub
Health Class Reference