Merge pull request #613 from prometheus/beorn7/push

Add Delete method to Pusher
This commit is contained in:
Björn Rabenstein 2019-06-28 18:29:03 +02:00 committed by GitHub
commit a6c69798cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 10 deletions

View File

@ -117,14 +117,14 @@ func New(url, job string) *Pusher {
// Push returns the first error encountered by any method call (including this // Push returns the first error encountered by any method call (including this
// one) in the lifetime of the Pusher. // one) in the lifetime of the Pusher.
func (p *Pusher) Push() error { func (p *Pusher) Push() error {
return p.push("PUT") return p.push(http.MethodPut)
} }
// Add works like push, but only previously pushed metrics with the same name // Add works like push, but only previously pushed metrics with the same name
// (and the same job and other grouping labels) will be replaced. (It uses HTTP // (and the same job and other grouping labels) will be replaced. (It uses HTTP
// method “POST” to push to the Pushgateway.) // method “POST” to push to the Pushgateway.)
func (p *Pusher) Add() error { func (p *Pusher) Add() error {
return p.push("POST") return p.push(http.MethodPost)
} }
// Gatherer adds a Gatherer to the Pusher, from which metrics will be gathered // 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 {
return p return p
} }
// Delete sends a “DELETE” request to the Pushgateway configured while creating
// this Pusher, using the configured job name and any added grouping labels as
// grouping key. Any added Gatherers and Collectors added to this Pusher are
// ignored by this method.
//
// Delete returns the first error encountered by any method call (including this
// one) in the lifetime of the Pusher.
func (p *Pusher) Delete() error {
if p.error != nil {
return p.error
}
urlComponents := []string{url.QueryEscape(p.job)}
for ln, lv := range p.grouping {
urlComponents = append(urlComponents, ln, lv)
}
deleteURL := fmt.Sprintf("%s/metrics/job/%s", p.url, strings.Join(urlComponents, "/"))
req, err := http.NewRequest(http.MethodDelete, deleteURL, nil)
if err != nil {
return err
}
if p.useBasicAuth {
req.SetBasicAuth(p.username, p.password)
}
resp, err := p.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 202 {
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, deleteURL, body)
}
return nil
}
func (p *Pusher) push(method string) error { func (p *Pusher) push(method string) error {
if p.error != nil { if p.error != nil {
return p.error return p.error

View File

@ -93,8 +93,8 @@ func TestPush(t *testing.T) {
Push(); err != nil { Push(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if lastMethod != "PUT" { if lastMethod != http.MethodPut {
t.Error("want method PUT for Push, got", lastMethod) t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
} }
if !bytes.Equal(lastBody, wantBody) { if !bytes.Equal(lastBody, wantBody) {
t.Errorf("got body %v, want %v", lastBody, wantBody) t.Errorf("got body %v, want %v", lastBody, wantBody)
@ -110,8 +110,8 @@ func TestPush(t *testing.T) {
Add(); err != nil { Add(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if lastMethod != "POST" { if lastMethod != http.MethodPost {
t.Error("want method POST for Add, got", lastMethod) t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
} }
if !bytes.Equal(lastBody, wantBody) { if !bytes.Equal(lastBody, wantBody) {
t.Errorf("got body %v, want %v", lastBody, wantBody) t.Errorf("got body %v, want %v", lastBody, wantBody)
@ -167,8 +167,8 @@ func TestPush(t *testing.T) {
Push(); err != nil { Push(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if lastMethod != "PUT" { if lastMethod != http.MethodPut {
t.Error("want method PUT for Push, got", lastMethod) t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
} }
if !bytes.Equal(lastBody, wantBody) { if !bytes.Equal(lastBody, wantBody) {
t.Errorf("got body %v, want %v", lastBody, wantBody) t.Errorf("got body %v, want %v", lastBody, wantBody)
@ -182,8 +182,8 @@ func TestPush(t *testing.T) {
Add(); err != nil { Add(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if lastMethod != "POST" { if lastMethod != http.MethodPost {
t.Error("want method POST for Add, got", lastMethod) t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
} }
if !bytes.Equal(lastBody, wantBody) { if !bytes.Equal(lastBody, wantBody) {
t.Errorf("got body %v, want %v", lastBody, wantBody) t.Errorf("got body %v, want %v", lastBody, wantBody)
@ -191,4 +191,22 @@ func TestPush(t *testing.T) {
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" { if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
t.Error("unexpected path:", lastPath) t.Error("unexpected path:", lastPath)
} }
// Delete, all good.
if err := New(pgwOK.URL, "testjob").
Grouping("a", "x").
Grouping("b", "y").
Delete(); err != nil {
t.Fatal(err)
}
if lastMethod != http.MethodDelete {
t.Errorf("got method %q for Delete, want %q", lastMethod, http.MethodDelete)
}
if len(lastBody) != 0 {
t.Errorf("got body of length %d, want empty body", len(lastBody))
}
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
t.Error("unexpected path:", lastPath)
}
} }