Go healhchecker is a library for building easy custom functions to use them as your health check in your microservice
This healthchecker library goes fine with kubernetes or openshift platform where you set the intervals of your health checkers calls.
Install it with
dep
dep ensure -add github.com/ervitis/gohealthchecker
go modules
go get -u github.com/ervitis/gohealthchecker
If you want to test it:
go test -v -race ./...
Let's take a look at the example:
func checkPort() gohealthchecker.Healthfunc {
return func() (code int, e error) {
conn, err := net.Dial("tcp", ":8185")
if err != nil {
return http.StatusInternalServerError, err
}
_ = conn.Close()
return http.StatusOK, nil
}
}
func main() {
health := gohealthchecker.NewHealthchecker(http.StatusOK, http.StatusInternalServerError)
health.Add(checkPort(), "checkPort")
panic(http.ListenAndServe(":8085", health.ActivateHealthCheck("health")))
}
You can instance a healthchecker
by using the NewHealthchecker
function passing the two types of HTTP responses you want when it goes well or not.
Create a function for example checkPort()
that will return a Healthfunc
type. Inside that function add the logic you want for the healthchecker.
After that, add it using the Add
method. You can pass a name if you want.
If you use the example it will return a KO message because it will check if the port 8185 is opened (or maybe yes, that depend of your opened ports) The message would be like this:
{
"info":[
{
"message":"dial tcp :8185: connect: connection refused",
"code":500,
"service":"checkPort"
}
],
"code":500,
"systemInformation":{
"processStatus":"S",
"processActive":true,
"pid":13521,
"startTime":"2020-12-07T11:23:18+01:00",
"memory":{
"total":16307060,
"free":6811484,
"available":9923808
},
"ipAddress":"192.168.1.2",
"runtimeVersion":"go1.15.4",
"canAcceptWork":true
}
}
Install the prerequisites using go modules
go mod install
Use the library in your microservice app.
You can see an example inside the folder examples
No library is needed for making additional tests. Use the testing
library from Golang
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- ervitis - Initial work - ervitis
See also the list of contributors who participated in this project.
This project is licensed under the Apache 2.0 - see the LICENSE.md file for details