Skip to content

Commit

Permalink
etcdserver/api/etcdhttp: log server-side /health checks
Browse files Browse the repository at this point in the history
ref.
#11704

Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Mar 18, 2020
1 parent 746c167 commit 7a86aa3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions etcdserver/api/etcdhttp/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,29 @@ func init() {
func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !allowMethod(w, r, "GET") {
plog.Warningf("/health error (status code %d)", http.StatusMethodNotAllowed)
return
}

if uint64(server.Leader()) == raft.None {
http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
plog.Warningf("/health error; no leader (status code %d)", http.StatusServiceUnavailable)
healthFailed.Inc()
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if _, err := server.Do(ctx, etcdserverpb.Request{Method: "QGET"}); err != nil {
http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
plog.Warningf("/health error; QGET failed %v (status code %d)", err, http.StatusServiceUnavailable)
healthFailed.Inc()
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"health": "true"}`))
plog.Infof("/health OK (status code %d)", http.StatusOK)
healthSuccess.Inc()
}
}
Expand Down

0 comments on commit 7a86aa3

Please sign in to comment.