diff --git a/services/httpd/handler.go b/services/httpd/handler.go index e37945a6a4..a96314e62d 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -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), @@ -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.") diff --git a/services/httpd/handler_test.go b/services/httpd/handler_test.go index 2c06e17bae..c483d74cec 100644 --- a/services/httpd/handler_test.go +++ b/services/httpd/handler_test.go @@ -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" @@ -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)