-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return 500 if not ok and 200 if ok. The response will contains a map of string/string for each checks. The value is 'OK' if the check passed, otherwise, is is the error.
- Loading branch information
Showing
18 changed files
with
248 additions
and
25 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
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
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,4 +1,4 @@ | ||
package ledgertesting | ||
package pgtesting | ||
|
||
import ( | ||
"context" | ||
|
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,40 +1,44 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/numary/ledger/pkg/health" | ||
"go.uber.org/fx" | ||
) | ||
|
||
const ( | ||
VersionKey = `name:"_apiVersion"` | ||
StorageDriverKey = `name:"_apiStorageDriver"` | ||
LedgerListerKey = `name:"_apiLedgerLister"` | ||
versionKey = `name:"_apiVersion"` | ||
storageDriverKey = `name:"_apiStorageDriver"` | ||
ledgerListerKey = `name:"_apiLedgerLister"` | ||
) | ||
|
||
func ProvideVersion(provider interface{}) fx.Option { | ||
return fx.Provide( | ||
fx.Annotate(provider, fx.ResultTags(VersionKey)), | ||
fx.Annotate(provider, fx.ResultTags(versionKey)), | ||
) | ||
} | ||
|
||
func ProvideStorageDriver(provider interface{}) fx.Option { | ||
return fx.Provide( | ||
fx.Annotate(provider, fx.ResultTags(StorageDriverKey)), | ||
fx.Annotate(provider, fx.ResultTags(storageDriverKey)), | ||
) | ||
} | ||
|
||
func ProvideLedgerLister(provider interface{}) fx.Option { | ||
return fx.Provide( | ||
fx.Annotate(provider, fx.ResultTags(LedgerListerKey)), | ||
fx.Annotate(provider, fx.ResultTags(ledgerListerKey)), | ||
) | ||
} | ||
|
||
var Module = fx.Options( | ||
fx.Provide( | ||
fx.Annotate(NewConfigController, fx.ParamTags(VersionKey, StorageDriverKey, LedgerListerKey)), | ||
fx.Annotate(NewConfigController, fx.ParamTags(versionKey, storageDriverKey, ledgerListerKey)), | ||
), | ||
fx.Provide(NewLedgerController), | ||
fx.Provide(NewScriptController), | ||
fx.Provide(NewAccountController), | ||
fx.Provide(NewTransactionController), | ||
fx.Provide(NewMappingController), | ||
fx.Provide( | ||
fx.Annotate(NewHealthController, fx.ParamTags(health.HealthCheckKey)), | ||
), | ||
) |
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,58 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/numary/ledger/pkg/health" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type HealthController struct { | ||
Checks []health.NamedCheck | ||
} | ||
|
||
func (ctrl *HealthController) Check(c *gin.Context) { | ||
w := sync.WaitGroup{} | ||
w.Add(len(ctrl.Checks)) | ||
type R struct { | ||
Check health.NamedCheck | ||
Err error | ||
} | ||
results := make(chan R, len(ctrl.Checks)) | ||
for _, ch := range ctrl.Checks { | ||
go func(ch health.NamedCheck) { | ||
defer w.Done() | ||
select { | ||
case <-c.Request.Context().Done(): | ||
return | ||
case results <- R{ | ||
Check: ch, | ||
Err: ch.Do(c.Request.Context()), | ||
}: | ||
} | ||
}(ch) | ||
} | ||
w.Wait() | ||
close(results) | ||
response := map[string]string{} | ||
hasError := false | ||
for r := range results { | ||
if r.Err != nil { | ||
hasError = true | ||
response[r.Check.Name()] = r.Err.Error() | ||
} else { | ||
response[r.Check.Name()] = "OK" | ||
} | ||
} | ||
status := http.StatusOK | ||
if hasError { | ||
status = http.StatusInternalServerError | ||
} | ||
c.JSON(status, response) | ||
} | ||
|
||
func NewHealthController(checks []health.NamedCheck) HealthController { | ||
return HealthController{ | ||
Checks: checks, | ||
} | ||
} |
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,96 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"github.com/numary/ledger/pkg/api" | ||
"github.com/numary/ledger/pkg/api/internal" | ||
"github.com/numary/ledger/pkg/health" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/fx" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHealthController(t *testing.T) { | ||
|
||
type testCase struct { | ||
name string | ||
healthChecksProvider []interface{} | ||
expectedStatus int | ||
expectedResult map[string]string | ||
} | ||
|
||
var tests = []testCase{ | ||
{ | ||
name: "all-ok", | ||
healthChecksProvider: []interface{}{ | ||
func() health.NamedCheck { | ||
return health.NewNamedCheck("test1", health.CheckFn(func(ctx context.Context) error { | ||
return nil | ||
})) | ||
}, | ||
func() health.NamedCheck { | ||
return health.NewNamedCheck("test2", health.CheckFn(func(ctx context.Context) error { | ||
return nil | ||
})) | ||
}, | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResult: map[string]string{ | ||
"test1": "OK", | ||
"test2": "OK", | ||
}, | ||
}, | ||
{ | ||
name: "one-failing", | ||
healthChecksProvider: []interface{}{ | ||
func() health.NamedCheck { | ||
return health.NewNamedCheck("test1", health.CheckFn(func(ctx context.Context) error { | ||
return nil | ||
})) | ||
}, | ||
func() health.NamedCheck { | ||
return health.NewNamedCheck("test2", health.CheckFn(func(ctx context.Context) error { | ||
return errors.New("failure") | ||
})) | ||
}, | ||
}, | ||
expectedStatus: http.StatusInternalServerError, | ||
expectedResult: map[string]string{ | ||
"test1": "OK", | ||
"test2": "failure", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
|
||
options := make([]fx.Option, 0) | ||
for _, p := range tc.healthChecksProvider { | ||
options = append(options, health.ProvideHealthCheck(p)) | ||
} | ||
|
||
internal.RunSubTest(t, tc.name, func(h *api.API) { | ||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest("GET", "/_health", nil) | ||
|
||
h.ServeHTTP(rec, req) | ||
|
||
if !assert.Equal(t, tc.expectedStatus, rec.Result().StatusCode) { | ||
return | ||
} | ||
ret := make(map[string]string) | ||
err := json.NewDecoder(rec.Result().Body).Decode(&ret) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
if !assert.Equal(t, tc.expectedResult, ret) { | ||
return | ||
} | ||
}, options...) | ||
} | ||
|
||
} |
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
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
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
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,33 @@ | ||
package health | ||
|
||
import "context" | ||
|
||
type Check interface { | ||
Do(ctx context.Context) error | ||
} | ||
type CheckFn func(ctx context.Context) error | ||
|
||
func (fn CheckFn) Do(ctx context.Context) error { | ||
return fn(ctx) | ||
} | ||
|
||
type NamedCheck interface { | ||
Check | ||
Name() string | ||
} | ||
|
||
type simpleNamedCheck struct { | ||
Check | ||
name string | ||
} | ||
|
||
func (c *simpleNamedCheck) Name() string { | ||
return c.name | ||
} | ||
|
||
func NewNamedCheck(name string, check Check) *simpleNamedCheck { | ||
return &simpleNamedCheck{ | ||
Check: check, | ||
name: name, | ||
} | ||
} |
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 health | ||
|
||
import "go.uber.org/fx" | ||
|
||
const HealthCheckKey = `group:"_healthCheck"` | ||
|
||
func ProvideHealthCheck(provider interface{}) fx.Option { | ||
return fx.Provide( | ||
fx.Annotate(provider, fx.ResultTags(HealthCheckKey)), | ||
) | ||
} |
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
Oops, something went wrong.