-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mundipagg/feature/BPROC-97-healthcheck
[BPROC-97] - Criação de biblioteca de healthcheck em Golang
- Loading branch information
Showing
25 changed files
with
1,168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
boleto-api | ||
.pid | ||
.exe | ||
debug | ||
|
||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# IDE/Editor files | ||
*.sublime-workspace | ||
*.sublime-project | ||
.idea/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,97 @@ | ||
# gohealthcheck | ||
Lib de healthcheck em Golang | ||
|
||
[](https://godoc.org/github.com/mundipagg/goseq) | ||
# Golang health check | ||
|
||
Bliblioteca de health check em Golang. | ||
|
||
### Lista de serviços | ||
1. MongoDB; | ||
2. RabbitMQ. | ||
|
||
## Instalação | ||
|
||
### Usando *go get* | ||
|
||
$ go get github.com/mundipagg/healthcheck-go | ||
|
||
### Usando govendor | ||
$ govendor add github.com/mundipagg/healthcheck-go | ||
$ govendor add github.com/mundipagg/healthcheck-go/checks | ||
$ govendor add github.com/mundipagg/healthcheck-go/checks/mongo | ||
$ govendor add github.com/mundipagg/healthcheck-go/checks/rabbit | ||
|
||
## Exemplo | ||
|
||
### HealthCheck | ||
```go | ||
package healthcheck | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/mundipagg/boleto-api/config" | ||
|
||
HealthCheckLib "github.com/mundipagg/healthcheck-go" | ||
"github.com/mundipagg/healthcheck-go/checks/mongo" | ||
"github.com/mundipagg/healthcheck-go/checks/rabbit" | ||
) | ||
|
||
func createHealthCheck() HealthCheckLib.HealthCheck { | ||
mongoConfig := &mongo.Config{ | ||
Url: config.Get().MongoURL, | ||
User: config.Get().MongoUser, | ||
Password: config.Get().MongoPassword, | ||
Database: config.Get().MongoDatabase, | ||
AuthSource: config.Get().MongoAuthSource, | ||
Timeout: 3, | ||
ForceTLS: config.Get().ForceTLS, | ||
MaxPoolSize: 100, | ||
} | ||
|
||
rabbitConfig := &rabbit.Config{ | ||
ConnectionString: config.Get().ConnQueue, | ||
} | ||
|
||
healthCheck := HealthCheckLib.New() | ||
healthCheck.AddService(mongoConfig) | ||
healthCheck.AddService(rabbitConfig) | ||
|
||
return healthCheck | ||
} | ||
|
||
func Endpoint(c *gin.Context) { | ||
healthcheck := createHealthCheck() | ||
c.JSON(200, healthcheck.Execute()) | ||
} | ||
``` | ||
|
||
### API | ||
|
||
```go | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/mundipagg/boleto-api/healthcheck" | ||
) | ||
|
||
func Base(router *gin.Engine) { | ||
router.GET("/healthcheck", healthcheck.Endpoint) | ||
} | ||
``` | ||
|
||
### Response do endpoint | ||
```json | ||
{ | ||
"status": "Healthy", | ||
"results": { | ||
"mongo": { | ||
"status": "Healthy", | ||
"description": "mongo is healthy" | ||
}, | ||
"rabbit": { | ||
"status": "Healthy", | ||
"description": "rabbit is healthy" | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package checks | ||
|
||
const ( | ||
Unhealthy string = "Unhealthy" | ||
Healthy string = "Healthy" | ||
) | ||
|
||
type Check interface { | ||
Execute() CheckResult | ||
GetName() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package checks | ||
|
||
import "fmt" | ||
|
||
type CheckResult struct { | ||
Status string `json:"status"` | ||
Description string `json:"description"` | ||
} | ||
|
||
func NewCheckResult(name string, err error) CheckResult { | ||
if err == nil { | ||
return newHealthy(name) | ||
} | ||
|
||
return newUnhealthy(err) | ||
} | ||
|
||
func newHealthy(name string) CheckResult { | ||
return CheckResult{ | ||
Status: Healthy, | ||
Description: fmt.Sprintf("%s is healthy", name), | ||
} | ||
} | ||
|
||
func newUnhealthy(err error) CheckResult { | ||
return CheckResult{ | ||
Status: Unhealthy, | ||
Description: fmt.Sprintf("ERROR: %s", err), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package checks | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_NewCheckResult_WhenCreateCheckResultWithNilError_ShouldBeHealthy(t *testing.T) { | ||
result := NewCheckResult("serviceName", nil) | ||
|
||
assert.NotNil(t, result) | ||
assert.Equal(t, result.Status, Healthy) | ||
assert.Equal(t, result.Description, "serviceName is healthy") | ||
} | ||
|
||
func Test_NewCheckResult_WhenCreateCheckResultWithError_ShouldBeUnhealthy(t *testing.T) { | ||
result := NewCheckResult("serviceName", errors.New("error")) | ||
|
||
assert.NotNil(t, result) | ||
assert.Equal(t, result.Status, Unhealthy) | ||
assert.Contains(t, result.Description, "ERROR:") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package checks | ||
|
||
type Config interface { | ||
CreateCheck() Check | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package checks | ||
|
||
type HealthCheckResult struct { | ||
Status string `json:"status"` | ||
Results map[string]CheckResult `json:"results"` | ||
} | ||
|
||
func NewHealthCheckResult() HealthCheckResult { | ||
return HealthCheckResult{ | ||
Results: make(map[string]CheckResult), | ||
Status: Healthy, | ||
} | ||
} | ||
|
||
func (healthCheckResult *HealthCheckResult) AddCheckResult(key string, checkResult CheckResult) { | ||
if checkResult.Status == Unhealthy { | ||
healthCheckResult.Status = Unhealthy | ||
} | ||
|
||
healthCheckResult.Results[key] = checkResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package checks | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_AddCheckResult_WhenCreateCheckResultWithNilError_ShouldBeHealthy(t *testing.T) { | ||
healthCheckResult := NewHealthCheckResult() | ||
checkResult := NewCheckResult("serviceName", nil) | ||
|
||
healthCheckResult.AddCheckResult("serviceName", checkResult) | ||
|
||
assert.NotNil(t, healthCheckResult) | ||
assert.NotNil(t, checkResult) | ||
assert.Equal(t, healthCheckResult.Status, Healthy) | ||
assert.Equal(t, len(healthCheckResult.Results), 1) | ||
|
||
assert.Equal(t, healthCheckResult.Results["serviceName"].Status, Healthy) | ||
assert.Equal(t, healthCheckResult.Results["serviceName"].Description, "serviceName is healthy") | ||
} | ||
|
||
func Test_AddCheckResult_WhenCreateCheckResultWithError_ShouldBeUnhealthy(t *testing.T) { | ||
healthCheckResult := NewHealthCheckResult() | ||
checkResult := NewCheckResult("serviceName", errors.New("error")) | ||
|
||
healthCheckResult.AddCheckResult("serviceName", checkResult) | ||
|
||
assert.NotNil(t, healthCheckResult) | ||
assert.NotNil(t, checkResult) | ||
assert.Equal(t, healthCheckResult.Status, Unhealthy) | ||
assert.Equal(t, len(healthCheckResult.Results), 1) | ||
|
||
assert.Equal(t, healthCheckResult.Results["serviceName"].Status, Unhealthy) | ||
assert.Contains(t, healthCheckResult.Results["serviceName"].Description, "ERROR:") | ||
} |
Oops, something went wrong.