Skip to content

Commit

Permalink
chore(handler): add 2.0 compatible health endpoint from v1.8 (#17252)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored and sranka committed Jun 9, 2020
1 parent ab00f36 commit 6160628
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func NewHandler(c Config) *Handler {
"status-head",
"HEAD", "/status", false, true, authWrapper(h.serveStatus),
},
Route{ // Ping
"ping",
"GET", "/health", false, true, authWrapper(h.serveHealth),
},
Route{
"prometheus-metrics",
"GET", "/metrics", false, true, authWrapper(promhttp.Handler().ServeHTTP),
Expand Down Expand Up @@ -1000,6 +1004,24 @@ func (h *Handler) servePing(w http.ResponseWriter, r *http.Request) {
}
}

// serveHealth maps v2 health endpoint to ping endpoint
func (h *Handler) serveHealth(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"name": "influxdb",
"message": "ready for queries and writes",
"status": "pass",
"checks": []string{},
"version": h.Version,
}
b, _ := json.Marshal(resp)
h.writeHeader(w, http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := w.Write(b); err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}
}

// serveStatus has been deprecated.
func (h *Handler) serveStatus(w http.ResponseWriter, r *http.Request) {
h.Logger.Info("WARNING: /status has been deprecated. Use /ping instead.")
Expand Down
24 changes: 24 additions & 0 deletions services/httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/monitor"
"github.com/influxdata/influxdb/monitor/diagnostics"
"github.com/influxdata/influxdb/pkg/testing/assert"
"github.com/influxdata/influxdb/prometheus/remote"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/services/httpd"
Expand Down Expand Up @@ -1476,6 +1477,29 @@ func TestHandler_Ping(t *testing.T) {
}
}

// Ensure the handler handles health requests correctly.
func TestHandler_Health(t *testing.T) {
h := NewHandler(false)
w := httptest.NewRecorder()
h.ServeHTTP(w, MustNewRequest("GET", "/health", nil))
if w.Code != http.StatusOK {
t.Fatalf("unexpected status: %d", w.Code)
}

var got map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}

assert.Equal(t, got["name"], "influxdb", "invalid name")
assert.Equal(t, got["message"], "ready for queries and writes", "invalid message")
assert.Equal(t, got["status"], "pass", "invalid status")
assert.Equal(t, got["version"], "0.0.0", "invalid version")
if _, present := got["checks"]; !present {
t.Fatal("missing checks")
}
}

// Ensure the handler returns the version correctly from the different endpoints.
func TestHandler_Version(t *testing.T) {
h := NewHandler(false)
Expand Down

0 comments on commit 6160628

Please sign in to comment.