Skip to content

Commit a6c6979

Browse files
authored
Merge pull request #613 from prometheus/beorn7/push
Add Delete method to Pusher
2 parents f1c4042 + 48cd700 commit a6c6979

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

prometheus/push/push.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ func New(url, job string) *Pusher {
117117
// Push returns the first error encountered by any method call (including this
118118
// one) in the lifetime of the Pusher.
119119
func (p *Pusher) Push() error {
120-
return p.push("PUT")
120+
return p.push(http.MethodPut)
121121
}
122122

123123
// Add works like push, but only previously pushed metrics with the same name
124124
// (and the same job and other grouping labels) will be replaced. (It uses HTTP
125125
// method “POST” to push to the Pushgateway.)
126126
func (p *Pusher) Add() error {
127-
return p.push("POST")
127+
return p.push(http.MethodPost)
128128
}
129129

130130
// Gatherer adds a Gatherer to the Pusher, from which metrics will be gathered
@@ -204,6 +204,42 @@ func (p *Pusher) Format(format expfmt.Format) *Pusher {
204204
return p
205205
}
206206

207+
// Delete sends a “DELETE” request to the Pushgateway configured while creating
208+
// this Pusher, using the configured job name and any added grouping labels as
209+
// grouping key. Any added Gatherers and Collectors added to this Pusher are
210+
// ignored by this method.
211+
//
212+
// Delete returns the first error encountered by any method call (including this
213+
// one) in the lifetime of the Pusher.
214+
func (p *Pusher) Delete() error {
215+
if p.error != nil {
216+
return p.error
217+
}
218+
urlComponents := []string{url.QueryEscape(p.job)}
219+
for ln, lv := range p.grouping {
220+
urlComponents = append(urlComponents, ln, lv)
221+
}
222+
deleteURL := fmt.Sprintf("%s/metrics/job/%s", p.url, strings.Join(urlComponents, "/"))
223+
224+
req, err := http.NewRequest(http.MethodDelete, deleteURL, nil)
225+
if err != nil {
226+
return err
227+
}
228+
if p.useBasicAuth {
229+
req.SetBasicAuth(p.username, p.password)
230+
}
231+
resp, err := p.client.Do(req)
232+
if err != nil {
233+
return err
234+
}
235+
defer resp.Body.Close()
236+
if resp.StatusCode != 202 {
237+
body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only.
238+
return fmt.Errorf("unexpected status code %d while deleting %s: %s", resp.StatusCode, deleteURL, body)
239+
}
240+
return nil
241+
}
242+
207243
func (p *Pusher) push(method string) error {
208244
if p.error != nil {
209245
return p.error

prometheus/push/push_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func TestPush(t *testing.T) {
9393
Push(); err != nil {
9494
t.Fatal(err)
9595
}
96-
if lastMethod != "PUT" {
97-
t.Error("want method PUT for Push, got", lastMethod)
96+
if lastMethod != http.MethodPut {
97+
t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
9898
}
9999
if !bytes.Equal(lastBody, wantBody) {
100100
t.Errorf("got body %v, want %v", lastBody, wantBody)
@@ -110,8 +110,8 @@ func TestPush(t *testing.T) {
110110
Add(); err != nil {
111111
t.Fatal(err)
112112
}
113-
if lastMethod != "POST" {
114-
t.Error("want method POST for Add, got", lastMethod)
113+
if lastMethod != http.MethodPost {
114+
t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
115115
}
116116
if !bytes.Equal(lastBody, wantBody) {
117117
t.Errorf("got body %v, want %v", lastBody, wantBody)
@@ -167,8 +167,8 @@ func TestPush(t *testing.T) {
167167
Push(); err != nil {
168168
t.Fatal(err)
169169
}
170-
if lastMethod != "PUT" {
171-
t.Error("want method PUT for Push, got", lastMethod)
170+
if lastMethod != http.MethodPut {
171+
t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
172172
}
173173
if !bytes.Equal(lastBody, wantBody) {
174174
t.Errorf("got body %v, want %v", lastBody, wantBody)
@@ -182,13 +182,31 @@ func TestPush(t *testing.T) {
182182
Add(); err != nil {
183183
t.Fatal(err)
184184
}
185-
if lastMethod != "POST" {
186-
t.Error("want method POST for Add, got", lastMethod)
185+
if lastMethod != http.MethodPost {
186+
t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
187187
}
188188
if !bytes.Equal(lastBody, wantBody) {
189189
t.Errorf("got body %v, want %v", lastBody, wantBody)
190190
}
191191
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
192192
t.Error("unexpected path:", lastPath)
193193
}
194+
195+
// Delete, all good.
196+
if err := New(pgwOK.URL, "testjob").
197+
Grouping("a", "x").
198+
Grouping("b", "y").
199+
Delete(); err != nil {
200+
t.Fatal(err)
201+
}
202+
if lastMethod != http.MethodDelete {
203+
t.Errorf("got method %q for Delete, want %q", lastMethod, http.MethodDelete)
204+
}
205+
if len(lastBody) != 0 {
206+
t.Errorf("got body of length %d, want empty body", len(lastBody))
207+
}
208+
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
209+
t.Error("unexpected path:", lastPath)
210+
}
211+
194212
}

0 commit comments

Comments
 (0)