Skip to content

Commit d1fe320

Browse files
committed
Add MustCollector API for pusher
Signed-off-by: kun <[email protected]>
1 parent 2cfd1eb commit d1fe320

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

prometheus/push/push.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ func (p *Pusher) Collector(c prometheus.Collector) *Pusher {
168168
return p
169169
}
170170

171+
// MustCollector works like Collector but registers any number of
172+
// Collectors and panics upon the first registration that causes an
173+
// error.
174+
//
175+
// For convenience, this method returns a pointer to the Pusher itself.
176+
func (p *Pusher) MustCollector(c ...prometheus.Collector) *Pusher {
177+
p.registerer.MustRegister(c...)
178+
return p
179+
}
180+
171181
// Grouping adds a label pair to the grouping key of the Pusher, replacing any
172182
// previously added label pair with the same label name. Note that setting any
173183
// labels in the grouping key that are already contained in the metrics to push

prometheus/push/push_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ func TestPush(t *testing.T) {
107107
t.Error("unexpected path:", lastPath)
108108
}
109109

110+
// use MustCollectors, all good.
111+
if err := New(pgwOK.URL, "testjob").
112+
MustCollector(metric1, metric2).
113+
Push(); err != nil {
114+
t.Fatal(err)
115+
}
116+
if lastMethod != http.MethodPut {
117+
t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
118+
}
119+
if !bytes.Equal(lastBody, wantBody) {
120+
t.Errorf("got body %v, want %v", lastBody, wantBody)
121+
}
122+
if lastPath != "/metrics/job/testjob" {
123+
t.Error("unexpected path:", lastPath)
124+
}
125+
110126
// Add some Collectors, with nil grouping, all good.
111127
if err := New(pgwOK.URL, "testjob").
112128
Collector(metric1).

0 commit comments

Comments
 (0)