proxy support for http input (#8477)

This commit is contained in:
Steven Soroka 2020-11-26 17:16:25 -05:00 committed by GitHub
parent a7096c8128
commit 4090c77275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 5 deletions

View File

@ -0,0 +1,24 @@
package proxy
import (
"fmt"
"net/http"
"net/url"
)
type HTTPProxy struct {
HTTPProxyURL string `toml:"http_proxy_url"`
}
type proxyFunc func(req *http.Request) (*url.URL, error)
func (p *HTTPProxy) Proxy() (proxyFunc, error) {
if len(p.HTTPProxyURL) > 0 {
url, err := url.Parse(p.HTTPProxyURL)
if err != nil {
return nil, fmt.Errorf("error parsing proxy url %q: %w", p.HTTPProxyURL, err)
}
return http.ProxyURL(url), nil
}
return http.ProxyFromEnvironment, nil
}

View File

@ -34,6 +34,9 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
# username = "username"
# password = "pa$$word"
## HTTP Proxy support
# http_proxy_url = ""
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"

View File

@ -11,6 +11,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/proxy"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
@ -29,6 +30,8 @@ type HTTP struct {
Password string `toml:"password"`
tls.ClientConfig
proxy.HTTPProxy
// Absolute path to file with Bearer token
BearerToken string `toml:"bearer_token"`
@ -70,6 +73,9 @@ var sampleConfig = `
## compress body or "identity" to apply no encoding.
# content_encoding = "identity"
## HTTP Proxy support
# http_proxy_url = ""
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
@ -106,12 +112,19 @@ func (h *HTTP) Init() error {
return err
}
proxy, err := h.HTTPProxy.Proxy()
if err != nil {
return err
}
transport := &http.Transport{
TLSClientConfig: tlsCfg,
Proxy: proxy,
}
h.client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
Proxy: http.ProxyFromEnvironment,
},
Timeout: h.Timeout.Duration,
Transport: transport,
Timeout: h.Timeout.Duration,
}
// Set default as [200]