Skip to content

Commit

Permalink
Add HTTP healthcheck endpoint (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksanders authored Jan 28, 2021
1 parent 07b69e1 commit 811e1a6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ecs_credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func runEcsMetadata(cmd *cobra.Command, args []string) error {
listenAddr := fmt.Sprintf("%s:%d", ipaddress, ecsProviderListenPort)

router := mux.NewRouter()
router.HandleFunc("/healthcheck", handlers.HealthcheckHandler)
router.HandleFunc("/ecs/{role:.*}", handlers.MetaDataServiceMiddleware(handlers.ECSMetadataServiceCredentialsHandler))
router.HandleFunc("/{path:.*}", handlers.MetaDataServiceMiddleware(handlers.CustomHandler))

Expand Down
1 change: 1 addition & 0 deletions cmd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func runMetadata(cmd *cobra.Command, args []string) error {
listenAddr := fmt.Sprintf("%s:%d", ipaddress, metadataListenPort)

router := mux.NewRouter()
router.HandleFunc("/healthcheck", handlers.HealthcheckHandler)
router.HandleFunc("/{version}/", handlers.MetaDataServiceMiddleware(handlers.BaseVersionHandler))
router.HandleFunc("/{version}/api/token", handlers.MetaDataServiceMiddleware(handlers.TokenHandler)).Methods("PUT")
router.HandleFunc("/{version}/meta-data", handlers.MetaDataServiceMiddleware(handlers.BaseHandler))
Expand Down
34 changes: 34 additions & 0 deletions handlers/healthcheckHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/netflix/weep/health"
log "github.com/sirupsen/logrus"
)

type healthcheckResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}

func HealthcheckHandler(w http.ResponseWriter, r *http.Request) {
healthy, reason := health.WeepStatus.Get()
var status int
if healthy {
status = http.StatusOK
} else {
status = http.StatusInternalServerError
}
resp := healthcheckResponse{
Status: status,
Message: reason,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
err := json.NewEncoder(w).Encode(resp)
if err != nil {
log.Errorf("error writing healthcheck response: %v", err)
}
}
38 changes: 38 additions & 0 deletions health/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package health

import "sync"

var WeepStatus status

type status struct {
sync.RWMutex
healthy bool
reason string
}

func init() {
WeepStatus = status{
healthy: true,
reason: "healthy",
}
}

func (s *status) Get() (bool, string) {
s.RLock()
defer s.RUnlock()
return s.healthy, s.reason
}

func (s *status) SetUnhealthy(reason string) {
s.Lock()
defer s.Unlock()
s.healthy = false
s.reason = reason
}

func (s *status) SetHealthy() {
s.Lock()
defer s.Unlock()
s.healthy = true
s.reason = "healthy"
}

0 comments on commit 811e1a6

Please sign in to comment.