From 11ad170c32567c95b92d4356403f2b2a721baa64 Mon Sep 17 00:00:00 2001 From: NghiaLT Date: Mon, 6 May 2019 04:18:09 +0700 Subject: [PATCH] Replace HTTPClient with HTTPDoer; Update document accordingly Signed-off-by: NghiaLT --- prometheus/push/http.go | 21 --------------------- prometheus/push/push.go | 13 +++++++++++-- 2 files changed, 11 insertions(+), 23 deletions(-) delete mode 100644 prometheus/push/http.go diff --git a/prometheus/push/http.go b/prometheus/push/http.go deleted file mode 100644 index 208adf38c..000000000 --- a/prometheus/push/http.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2019 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package push - -import "net/http" - -// HTTPClient is a interface for http client -type HTTPClient interface { - Do(*http.Request) (*http.Response, error) -} diff --git a/prometheus/push/push.go b/prometheus/push/push.go index f1459194f..79d61557f 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -50,6 +50,11 @@ import ( const contentTypeHeader = "Content-Type" +// HTTPDoer is a interface for http client +type HTTPDoer interface { + Do(*http.Request) (*http.Response, error) +} + // 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. type Pusher struct { @@ -61,7 +66,7 @@ type Pusher struct { gatherers prometheus.Gatherers registerer prometheus.Registerer - client HTTPClient + client HTTPDoer useBasicAuth bool 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 // returns a pointer to the Pusher itself. -func (p *Pusher) Client(c HTTPClient) *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 return p }