diff --git a/plugins/inputs/cloudwatch/README.md b/plugins/inputs/cloudwatch/README.md index bc7b9b50c..c86e66e67 100644 --- a/plugins/inputs/cloudwatch/README.md +++ b/plugins/inputs/cloudwatch/README.md @@ -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. diff --git a/plugins/inputs/cloudwatch/cloudwatch.go b/plugins/inputs/cloudwatch/cloudwatch.go index 10f34a41f..1bc5379e5 100644 --- a/plugins/inputs/cloudwatch/cloudwatch.go +++ b/plugins/inputs/cloudwatch/cloudwatch.go @@ -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" ) @@ -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"` @@ -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. @@ -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) @@ -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, @@ -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, @@ -283,6 +297,8 @@ func (c *CloudWatch) initializeCloudWatch() { loglevel := aws.LogOff c.client = cloudwatch.New(configProvider, cfg.WithLogLevel(loglevel)) + + return nil } type filteredMetric struct { diff --git a/plugins/inputs/cloudwatch/cloudwatch_test.go b/plugins/inputs/cloudwatch/cloudwatch_test.go index 2983773ad..798cdff1f 100644 --- a/plugins/inputs/cloudwatch/cloudwatch_test.go +++ b/plugins/inputs/cloudwatch/cloudwatch_test.go @@ -1,6 +1,7 @@ package cloudwatch import ( + "net/http" "testing" "time" @@ -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" ) @@ -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) +}