https://github.com/zacscoding/gochecker
Simple health checker for Go application :)
https://github.com/zacscoding/gochecker
go health healthcheck
Last synced: about 1 month ago
JSON representation
Simple health checker for Go application :)
- Host: GitHub
- URL: https://github.com/zacscoding/gochecker
- Owner: zacscoding
- License: mit
- Created: 2020-12-30T09:09:20.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-12T16:17:54.000Z (over 3 years ago)
- Last Synced: 2024-06-20T06:32:32.801Z (over 1 year ago)
- Topics: go, health, healthcheck
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/zacscoding/gochecker) 
:heavy_check_mark: Go Health Checker
===
Simple health check library for ur Go application :)
# Installation
```shell
$ go get github.com/zacscoding/gochecker
```
# Usage
> Initialize new health checker
```go
package main
import (
"github.com/zacscoding/gochecker"
"time"
)
func main() {
// This health checker uses cache health result with given TTL
checker := gochecker.NewHealthChecker(gochecker.WithCacheTTL(time.Minute))
// This health checker checks health status with given time interval in a new goroutine
checker = gochecker.NewHealthChecker(gochecker.WithBackground(time.Minute))
}
```
> Add checkers and observers
```go
package main
import (
"github.com/zacscoding/gochecker"
"github.com/zacscoding/gochecker/database"
"time"
)
func main() {
// Add health check components
// add database component
checker.AddChecker("MyDatabase", database.NewMySQLIndicator(db))
// add remote service component
checker.AddChecker("RemoteService-1", gochecker.NewUrlIndicator("RemoteService-1", "GET", "http://anotherservice.com", nil, time.Second*10))
// add component with function
checker.AddCheckerFn("RemoteService-3", func(ctx context.Context) gochecker.ComponentStatus {
status := gochecker.NewComponentStatus()
_, err := http.Get("https://google.com")
if err != nil {
status.WithDown()
status.WithDetail("err", err.Error())
return *status
}
status.WithUp()
return *status
})
// Add observers
checker.AddObserver("BatchProcessor", batchProcessor)
checker.AddChecker("RemoteService-2", gochecker.NewUrlIndicator("RemoteService-2", "GET", "http://localhost:8890", nil, time.Second*10))
}
```
> Getting health check result
```go
status := checker.Health(context.Background())
```
```json
{
"status": "UP",
"components": {
"BatchProcessor": {
"details": {
"checkpoint": "100"
},
"status": "UP"
},
"LocalDatabase": {
"details": {
"database": "mysql",
"validationQuery": "SELECT 1",
"version": "5.6.1"
},
"status": "UP"
},
"RemoteService-1": {
"details": {
"status": 200
},
"status": "UP"
},
"RemoteService-2": {
"details": {
"err": "Get \"http://localhost:8990\": dial tcp [::1]:8990: connect: connection refused"
},
"status": "DOWN"
}
}
}
```