golang-health-checker
A simple package to allow you to track your application healthy providing two ways of checking:
Simple: will return a "fully functional" string and with this, you can check if your application is online and responding without any integration check
Detailed: will return a detailed status for any integration configuration informed on the integrations, just like in the examples below
How to install
If you are just starting a Go projetct you must start a go.mod file like below
go mod init github.com/my/repo
Or else, you already has a started project, just run the command below
go get github.com/gritzkoo/golang-health-checker
How to use
In this example, we will use the Echo web server to show how to import and use Simple and Detailed calls.
If you want check the full options in configurations, look this IntegrationConfig struct
Available integrations
- Redis
- Memcached
- Web integration (https)
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gritzkoo/golang-health-checker/pkg/healthcheck"
)
func main() {
// all the content below is just an example
// Gin instance
e := gin.Default()
// example of simple call
e.GET("/health-check/liveness", func(c *gin.Context) {
c.JSON(http.StatusOK, healthcheck.HealthCheckerSimple())
})
// example of detailed call
e.GET("/health-check/readiness", func(c *gin.Context) {
// define all integrations of your application with type healthcheck.ApplicationConfig
myApplicationConfig := healthcheck.ApplicationConfig{ // check the full list of available props in structs.go
Name: "You APP Name", // optional prop
Version: "V1.0.0", // optional prop
Integrations: []healthcheck.IntegrationConfig{ // mandatory prop
{
Type: healthcheck.Redis, // this prop will determine the kind of check, the list of types available in structs.go
Name: "redis-user-db", // the name of you integration to display in response
Host: "redis", // you can pass host:port and omit Port attribute
Port: "6379",
DB: 0, // default value is 0
}, {
Type: healthcheck.Memcached, // this prop will determine the kind of check, the list of types available in structs.go
Name: "Memcached server", // the name of you integration to display in response
Host: "memcache", // you can pass host:port and omit Port attribute
Port: "11211",
}, {
Type: healthcheck.Web, // this prop will determine the kind of check, the list of types available in structs.go
Name: "Github Integration", // the name of you integration to display in response
Host: "https://github.com/status", // you can pass host:port and omit Port attribute
TimeOut: 5, // default value to web call is 10s
Headers: []healthcheck.HTTPHeader{ // to customize headers to perform a GET request
{
Key: "Accept",
Value: "application/json",
},
},
}, {
Type: "unknown", // this prop will determine the kind of check, the list of types available in structs.go
Name: "Github Integration", // the name of you integration to display in response
Host: "https://github.com/status", // you can pass host:port and omit Port attribute
TimeOut: 5, // default value to web call is 10s
Headers: []healthcheck.HTTPHeader{ // to customize headers to perform a GET request
{
Key: "Accept",
Value: "application/json",
},
},
},
},
}
c.JSON(http.StatusOK, healthcheck.HealthCheckerDetailed(myApplicationConfig))
})
// Start server
e.Run(":8888")
}
This simple call will return a JSON as below
{
"status": "fully functional"
}
And detailed call will return a JSON as below
{
"name": "You APP Name",
"status": false, # here is the main status of your application when one of the integrations fails.. false will return
"version": "V1.0.0",
"date": "2021-08-27 08:18:06.762044096 -0300 -03 m=+24.943851850",
"duration": 0.283596049,
"integrations": [
{
"name": "Github Integration",
"kind": "unknown",
"status": false,
"response_time": 0,
"url": "https://github.com/status",
"errors": "unsuported type of:unknown"
},
{
"name": "Memcached server",
"kind": "Memcached DB",
"status": true,
"response_time": 0.000419116,
"url": "localhost:11211"
},
{
"name": "redis-user-db",
"kind": "Redis DB",
"status": true,
"response_time": 0.000845594,
"url": "localhost:6379"
},
{
"name": "Github Integration",
"kind": "Web service API",
"status": true,
"response_time": 0.283513713,
"url": "https://github.com/status"
}
]
}
Kubernetes liveness and readiness probing
And then, you could call this endpoints manually to see your application health, but, if you are using modern kubernetes deployment, you can config your chart to check your application with the setup below:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: 'go' #your application image
args:
- /server
livenessProbe:
httpGet:
path: /health-check/liveness
port: 80
httpHeaders:
- name: Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3
- name: readiness
image: 'go' #your application image
args:
- /server
readinessProbe:
httpGet:
path: /health-check/readiness
port: 80
httpHeaders:
- name: Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3