/-/{healthy,ready}/ respond to HEAD (#11160)

Some frameworks issue HEAD requests to determine health.

This resolves prometheus/prometheus#11159

Signed-off-by: Nicolas Dumazet <nicdumz.commits@gmail.com>

Signed-off-by: Nicolas Dumazet <nicdumz.commits@gmail.com>
This commit is contained in:
Nicolas Dumazet 2022-08-16 21:06:26 +02:00 committed by GitHub
parent 0b03ef73cf
commit 9594fa4dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View file

@ -12,6 +12,7 @@ Prometheus provides a set of management APIs to facilitate automation and integr
``` ```
GET /-/healthy GET /-/healthy
HEAD /-/healthy
``` ```
This endpoint always returns 200 and should be used to check Prometheus health. 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 GET /-/ready
HEAD /-/ready
``` ```
This endpoint returns 200 when Prometheus is ready to serve traffic (i.e. respond to queries). This endpoint returns 200 when Prometheus is ready to serve traffic (i.e. respond to queries).

View file

@ -471,10 +471,16 @@ func New(logger log.Logger, o *Options) *Handler {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, o.AppName+" is Healthy.\n") 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) { router.Get("/-/ready", readyf(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, o.AppName+" is Ready.\n") 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 return h
} }

View file

@ -126,6 +126,11 @@ func TestReadyAndHealthy(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
cleanupTestResponse(t, resp) 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{ for _, u := range []string{
baseURL + "/-/ready", baseURL + "/-/ready",
} { } {
@ -133,6 +138,11 @@ func TestReadyAndHealthy(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
cleanupTestResponse(t, resp) 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("")) 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.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
cleanupTestResponse(t, resp) 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("")) resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader(""))