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.numberOfChecks
Declaration
Swift
public var numberOfChecks: Int
-
Creates an instance of the
Health
class.Usage Example:
In the example below the
statusExpirationTime
defaults to30000
milliseconds, in this case 30 seconds must elapse before theHealth
instance computes its status again by querying each health check that has been registered.let health = Health()
Declaration
Swift
public init(statusExpirationTime: Int = 30000)
Parameters
statusExpirationTime
Optional. The time window in milliseconds that should elapse before the status cache for this
Health
instance 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
check
An object that extends the
HealthCheck
class. -
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
check
A closure that conforms to the
HealthCheckClosure
type alias. -
Forces an update to the status of this instance.
Declaration
Swift
public func forceUpdateStatus()