Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/etcdhttp: serve error information in '/health', marshal health in JSON #8312

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Documentation/v2/admin_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ It is important to monitor your production etcd cluster for healthy information

#### Health Monitoring

At lowest level, etcd exposes health information via HTTP at `/health` in JSON format. If it returns `{"health": "true"}`, then the cluster is healthy. Please note the `/health` endpoint is still an experimental one as in etcd 2.2.
At lowest level, etcd exposes health information via HTTP at `/health` in JSON format. If it returns `{"health":true}`, then the cluster is healthy.

```
$ curl -L http://127.0.0.1:2379/health

{"health": "true"}
{"health":true}
```

You can also use etcdctl to check the cluster-wide health information. It will contact all the members of the cluster and collect the health information for you.
Expand Down
2 changes: 1 addition & 1 deletion Documentation/v2/other_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ curl http://10.0.0.10:2379/health
```

```json
{"health": "true"}
{"health":true}
```
2 changes: 1 addition & 1 deletion e2e/ctl_v3_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func alarmTest(cx ctlCtx) {
}

// '/health' handler should return 'false'
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health": "false"}`}); err != nil {
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health":false,"errors":["NOSPACE"]}`}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func metricsTest(cx ctlCtx) {
if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: `etcd_debugging_mvcc_keys_total 1`, metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health": "true"}`, metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health":true}`, metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
}
20 changes: 15 additions & 5 deletions etcdserver/api/etcdhttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package etcdhttp

import (
"context"
"fmt"
"encoding/json"
"net/http"
"time"

Expand Down Expand Up @@ -57,7 +57,7 @@ func newHealthHandler(srv *etcdserver.EtcdServer) http.HandlerFunc {
return
}
h := checkHealth(srv)
d := []byte(fmt.Sprintf(`{"health": "%v"}`, h.Health))
d, _ := json.Marshal(h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a TODO about removing {"health" : "true"} from etcdctl cluster-health in the future; I don't think it can be done in the same minor rev without breaking compat

if !h.Health {
http.Error(w, string(d), http.StatusServiceUnavailable)
return
Expand All @@ -67,24 +67,34 @@ func newHealthHandler(srv *etcdserver.EtcdServer) http.HandlerFunc {
}
}

// TODO: remove manual parsing in etcdctl cluster-health
type health struct {
Health bool `json:"health"`
Health bool `json:"health"`
Errors []string `json:"errors,omitempty"`
}

func checkHealth(srv *etcdserver.EtcdServer) health {
h := health{Health: false}
if len(srv.Alarms()) > 0 {
// TODO: provide alarm lists

as := srv.Alarms()
if len(as) > 0 {
for _, v := range as {
h.Errors = append(h.Errors, v.Alarm.String())
}
return h
}

if uint64(srv.Leader()) == raft.None {
h.Errors = append(h.Errors, etcdserver.ErrNoLeader.Error())
return h
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
cancel()
if err != nil {
h.Errors = append(h.Errors, err.Error())
}

h.Health = err == nil
return h
Expand Down