Skip to content

Commit

Permalink
proxy support for http input (influxdata#8477)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka authored Nov 26, 2020
1 parent 1aaf1f9 commit c45a87d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
24 changes: 24 additions & 0 deletions plugins/common/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions plugins/inputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 18 additions & 5 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit c45a87d

Please sign in to comment.