Skip to content

Commit 83682eb

Browse files
glintondanielnelson
authored andcommitted
Add timeout option to cloudwatch input (influxdata#6553)
1 parent 43e3390 commit 83682eb

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

plugins/inputs/cloudwatch/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ API endpoint. In the following order the plugin will attempt to authenticate.
7070
## See http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
7171
# ratelimit = 25
7272

73+
## Timeout for http requests made by the cloudwatch client.
74+
# timeout = "5s"
75+
7376
## Namespace-wide statistic filters. These allow fewer queries to be made to
7477
## cloudwatch.
7578
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]

plugins/inputs/cloudwatch/cloudwatch.go

+39-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cloudwatch
33
import (
44
"errors"
55
"fmt"
6+
"net"
7+
"net/http"
68
"strconv"
79
"strings"
810
"sync"
@@ -23,16 +25,17 @@ import (
2325
type (
2426
// CloudWatch contains the configuration and cache for the cloudwatch plugin.
2527
CloudWatch struct {
26-
Region string `toml:"region"`
27-
AccessKey string `toml:"access_key"`
28-
SecretKey string `toml:"secret_key"`
29-
RoleARN string `toml:"role_arn"`
30-
Profile string `toml:"profile"`
31-
CredentialPath string `toml:"shared_credential_file"`
32-
Token string `toml:"token"`
33-
EndpointURL string `toml:"endpoint_url"`
34-
StatisticExclude []string `toml:"statistic_exclude"`
35-
StatisticInclude []string `toml:"statistic_include"`
28+
Region string `toml:"region"`
29+
AccessKey string `toml:"access_key"`
30+
SecretKey string `toml:"secret_key"`
31+
RoleARN string `toml:"role_arn"`
32+
Profile string `toml:"profile"`
33+
CredentialPath string `toml:"shared_credential_file"`
34+
Token string `toml:"token"`
35+
EndpointURL string `toml:"endpoint_url"`
36+
StatisticExclude []string `toml:"statistic_exclude"`
37+
StatisticInclude []string `toml:"statistic_include"`
38+
Timeout internal.Duration `toml:"timeout"`
3639

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

186-
err = c.updateWindow(time.Now())
187-
if err != nil {
188-
return err
189-
}
192+
c.updateWindow(time.Now())
190193

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

238-
func (c *CloudWatch) initializeCloudWatch() error {
241+
func (c *CloudWatch) initializeCloudWatch() {
239242
credentialConfig := &internalaws.CredentialConfig{
240243
Region: c.Region,
241244
AccessKey: c.AccessKey,
@@ -248,10 +251,27 @@ func (c *CloudWatch) initializeCloudWatch() error {
248251
}
249252
configProvider := credentialConfig.Credentials()
250253

251-
cfg := &aws.Config{}
254+
cfg := &aws.Config{
255+
HTTPClient: &http.Client{
256+
// use values from DefaultTransport
257+
Transport: &http.Transport{
258+
Proxy: http.ProxyFromEnvironment,
259+
DialContext: (&net.Dialer{
260+
Timeout: 30 * time.Second,
261+
KeepAlive: 30 * time.Second,
262+
DualStack: true,
263+
}).DialContext,
264+
MaxIdleConns: 100,
265+
IdleConnTimeout: 90 * time.Second,
266+
TLSHandshakeTimeout: 10 * time.Second,
267+
ExpectContinueTimeout: 1 * time.Second,
268+
},
269+
Timeout: c.Timeout.Duration,
270+
},
271+
}
272+
252273
loglevel := aws.LogOff
253274
c.client = cloudwatch.New(configProvider, cfg.WithLogLevel(loglevel))
254-
return nil
255275
}
256276

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

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

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

384404
c.windowEnd = windowEnd
385-
386-
return nil
387405
}
388406

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

0 commit comments

Comments
 (0)