Skip to content

Commit

Permalink
Add MustCollector API for pusher
Browse files Browse the repository at this point in the history
Signed-off-by: kun <oiooj@qq.com>
  • Loading branch information
oiooj committed Jun 17, 2022
1 parent 2cfd1eb commit d1fe320
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions prometheus/push/push.go
Expand Up @@ -168,6 +168,16 @@ func (p *Pusher) Collector(c prometheus.Collector) *Pusher {
return p
}

// MustCollector works like Collector but registers any number of
// Collectors and panics upon the first registration that causes an
// error.
//
// For convenience, this method returns a pointer to the Pusher itself.
func (p *Pusher) MustCollector(c ...prometheus.Collector) *Pusher {
p.registerer.MustRegister(c...)
return p
}

// Grouping adds a label pair to the grouping key of the Pusher, replacing any
// previously added label pair with the same label name. Note that setting any
// labels in the grouping key that are already contained in the metrics to push
Expand Down
16 changes: 16 additions & 0 deletions prometheus/push/push_test.go
Expand Up @@ -107,6 +107,22 @@ func TestPush(t *testing.T) {
t.Error("unexpected path:", lastPath)
}

// use MustCollectors, all good.
if err := New(pgwOK.URL, "testjob").
MustCollector(metric1, metric2).
Push(); err != nil {
t.Fatal(err)
}
if lastMethod != http.MethodPut {
t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
}
if !bytes.Equal(lastBody, wantBody) {
t.Errorf("got body %v, want %v", lastBody, wantBody)
}
if lastPath != "/metrics/job/testjob" {
t.Error("unexpected path:", lastPath)
}

// Add some Collectors, with nil grouping, all good.
if err := New(pgwOK.URL, "testjob").
Collector(metric1).
Expand Down

0 comments on commit d1fe320

Please sign in to comment.