Skip to content

Commit

Permalink
Add timeout option to cloudwatch input (influxdata#6553)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and idohalevi committed Sep 23, 2020
1 parent eee364b commit 1be61aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ API endpoint. In the following order the plugin will attempt to authenticate.
## See http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
# ratelimit = 25

## Timeout for http requests made by the cloudwatch client.
# timeout = "5s"

## Namespace-wide statistic filters. These allow fewer queries to be made to
## cloudwatch.
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]
Expand Down
59 changes: 39 additions & 20 deletions plugins/inputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cloudwatch
import (
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"sync"
Expand All @@ -23,16 +25,17 @@ import (
type (
// CloudWatch contains the configuration and cache for the cloudwatch plugin.
CloudWatch struct {
Region string `toml:"region"`
AccessKey string `toml:"access_key"`
SecretKey string `toml:"secret_key"`
RoleARN string `toml:"role_arn"`
Profile string `toml:"profile"`
CredentialPath string `toml:"shared_credential_file"`
Token string `toml:"token"`
EndpointURL string `toml:"endpoint_url"`
StatisticExclude []string `toml:"statistic_exclude"`
StatisticInclude []string `toml:"statistic_include"`
Region string `toml:"region"`
AccessKey string `toml:"access_key"`
SecretKey string `toml:"secret_key"`
RoleARN string `toml:"role_arn"`
Profile string `toml:"profile"`
CredentialPath string `toml:"shared_credential_file"`
Token string `toml:"token"`
EndpointURL string `toml:"endpoint_url"`
StatisticExclude []string `toml:"statistic_exclude"`
StatisticInclude []string `toml:"statistic_include"`
Timeout internal.Duration `toml:"timeout"`

Period internal.Duration `toml:"period"`
Delay internal.Duration `toml:"delay"`
Expand Down Expand Up @@ -133,6 +136,9 @@ func (c *CloudWatch) SampleConfig() string {
## See http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
# ratelimit = 25
## Timeout for http requests made by the cloudwatch client.
# timeout = "5s"
## Namespace-wide statistic filters. These allow fewer queries to be made to
## cloudwatch.
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]
Expand Down Expand Up @@ -183,10 +189,7 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
return err
}

err = c.updateWindow(time.Now())
if err != nil {
return err
}
c.updateWindow(time.Now())

// Get all of the possible queries so we can send groups of 100.
queries, err := c.getDataQueries(filteredMetrics)
Expand Down Expand Up @@ -235,7 +238,7 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
return c.aggregateMetrics(acc, results)
}

func (c *CloudWatch) initializeCloudWatch() error {
func (c *CloudWatch) initializeCloudWatch() {
credentialConfig := &internalaws.CredentialConfig{
Region: c.Region,
AccessKey: c.AccessKey,
Expand All @@ -248,10 +251,27 @@ func (c *CloudWatch) initializeCloudWatch() error {
}
configProvider := credentialConfig.Credentials()

cfg := &aws.Config{}
cfg := &aws.Config{
HTTPClient: &http.Client{
// use values from DefaultTransport
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: c.Timeout.Duration,
},
}

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

type filteredMetric struct {
Expand Down Expand Up @@ -370,7 +390,7 @@ func (c *CloudWatch) fetchNamespaceMetrics() ([]*cloudwatch.Metric, error) {
return metrics, nil
}

func (c *CloudWatch) updateWindow(relativeTo time.Time) error {
func (c *CloudWatch) updateWindow(relativeTo time.Time) {
windowEnd := relativeTo.Add(-c.Delay.Duration)

if c.windowEnd.IsZero() {
Expand All @@ -382,8 +402,6 @@ func (c *CloudWatch) updateWindow(relativeTo time.Time) error {
}

c.windowEnd = windowEnd

return nil
}

// getDataQueries gets all of the possible queries so we can maximize the request payload.
Expand Down Expand Up @@ -535,6 +553,7 @@ func init() {
return &CloudWatch{
CacheTTL: internal.Duration{Duration: time.Hour},
RateLimit: 25,
Timeout: internal.Duration{Duration: time.Second * 5},
}
})
}
Expand Down

0 comments on commit 1be61aa

Please sign in to comment.