Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPClient interface to Pusher struct #559

Merged
merged 2 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions prometheus/push/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Let's not create a new file for a one-method interface. This interface is tightly coupled to the Pusher type. Let's put it into the same file.
  2. Let's call it HTTPDoer as it doesn't really represent a fully fledged http.Client.
  3. Suggested doc comment to tell the reader why we are doing all of this: // HTTPDoer is an interface for the one method of http.Client that is used by Pusher.

Do(*http.Request) (*http.Response, error)
}
4 changes: 2 additions & 2 deletions prometheus/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Pusher struct {
gatherers prometheus.Gatherers
registerer prometheus.Registerer

client *http.Client
client HTTPClient
useBasicAuth bool
username, password string

Expand Down Expand Up @@ -170,7 +170,7 @@ 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 *http.Client) *Pusher {
func (p *Pusher) Client(c HTTPClient) *Pusher {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment needs to be amended. Suggestion for additional text:

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.

p.client = c
return p
}
Expand Down