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

Feature/http response configurable proxy #3374 #3626

Merged
Merged
Changes from 3 commits
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
22 changes: 21 additions & 1 deletion plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// HTTPResponse struct
type HTTPResponse struct {
Address string
Proxy string
Body string
Method string
ResponseTimeout internal.Duration
Expand Down Expand Up @@ -49,6 +50,9 @@ var sampleConfig = `
## Server address (default http://localhost)
# address = "http://localhost"

## Set proxy (telegraf uses the system wide proxy settings if proxy is not set)
# proxy = "http://localhost:8888"
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change this to http_proxy to match the variable name in other plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, sounds legit. Did that and will push it right after I tested it.


## Set response_timeout (default 5 seconds)
# response_timeout = "5s"

Expand Down Expand Up @@ -88,6 +92,22 @@ func (h *HTTPResponse) SampleConfig() string {
// ErrRedirectAttempted indicates that a redirect occurred
var ErrRedirectAttempted = errors.New("redirect")

// Set the proxy. A configured proxy overwrites the system wide proxy.
func getProxyFunc(proxy string) func(*http.Request) (*url.URL, error) {
if proxy == "" {
return http.ProxyFromEnvironment
}
proxyURL, err := url.Parse(proxy)
if err != nil {
return func(_ *http.Request) (*url.URL, error) {
return nil, errors.New("bad proxy: " + err.Error())
}
}
return func(r *http.Request) (*url.URL, error) {
return proxyURL, nil
}
}

// CreateHttpClient creates an http client which will timeout at the specified
// timeout period and can follow redirects if specified
func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
Expand All @@ -98,7 +118,7 @@ func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Proxy: getProxyFunc(h.Proxy),
DisableKeepAlives: true,
TLSClientConfig: tlsCfg,
},
Expand Down