Skip to content

Commit

Permalink
add proxy (influxdata#8915)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivorybilled authored Feb 26, 2021
1 parent 956350d commit accf913
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ API endpoint. In the following order the plugin will attempt to authenticate.
## ex: endpoint_url = "http://localhost:8000"
# endpoint_url = ""

## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
# http_proxy_url = "http://localhost:8888"

# The minimum period for Cloudwatch metrics is 1 minute (60s). However not all
# metrics are made available to the 1 minute period. Some are collected at
# 3 minute, 5 minute, or larger intervals. See https://aws.amazon.com/cloudwatch/faqs/#monitoring.
Expand Down
22 changes: 19 additions & 3 deletions plugins/inputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/limiter"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/common/proxy"
"github.com/influxdata/telegraf/plugins/inputs"
)

Expand All @@ -35,6 +36,8 @@ type CloudWatch struct {
StatisticInclude []string `toml:"statistic_include"`
Timeout config.Duration `toml:"timeout"`

proxy.HTTPProxy

Period config.Duration `toml:"period"`
Delay config.Duration `toml:"delay"`
Namespace string `toml:"namespace"`
Expand Down Expand Up @@ -107,6 +110,9 @@ func (c *CloudWatch) SampleConfig() string {
## ex: endpoint_url = "http://localhost:8000"
# endpoint_url = ""
## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
# http_proxy_url = "http://localhost:8888"
# The minimum period for Cloudwatch metrics is 1 minute (60s). However not all
# metrics are made available to the 1 minute period. Some are collected at
# 3 minute, 5 minute, or larger intervals. See https://aws.amazon.com/cloudwatch/faqs/#monitoring.
Expand Down Expand Up @@ -188,7 +194,10 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
}

if c.client == nil {
c.initializeCloudWatch()
err := c.initializeCloudWatch()
if err != nil {
return err
}
}

filteredMetrics, err := getFilteredMetrics(c)
Expand Down Expand Up @@ -249,7 +258,7 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
return c.aggregateMetrics(acc, results)
}

func (c *CloudWatch) initializeCloudWatch() {
func (c *CloudWatch) initializeCloudWatch() error {
credentialConfig := &internalaws.CredentialConfig{
Region: c.Region,
AccessKey: c.AccessKey,
Expand All @@ -262,11 +271,16 @@ func (c *CloudWatch) initializeCloudWatch() {
}
configProvider := credentialConfig.Credentials()

proxy, err := c.HTTPProxy.Proxy()
if err != nil {
return err
}

cfg := &aws.Config{
HTTPClient: &http.Client{
// use values from DefaultTransport
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Proxy: proxy,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
Expand All @@ -283,6 +297,8 @@ func (c *CloudWatch) initializeCloudWatch() {

loglevel := aws.LogOff
c.client = cloudwatch.New(configProvider, cfg.WithLogLevel(loglevel))

return nil
}

type filteredMetric struct {
Expand Down
15 changes: 15 additions & 0 deletions plugins/inputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cloudwatch

import (
"net/http"
"testing"
"time"

Expand All @@ -11,6 +12,7 @@ import (

"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/common/proxy"
"github.com/influxdata/telegraf/testutil"
)

Expand Down Expand Up @@ -333,3 +335,16 @@ func TestUpdateWindow(t *testing.T) {
assert.EqualValues(t, c.windowEnd, now.Add(-time.Duration(c.Delay)))
assert.EqualValues(t, c.windowStart, newStartTime)
}

func TestProxyFunction(t *testing.T) {
c := &CloudWatch{
HTTPProxy: proxy.HTTPProxy{HTTPProxyURL: "http://www.penguins.com"},
}

proxyFunction, err := c.HTTPProxy.Proxy()
require.NoError(t, err)

proxyResult, err := proxyFunction(&http.Request{})
require.NoError(t, err)
require.Equal(t, "www.penguins.com", proxyResult.Host)
}

0 comments on commit accf913

Please sign in to comment.