Add Delete method
Fixes #611. Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
parent
f1c4042316
commit
9a1440d469
|
@ -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
|
||||||
|
|
|
@ -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 != "DELETE" {
|
||||||
|
t.Error("want method DELETE for delete, got", lastMethod)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue