chore(handler): add 2.0 compatible health endpoint from v1.8 (#17252)

pull/18410/head
Jakub Bednář 2020-04-06 07:36:26 +02:00 committed by Pavel Zavora
parent ab00f36a32
commit 61606289db
2 changed files with 46 additions and 0 deletions

View File

@ -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.")

View File

@ -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)