From f24de70161c371f770165c153b18689480f0809c Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 14 Oct 2019 19:24:20 +0200 Subject: [PATCH 1/2] Update push test to use status code 200 Signed-off-by: beorn7 --- prometheus/push/push_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/prometheus/push/push_test.go b/prometheus/push/push_test.go index 4992eb1..1fabe39 100644 --- a/prometheus/push/push_test.go +++ b/prometheus/push/push_test.go @@ -33,7 +33,8 @@ func TestPush(t *testing.T) { lastPath string ) - // Fake a Pushgateway that always responds with 202. + // Fake a Pushgateway that responds with 202 to DELETE and with 200 in + // all other cases. pgwOK := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { lastMethod = r.Method @@ -44,7 +45,11 @@ func TestPush(t *testing.T) { } lastPath = r.URL.EscapedPath() w.Header().Set("Content-Type", `text/plain; charset=utf-8`) - w.WriteHeader(http.StatusAccepted) + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusAccepted) + return + } + w.WriteHeader(http.StatusOK) }), ) defer pgwOK.Close() From bd362a9ced7d7e53cc05454237dcb356ab3f6de7 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 14 Oct 2019 19:27:09 +0200 Subject: [PATCH 2/2] Use HTTP status code constants rather than numerical literals Signed-off-by: beorn7 --- prometheus/push/push.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prometheus/push/push.go b/prometheus/push/push.go index d8bfd84..77ce9c8 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -220,7 +220,7 @@ func (p *Pusher) Delete() error { return err } defer resp.Body.Close() - if resp.StatusCode != 202 { + if resp.StatusCode != http.StatusAccepted { body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. return fmt.Errorf("unexpected status code %d while deleting %s: %s", resp.StatusCode, p.fullURL(), body) } @@ -267,7 +267,8 @@ func (p *Pusher) push(method string) error { return err } defer resp.Body.Close() - if resp.StatusCode != 200 && resp.StatusCode != 202 { + // Pushgateway 0.10+ responds with StatusOK, earlier versions with StatusAccepted. + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. return fmt.Errorf("unexpected status code %d while pushing to %s: %s", resp.StatusCode, p.fullURL(), body) }