api/etcdhttp: change /health type back to string for backwards compatibility

This commit is contained in:
Jordan Liggitt 2018-01-15 01:49:59 -05:00
parent 7a8c192c8f
commit f77e54eb13
No known key found for this signature in database
GPG Key ID: 39928704103C7229
7 changed files with 20 additions and 17 deletions

View File

@ -74,7 +74,7 @@ Set `embed.Config.Debug` field to `true` to enable gRPC server logs.
#### Change in `/health` endpoint response value #### Change in `/health` endpoint response value
Previously, `[endpoint]:[client-port]/health` returned manually marshaled JSON value. 3.3 instead defines [`etcdhttp.Health`](https://godoc.org/github.com/coreos/etcd/etcdserver/api/etcdhttp#Health) struct and returns properly encoded JSON value with errors, if any. Previously, `[endpoint]:[client-port]/health` returned manually marshaled JSON value. 3.3 now defines [`etcdhttp.Health`](https://godoc.org/github.com/coreos/etcd/etcdserver/api/etcdhttp#Health) struct and includes errors, if any.
Before Before
@ -87,10 +87,10 @@ After
```bash ```bash
$ curl http://localhost:2379/health $ curl http://localhost:2379/health
{"health":true} {"health":"true"}
# Or # Or
{"health":false,"errors":["NOSPACE"]} {"health":"false","errors":["NOSPACE"]}
``` ```
#### Change in gRPC gateway HTTP endpoints (replaced `/v3alpha` with `/v3beta`) #### Change in gRPC gateway HTTP endpoints (replaced `/v3alpha` with `/v3beta`)

View File

@ -45,12 +45,12 @@ It is important to monitor your production etcd cluster for healthy information
#### Health Monitoring #### 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. 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 $ 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. 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.

View File

@ -29,5 +29,5 @@ curl http://10.0.0.10:2379/health
``` ```
```json ```json
{"health":true} {"health":"true"}
``` ```

View File

@ -53,7 +53,7 @@ func alarmTest(cx ctlCtx) {
} }
// '/health' handler should return 'false' // '/health' handler should return 'false'
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health":false,"errors":["NOSPACE"]}`}); 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) cx.t.Fatalf("failed get with curl (%v)", err)
} }

View File

@ -45,7 +45,7 @@ func metricsTest(cx ctlCtx) {
if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: fmt.Sprintf(`etcd_server_version{server_version="%s"} 1`, version.Version), metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil { if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: fmt.Sprintf(`etcd_server_version{server_version="%s"} 1`, version.Version), metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err) 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) cx.t.Fatalf("failed get with curl (%v)", err)
} }
} }

View File

@ -58,7 +58,7 @@ func NewHealthHandler(hfunc func() Health) http.HandlerFunc {
} }
h := hfunc() h := hfunc()
d, _ := json.Marshal(h) d, _ := json.Marshal(h)
if !h.Health { if h.Health != "true" {
http.Error(w, string(d), http.StatusServiceUnavailable) http.Error(w, string(d), http.StatusServiceUnavailable)
return return
} }
@ -70,12 +70,12 @@ func NewHealthHandler(hfunc func() Health) http.HandlerFunc {
// Health defines etcd server health status. // Health defines etcd server health status.
// TODO: remove manual parsing in etcdctl cluster-health // TODO: remove manual parsing in etcdctl cluster-health
type Health struct { type Health struct {
Health bool `json:"health"` Health string `json:"health"`
Errors []string `json:"errors,omitempty"` Errors []string `json:"errors,omitempty"`
} }
func checkHealth(srv etcdserver.ServerV2) Health { func checkHealth(srv etcdserver.ServerV2) Health {
h := Health{Health: false} h := Health{Health: "false"}
as := srv.Alarms() as := srv.Alarms()
if len(as) > 0 { if len(as) > 0 {
@ -96,7 +96,8 @@ func checkHealth(srv etcdserver.ServerV2) Health {
if err != nil { if err != nil {
h.Errors = append(h.Errors, err.Error()) h.Errors = append(h.Errors, err.Error())
} }
if err == nil {
h.Health = err == nil h.Health = "true"
}
return h return h
} }

View File

@ -30,12 +30,14 @@ func HandleHealth(mux *http.ServeMux, c *clientv3.Client) {
} }
func checkHealth(c *clientv3.Client) etcdhttp.Health { func checkHealth(c *clientv3.Client) etcdhttp.Health {
h := etcdhttp.Health{Health: false} h := etcdhttp.Health{Health: "false"}
ctx, cancel := context.WithTimeout(c.Ctx(), time.Second) ctx, cancel := context.WithTimeout(c.Ctx(), time.Second)
_, err := c.Get(ctx, "a") _, err := c.Get(ctx, "a")
cancel() cancel()
h.Health = err == nil || err == rpctypes.ErrPermissionDenied if err == nil || err == rpctypes.ErrPermissionDenied {
if !h.Health { h.Health = "true"
}
if h.Health != "true" {
h.Errors = append(h.Errors, err.Error()) h.Errors = append(h.Errors, err.Error())
} }
return h return h