Skip to content

Commit

Permalink
Add Header method to Pusher for custom header (#1218)
Browse files Browse the repository at this point in the history
Signed-off-by: songjiayang <songjiayang1@gmail.com>
  • Loading branch information
songjiayang authored Feb 7, 2023
1 parent 2fced96 commit 2771bcc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions prometheus/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Pusher struct {
registerer prometheus.Registerer

client HTTPDoer
header http.Header
useBasicAuth bool
username, password string

Expand Down Expand Up @@ -201,6 +202,13 @@ func (p *Pusher) Client(c HTTPDoer) *Pusher {
return p
}

// Header sets a custom HTTP header for the Pusher's client. For convenience, this method
// returns a pointer to the Pusher itself.
func (p *Pusher) Header(header http.Header) *Pusher {
p.header = header
return p
}

// BasicAuth configures the Pusher to use HTTP Basic Authentication with the
// provided username and password. For convenience, this method returns a
// pointer to the Pusher itself.
Expand Down Expand Up @@ -236,6 +244,9 @@ func (p *Pusher) Delete() error {
if err != nil {
return err
}
if p.header != nil {
req.Header = p.header
}
if p.useBasicAuth {
req.SetBasicAuth(p.username, p.password)
}
Expand Down Expand Up @@ -286,6 +297,9 @@ func (p *Pusher) push(ctx context.Context, method string) error {
if err != nil {
return err
}
if p.header != nil {
req.Header = p.header
}
if p.useBasicAuth {
req.SetBasicAuth(p.username, p.password)
}
Expand Down
25 changes: 25 additions & 0 deletions prometheus/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func TestPush(t *testing.T) {
lastMethod string
lastBody []byte
lastPath string
lastHeader http.Header
)

// Fake a Pushgateway that responds with 202 to DELETE and with 200 in
// all other cases.
pgwOK := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastMethod = r.Method
lastHeader = r.Header
var err error
lastBody, err = io.ReadAll(r.Body)
if err != nil {
Expand Down Expand Up @@ -281,4 +283,27 @@ func TestPush(t *testing.T) {
if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
t.Error("unexpected path:", lastPath)
}

// Push some Collectors with custom header, all good.
header := make(http.Header)
header.Set("Authorization", "Bearer Token")
if err := New(pgwOK.URL, "testjob").
Collector(metric1).
Collector(metric2).
Header(header).
Push(); err != nil {
t.Fatal(err)
}
if lastMethod != http.MethodPut {
t.Errorf("got method %q for Add, 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)
}
if lastHeader == nil || lastHeader.Get("Authorization") == "" {
t.Error("empty Authorization header")
}
}

0 comments on commit 2771bcc

Please sign in to comment.