refactor: use new health checker

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2021-02-11 01:03:25 +01:00
parent d77147f7cf
commit 316da70545
No known key found for this signature in database
GPG Key ID: 34CC109EB5ED1C2A
4 changed files with 29 additions and 11 deletions

View File

@ -275,6 +275,8 @@ func runServe(options serveOptions) error {
// explicitly convert to UTC.
now := func() time.Time { return time.Now().UTC() }
healthChecker := gosundheit.New()
serverConfig := server.Config{
SupportedResponseTypes: c.OAuth2.ResponseTypes,
SkipApprovalScreen: c.OAuth2.SkipApprovalScreen,
@ -287,6 +289,7 @@ func runServe(options serveOptions) error {
Logger: logger,
Now: now,
PrometheusRegistry: prometheusRegistry,
HealthChecker: healthChecker,
}
if c.Expiry.SigningKeys != "" {
signingKeys, err := time.ParseDuration(c.Expiry.SigningKeys)
@ -329,7 +332,6 @@ func runServe(options serveOptions) error {
telemetryRouter.Handle("/metrics", promhttp.HandlerFor(prometheusRegistry, promhttp.HandlerOpts{}))
// Configure health checker
healthChecker := gosundheit.New()
{
handler := gosundheithttp.HandleHealthJSON(healthChecker)
telemetryRouter.Handle("/healthz", handler)

View File

@ -10,6 +10,8 @@ import (
"testing"
"time"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/AppsFlyer/go-sundheit/checks"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
@ -33,20 +35,23 @@ func TestHandleHealth(t *testing.T) {
}
}
type badStorage struct {
storage.Storage
}
func (b *badStorage) CreateAuthRequest(r storage.AuthRequest) error {
return errors.New("storage unavailable")
}
func TestHandleHealthFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
httpServer, server := newTestServer(ctx, t, func(c *Config) {
c.Storage = &badStorage{c.Storage}
c.HealthChecker = gosundheit.New()
c.HealthChecker.RegisterCheck(&gosundheit.Config{
Check: &checks.CustomCheck{
CheckName: "fail",
CheckFunc: func() (details interface{}, err error) {
return nil, errors.New("error")
},
},
InitiallyPassing: false,
ExecutionPeriod: 1 * time.Second,
})
})
defer httpServer.Close()

View File

@ -15,6 +15,7 @@ import (
"sync/atomic"
"time"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/felixge/httpsnoop"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
@ -93,6 +94,8 @@ type Config struct {
Logger log.Logger
PrometheusRegistry *prometheus.Registry
HealthChecker gosundheit.Health
}
// WebConfig holds the server's frontend templates and asset configuration.
@ -333,7 +336,13 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
// "authproxy" connector.
handleFunc("/callback/{connector}", s.handleConnectorCallback)
handleFunc("/approval", s.handleApproval)
handle("/healthz", s.newHealthChecker(ctx))
handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !c.HealthChecker.IsHealthy() {
s.renderError(r, w, http.StatusInternalServerError, "Health check failed.")
return
}
fmt.Fprintf(w, "Health check passed")
}))
handlePrefix("/static", static)
handlePrefix("/theme", theme)
s.mux = r

View File

@ -21,6 +21,7 @@ import (
"testing"
"time"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/kylelemons/godebug/pretty"
"github.com/prometheus/client_golang/prometheus"
@ -96,6 +97,7 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi
},
Logger: logger,
PrometheusRegistry: prometheus.NewRegistry(),
HealthChecker: gosundheit.New(),
}
if updateConfig != nil {
updateConfig(&config)