diff --git a/pkg/util/healthcheck/definitions.go b/pkg/util/healthcheck/definitions.go index 930f32418..de98b003f 100644 --- a/pkg/util/healthcheck/definitions.go +++ b/pkg/util/healthcheck/definitions.go @@ -15,6 +15,7 @@ type healthChecker struct { StatusDetail string `json:"-"` Checks map[string]*statusCheck `json:"statusChecks"` registered bool + statusServer *Server `json:"-"` } // Status - the status of this healthcheck diff --git a/pkg/util/healthcheck/healthcheck_test.go b/pkg/util/healthcheck/healthcheck_test.go index ed781bab5..f6d3573a0 100644 --- a/pkg/util/healthcheck/healthcheck_test.go +++ b/pkg/util/healthcheck/healthcheck_test.go @@ -158,7 +158,7 @@ func TestHTTPRequests(t *testing.T) { // Register healthchecks RegisterHealthcheck("hc1", "hc1", hcFunc) RegisterHealthcheck("hc2", "hc2", hcFunc) - + RunChecks() var server *httptest.Server // http Client @@ -194,6 +194,7 @@ func TestHTTPRequests(t *testing.T) { // Marshall the body to the healthChecker struct var result healthChecker + RunChecks() resp := getRequest("/status", &result, false) // assert response values @@ -207,6 +208,7 @@ func TestHTTPRequests(t *testing.T) { hcValues["hc2"] = true // Execute another request + RunChecks() resp = getRequest("/status", &result, false) // assert response values @@ -238,6 +240,7 @@ func TestHTTPRequests(t *testing.T) { // Set hc1 to fail hcValues["hc1"] = false + RunChecks() resp = getRequest("/status/hc1", &checkRes, false) // assert response values @@ -246,6 +249,7 @@ func TestHTTPRequests(t *testing.T) { // Set hc1 to fail hcValues["hc1"] = true + RunChecks() resp = getRequest("/status/hc1", &checkRes, false) // assert response values diff --git a/pkg/util/healthcheck/healthchecker.go b/pkg/util/healthcheck/healthchecker.go index 6b9cc04fd..d29a222c2 100644 --- a/pkg/util/healthcheck/healthchecker.go +++ b/pkg/util/healthcheck/healthchecker.go @@ -69,8 +69,10 @@ func RegisterHealthcheck(name, endpoint string, check CheckStatus) (string, erro } globalHealthChecker.Checks[endpoint] = newChecker - - http.HandleFunc(fmt.Sprintf("/status/%s", endpoint), checkHandler) + statusServer := globalHealthChecker.statusServer + if statusServer != nil { + statusServer.registerHandler(fmt.Sprintf("/status/%s", endpoint), checkHandler) + } if util.IsNotTest() { newChecker.executeCheck() @@ -153,16 +155,24 @@ type Server struct { } func NewServer(httpprof bool) *Server { - return &Server{ + globalHealthChecker.statusServer = &Server{ router: http.NewServeMux(), httpprof: httpprof, } + return globalHealthChecker.statusServer +} + +func (s *Server) registerHandler(path string, handler func(http.ResponseWriter, *http.Request)) { + s.router.HandleFunc(path, handler) } // HandleRequests - starts the http server func (s *Server) HandleRequests() { if !globalHealthChecker.registered { - s.router.HandleFunc("/status", statusHandler) + s.registerHandler("/status", statusHandler) + for _, statusChecks := range globalHealthChecker.Checks { + s.registerHandler(fmt.Sprintf("/status/%s", statusChecks.Endpoint), checkHandler) + } globalHealthChecker.registered = true } @@ -241,9 +251,6 @@ func GetHealthcheckOutput(url string) (string, error) { } func statusHandler(w http.ResponseWriter, r *http.Request) { - // Run the checks to get the latest results - RunChecks() - w.Header().Set("Content-Type", "application/json; charset=utf-8") // Return the data @@ -281,8 +288,6 @@ func checkHandler(w http.ResponseWriter, r *http.Request) { return } - thisCheck.executeCheck() - w.Header().Set("Content-Type", "application/json; charset=utf-8") // If check failed change return code to 500 if thisCheck.Status.Result == FAIL {