Skip to content

Commit 2771bcc

Browse files
authored
Add Header method to Pusher for custom header (#1218)
Signed-off-by: songjiayang <[email protected]>
1 parent 2fced96 commit 2771bcc

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

prometheus/push/push.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type Pusher struct {
7777
registerer prometheus.Registerer
7878

7979
client HTTPDoer
80+
header http.Header
8081
useBasicAuth bool
8182
username, password string
8283

@@ -201,6 +202,13 @@ func (p *Pusher) Client(c HTTPDoer) *Pusher {
201202
return p
202203
}
203204

205+
// Header sets a custom HTTP header for the Pusher's client. For convenience, this method
206+
// returns a pointer to the Pusher itself.
207+
func (p *Pusher) Header(header http.Header) *Pusher {
208+
p.header = header
209+
return p
210+
}
211+
204212
// BasicAuth configures the Pusher to use HTTP Basic Authentication with the
205213
// provided username and password. For convenience, this method returns a
206214
// pointer to the Pusher itself.
@@ -236,6 +244,9 @@ func (p *Pusher) Delete() error {
236244
if err != nil {
237245
return err
238246
}
247+
if p.header != nil {
248+
req.Header = p.header
249+
}
239250
if p.useBasicAuth {
240251
req.SetBasicAuth(p.username, p.password)
241252
}
@@ -286,6 +297,9 @@ func (p *Pusher) push(ctx context.Context, method string) error {
286297
if err != nil {
287298
return err
288299
}
300+
if p.header != nil {
301+
req.Header = p.header
302+
}
289303
if p.useBasicAuth {
290304
req.SetBasicAuth(p.username, p.password)
291305
}

prometheus/push/push_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ func TestPush(t *testing.T) {
3131
lastMethod string
3232
lastBody []byte
3333
lastPath string
34+
lastHeader http.Header
3435
)
3536

3637
// Fake a Pushgateway that responds with 202 to DELETE and with 200 in
3738
// all other cases.
3839
pgwOK := httptest.NewServer(
3940
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4041
lastMethod = r.Method
42+
lastHeader = r.Header
4143
var err error
4244
lastBody, err = io.ReadAll(r.Body)
4345
if err != nil {
@@ -281,4 +283,27 @@ func TestPush(t *testing.T) {
281283
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
282284
t.Error("unexpected path:", lastPath)
283285
}
286+
287+
// Push some Collectors with custom header, all good.
288+
header := make(http.Header)
289+
header.Set("Authorization", "Bearer Token")
290+
if err := New(pgwOK.URL, "testjob").
291+
Collector(metric1).
292+
Collector(metric2).
293+
Header(header).
294+
Push(); err != nil {
295+
t.Fatal(err)
296+
}
297+
if lastMethod != http.MethodPut {
298+
t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPut)
299+
}
300+
if !bytes.Equal(lastBody, wantBody) {
301+
t.Errorf("got body %v, want %v", lastBody, wantBody)
302+
}
303+
if lastPath != "/metrics/job/testjob" {
304+
t.Error("unexpected path:", lastPath)
305+
}
306+
if lastHeader == nil || lastHeader.Get("Authorization") == "" {
307+
t.Error("empty Authorization header")
308+
}
284309
}

0 commit comments

Comments
 (0)