@@ -3,6 +3,8 @@ package cloudwatch
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "net"
7
+ "net/http"
6
8
"strconv"
7
9
"strings"
8
10
"sync"
@@ -23,16 +25,17 @@ import (
23
25
type (
24
26
// CloudWatch contains the configuration and cache for the cloudwatch plugin.
25
27
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"`
36
39
37
40
Period internal.Duration `toml:"period"`
38
41
Delay internal.Duration `toml:"delay"`
@@ -133,6 +136,9 @@ func (c *CloudWatch) SampleConfig() string {
133
136
## See http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
134
137
# ratelimit = 25
135
138
139
+ ## Timeout for http requests made by the cloudwatch client.
140
+ # timeout = "5s"
141
+
136
142
## Namespace-wide statistic filters. These allow fewer queries to be made to
137
143
## cloudwatch.
138
144
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]
@@ -183,10 +189,7 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
183
189
return err
184
190
}
185
191
186
- err = c .updateWindow (time .Now ())
187
- if err != nil {
188
- return err
189
- }
192
+ c .updateWindow (time .Now ())
190
193
191
194
// Get all of the possible queries so we can send groups of 100.
192
195
queries , err := c .getDataQueries (filteredMetrics )
@@ -235,7 +238,7 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
235
238
return c .aggregateMetrics (acc , results )
236
239
}
237
240
238
- func (c * CloudWatch ) initializeCloudWatch () error {
241
+ func (c * CloudWatch ) initializeCloudWatch () {
239
242
credentialConfig := & internalaws.CredentialConfig {
240
243
Region : c .Region ,
241
244
AccessKey : c .AccessKey ,
@@ -248,10 +251,27 @@ func (c *CloudWatch) initializeCloudWatch() error {
248
251
}
249
252
configProvider := credentialConfig .Credentials ()
250
253
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
+
252
273
loglevel := aws .LogOff
253
274
c .client = cloudwatch .New (configProvider , cfg .WithLogLevel (loglevel ))
254
- return nil
255
275
}
256
276
257
277
type filteredMetric struct {
@@ -370,7 +390,7 @@ func (c *CloudWatch) fetchNamespaceMetrics() ([]*cloudwatch.Metric, error) {
370
390
return metrics , nil
371
391
}
372
392
373
- func (c * CloudWatch ) updateWindow (relativeTo time.Time ) error {
393
+ func (c * CloudWatch ) updateWindow (relativeTo time.Time ) {
374
394
windowEnd := relativeTo .Add (- c .Delay .Duration )
375
395
376
396
if c .windowEnd .IsZero () {
@@ -382,8 +402,6 @@ func (c *CloudWatch) updateWindow(relativeTo time.Time) error {
382
402
}
383
403
384
404
c .windowEnd = windowEnd
385
-
386
- return nil
387
405
}
388
406
389
407
// getDataQueries gets all of the possible queries so we can maximize the request payload.
@@ -535,6 +553,7 @@ func init() {
535
553
return & CloudWatch {
536
554
CacheTTL : internal.Duration {Duration : time .Hour },
537
555
RateLimit : 25 ,
556
+ Timeout : internal.Duration {Duration : time .Second * 5 },
538
557
}
539
558
})
540
559
}
0 commit comments