Skip to content

Commit e0e84de

Browse files
authored
Merge pull request #654 from prometheus/beorn7/push
Minor improvements to the push code
2 parents 3ddc3cf + bd362a9 commit e0e84de

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

prometheus/push/push.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (p *Pusher) Delete() error {
220220
return err
221221
}
222222
defer resp.Body.Close()
223-
if resp.StatusCode != 202 {
223+
if resp.StatusCode != http.StatusAccepted {
224224
body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only.
225225
return fmt.Errorf("unexpected status code %d while deleting %s: %s", resp.StatusCode, p.fullURL(), body)
226226
}
@@ -267,7 +267,8 @@ func (p *Pusher) push(method string) error {
267267
return err
268268
}
269269
defer resp.Body.Close()
270-
if resp.StatusCode != 200 && resp.StatusCode != 202 {
270+
// Pushgateway 0.10+ responds with StatusOK, earlier versions with StatusAccepted.
271+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
271272
body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only.
272273
return fmt.Errorf("unexpected status code %d while pushing to %s: %s", resp.StatusCode, p.fullURL(), body)
273274
}

prometheus/push/push_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func TestPush(t *testing.T) {
3333
lastPath string
3434
)
3535

36-
// Fake a Pushgateway that always responds with 202.
36+
// Fake a Pushgateway that responds with 202 to DELETE and with 200 in
37+
// all other cases.
3738
pgwOK := httptest.NewServer(
3839
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3940
lastMethod = r.Method
@@ -44,7 +45,11 @@ func TestPush(t *testing.T) {
4445
}
4546
lastPath = r.URL.EscapedPath()
4647
w.Header().Set("Content-Type", `text/plain; charset=utf-8`)
47-
w.WriteHeader(http.StatusAccepted)
48+
if r.Method == http.MethodDelete {
49+
w.WriteHeader(http.StatusAccepted)
50+
return
51+
}
52+
w.WriteHeader(http.StatusOK)
4853
}),
4954
)
5055
defer pgwOK.Close()

0 commit comments

Comments
 (0)