Skip to content

Commit

Permalink
Merge pull request #1 from mundipagg/feature/BPROC-97-healthcheck
Browse files Browse the repository at this point in the history
[BPROC-97] - Criação de biblioteca de healthcheck em Golang
  • Loading branch information
wesleycosta authored Sep 6, 2021
2 parents 144bf6f + 68ae5fe commit e4fb4e8
Show file tree
Hide file tree
Showing 25 changed files with 1,168 additions and 2 deletions.
23 changes: 23 additions & 0 deletions .gitignore
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/
99 changes: 97 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,97 @@
# gohealthcheck
Lib de healthcheck em Golang

[![GoDoc](https://godoc.org/github.com/mundipagg/goseq?status.svg)](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"
}
}
}
```
11 changes: 11 additions & 0 deletions checks/check.go
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
}
30 changes: 30 additions & 0 deletions checks/checkResult.go
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),
}
}
24 changes: 24 additions & 0 deletions checks/checkResult_test.go
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:")
}
5 changes: 5 additions & 0 deletions checks/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checks

type Config interface {
CreateCheck() Check
}
21 changes: 21 additions & 0 deletions checks/healthCheckResult.go
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
}
38 changes: 38 additions & 0 deletions checks/healthCheckResult_test.go
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:")
}
Loading

0 comments on commit e4fb4e8

Please sign in to comment.