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

View file

@ -10,6 +10,8 @@ import (
"testing" "testing"
"time" "time"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/AppsFlyer/go-sundheit/checks"
"github.com/coreos/go-oidc/v3/oidc" "github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/stretchr/testify/require" "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) { func TestHandleHealthFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
httpServer, server := newTestServer(ctx, t, func(c *Config) { 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() defer httpServer.Close()

View file

@ -15,6 +15,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/felixge/httpsnoop" "github.com/felixge/httpsnoop"
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -93,6 +94,8 @@ type Config struct {
Logger log.Logger Logger log.Logger
PrometheusRegistry *prometheus.Registry PrometheusRegistry *prometheus.Registry
HealthChecker gosundheit.Health
} }
// WebConfig holds the server's frontend templates and asset configuration. // 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. // "authproxy" connector.
handleFunc("/callback/{connector}", s.handleConnectorCallback) handleFunc("/callback/{connector}", s.handleConnectorCallback)
handleFunc("/approval", s.handleApproval) 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("/static", static)
handlePrefix("/theme", theme) handlePrefix("/theme", theme)
s.mux = r s.mux = r

View file

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