Merge pull request #559 from nghialt/master

Add HTTPClient interface to Pusher struct
This commit is contained in:
Björn Rabenstein 2019-05-06 01:31:30 +02:00 committed by GitHub
commit f1d50bcc55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -50,6 +50,11 @@ import (
const contentTypeHeader = "Content-Type" const contentTypeHeader = "Content-Type"
// HTTPDoer is an interface for the one method of http.Client that is used by Pusher
type HTTPDoer interface {
Do(*http.Request) (*http.Response, error)
}
// Pusher manages a push to the Pushgateway. Use New to create one, configure it // Pusher manages a push to the Pushgateway. Use New to create one, configure it
// with its methods, and finally use the Add or Push method to push. // with its methods, and finally use the Add or Push method to push.
type Pusher struct { type Pusher struct {
@ -61,7 +66,7 @@ type Pusher struct {
gatherers prometheus.Gatherers gatherers prometheus.Gatherers
registerer prometheus.Registerer registerer prometheus.Registerer
client *http.Client client HTTPDoer
useBasicAuth bool useBasicAuth bool
username, password string username, password string
@ -170,7 +175,11 @@ func (p *Pusher) Grouping(name, value string) *Pusher {
// Client sets a custom HTTP client for the Pusher. For convenience, this method // Client sets a custom HTTP client for the Pusher. For convenience, this method
// returns a pointer to the Pusher itself. // returns a pointer to the Pusher itself.
func (p *Pusher) Client(c *http.Client) *Pusher { // Pusher only needs one method of the custom HTTP client: Do(*http.Request).
// Thus, rather than requiring a fully fledged http.Client,
// the provided client only needs to implement the HTTPDoer interface.
// Since *http.Client naturally implements that interface, it can still be used normally.
func (p *Pusher) Client(c HTTPDoer) *Pusher {
p.client = c p.client = c
return p return p
} }