From 9594fa4dbdec5d89735be47d34ac3b7e5138ab34 Mon Sep 17 00:00:00 2001 From: Nicolas Dumazet Date: Tue, 16 Aug 2022 21:06:26 +0200 Subject: [PATCH] /-/{healthy,ready}/ respond to HEAD (#11160) Some frameworks issue HEAD requests to determine health. This resolves prometheus/prometheus#11159 Signed-off-by: Nicolas Dumazet Signed-off-by: Nicolas Dumazet --- docs/management_api.md | 2 ++ web/web.go | 6 ++++++ web/web_test.go | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/docs/management_api.md b/docs/management_api.md index 4bdfddcd0..0b496cbaa 100644 --- a/docs/management_api.md +++ b/docs/management_api.md @@ -12,6 +12,7 @@ Prometheus provides a set of management APIs to facilitate automation and integr ``` GET /-/healthy +HEAD /-/healthy ``` This endpoint always returns 200 and should be used to check Prometheus health. @@ -21,6 +22,7 @@ This endpoint always returns 200 and should be used to check Prometheus health. ``` GET /-/ready +HEAD /-/ready ``` This endpoint returns 200 when Prometheus is ready to serve traffic (i.e. respond to queries). diff --git a/web/web.go b/web/web.go index 43cfc96e7..27d7c9635 100644 --- a/web/web.go +++ b/web/web.go @@ -471,10 +471,16 @@ func New(logger log.Logger, o *Options) *Handler { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, o.AppName+" is Healthy.\n") }) + router.Head("/-/healthy", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) router.Get("/-/ready", readyf(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, o.AppName+" is Ready.\n") })) + router.Head("/-/ready", readyf(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) return h } diff --git a/web/web_test.go b/web/web_test.go index 65935079c..7da06a306 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -126,6 +126,11 @@ func TestReadyAndHealthy(t *testing.T) { require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) + resp, err = http.Head(baseURL + "/-/healthy") + require.NoError(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) + cleanupTestResponse(t, resp) + for _, u := range []string{ baseURL + "/-/ready", } { @@ -133,6 +138,11 @@ func TestReadyAndHealthy(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) + + resp, err = http.Head(u) + require.NoError(t, err) + require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) + cleanupTestResponse(t, resp) } resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) @@ -156,6 +166,11 @@ func TestReadyAndHealthy(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) + + resp, err = http.Head(u) + require.NoError(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) + cleanupTestResponse(t, resp) } resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader(""))