Skip to content

Commit 196abb7

Browse files
authored
fix(inputs.cloudwatch): customizable batch size when querying (#10851)
1 parent e7e3926 commit 196abb7

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

plugins/inputs/cloudwatch/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ API endpoint. In the following order the plugin will attempt to authenticate.
9191
## Timeout for http requests made by the cloudwatch client.
9292
# timeout = "5s"
9393

94+
## Batch Size
95+
## The size of each batch to send requests to Cloudwatch. 500 is the
96+
## suggested largest size. If a request gets to large (413 errors), consider
97+
## reducing this amount.
98+
# batch_size = 500
99+
94100
## Namespace-wide statistic filters. These allow fewer queries to be made to
95101
## cloudwatch.
96102
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]

plugins/inputs/cloudwatch/cloudwatch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type CloudWatch struct {
5555
CacheTTL config.Duration `toml:"cache_ttl"`
5656
RateLimit int `toml:"ratelimit"`
5757
RecentlyActive string `toml:"recently_active"`
58+
BatchSize int `toml:"batch_size"`
5859

5960
Log telegraf.Logger `toml:"-"`
6061

@@ -146,12 +147,10 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
146147
results := map[string][]types.MetricDataResult{}
147148

148149
for namespace, namespacedQueries := range queries {
149-
// 500 is the maximum number of metric data queries a `GetMetricData` request can contain.
150-
batchSize := 500
151150
var batches [][]types.MetricDataQuery
152151

153-
for batchSize < len(namespacedQueries) {
154-
namespacedQueries, batches = namespacedQueries[batchSize:], append(batches, namespacedQueries[0:batchSize:batchSize])
152+
for c.BatchSize < len(namespacedQueries) {
153+
namespacedQueries, batches = namespacedQueries[c.BatchSize:], append(batches, namespacedQueries[0:c.BatchSize:c.BatchSize])
155154
}
156155
batches = append(batches, namespacedQueries)
157156

@@ -530,6 +529,7 @@ func New() *CloudWatch {
530529
CacheTTL: config.Duration(time.Hour),
531530
RateLimit: 25,
532531
Timeout: config.Duration(time.Second * 5),
532+
BatchSize: 500,
533533
}
534534
}
535535

plugins/inputs/cloudwatch/cloudwatch_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func TestGather(t *testing.T) {
106106
Delay: internalDuration,
107107
Period: internalDuration,
108108
RateLimit: 200,
109+
BatchSize: 500,
109110
}
110111

111112
var acc testutil.Accumulator
@@ -137,6 +138,7 @@ func TestGather_MultipleNamespaces(t *testing.T) {
137138
Delay: internalDuration,
138139
Period: internalDuration,
139140
RateLimit: 200,
141+
BatchSize: 500,
140142
}
141143

142144
var acc testutil.Accumulator
@@ -213,6 +215,7 @@ func TestSelectMetrics(t *testing.T) {
213215
Delay: internalDuration,
214216
Period: internalDuration,
215217
RateLimit: 200,
218+
BatchSize: 500,
216219
Metrics: []*Metric{
217220
{
218221
MetricNames: []string{"Latency", "RequestCount"},
@@ -258,6 +261,7 @@ func TestGenerateStatisticsInputParams(t *testing.T) {
258261
Namespaces: []string{namespace},
259262
Delay: internalDuration,
260263
Period: internalDuration,
264+
BatchSize: 500,
261265
}
262266

263267
require.NoError(t, c.initializeCloudWatch())
@@ -297,6 +301,7 @@ func TestGenerateStatisticsInputParamsFiltered(t *testing.T) {
297301
Namespaces: []string{namespace},
298302
Delay: internalDuration,
299303
Period: internalDuration,
304+
BatchSize: 500,
300305
}
301306

302307
require.NoError(t, c.initializeCloudWatch())
@@ -336,6 +341,7 @@ func TestUpdateWindow(t *testing.T) {
336341
Namespace: "AWS/ELB",
337342
Delay: internalDuration,
338343
Period: internalDuration,
344+
BatchSize: 500,
339345
}
340346

341347
now := time.Now()
@@ -364,6 +370,7 @@ func TestProxyFunction(t *testing.T) {
364370
HTTPProxy: proxy.HTTPProxy{
365371
HTTPProxyURL: "http://www.penguins.com",
366372
},
373+
BatchSize: 500,
367374
}
368375

369376
proxyFunction, err := c.HTTPProxy.Proxy()
@@ -378,7 +385,11 @@ func TestProxyFunction(t *testing.T) {
378385
}
379386

380387
func TestCombineNamespaces(t *testing.T) {
381-
c := &CloudWatch{Namespace: "AWS/ELB", Namespaces: []string{"AWS/EC2", "AWS/Billing"}}
388+
c := &CloudWatch{
389+
Namespace: "AWS/ELB",
390+
Namespaces: []string{"AWS/EC2", "AWS/Billing"},
391+
BatchSize: 500,
392+
}
382393

383394
require.NoError(t, c.Init())
384395
require.Equal(t, []string{"AWS/EC2", "AWS/Billing", "AWS/ELB"}, c.Namespaces)

plugins/inputs/cloudwatch/sample.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
## Timeout for http requests made by the cloudwatch client.
7373
# timeout = "5s"
7474

75+
## Batch Size
76+
## The size of each batch to send requests to Cloudwatch. 500 is the
77+
## suggested largest size. If a request gets to large (413 errors), consider
78+
## reducing this amount.
79+
# batch_size = 500
80+
7581
## Namespace-wide statistic filters. These allow fewer queries to be made to
7682
## cloudwatch.
7783
# statistic_include = [ "average", "sum", "minimum", "maximum", sample_count" ]

0 commit comments

Comments
 (0)