Skip to content

Commit 4c8829d

Browse files
authored
stagger FilterLogEvents API calls to prevent throttling exceptions (#2597)
1 parent f0d501e commit 4c8829d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

.changeset/eighty-items-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/sandbox': patch
3+
---
4+
5+
stagger FilterLogEvents API calls to prevent throttling exceptions

packages/sandbox/src/cloudwatch_logs_monitor.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void describe('LambdaFunctionLogStreamer', () => {
5555
classUnderTest.addLogGroups('logFriendlyName2', 'logGroupName2');
5656
classUnderTest.activate();
5757
// wait just a bit to let the logs streamer run before deactivating it
58-
await new Promise((resolve) => setTimeout(resolve, 100));
58+
await new Promise((resolve) => setTimeout(resolve, 1100));
5959
classUnderTest.pause();
6060

6161
assert.strictEqual(mockedWrite.mock.callCount(), 4);
@@ -137,8 +137,9 @@ void describe('LambdaFunctionLogStreamer', () => {
137137

138138
classUnderTest.addLogGroups('logFriendlyName1', 'logGroupName1');
139139
classUnderTest.activate();
140-
// wait for just over two seconds to let the logs streamer get both the events before deactivating it
141-
await new Promise((resolve) => setTimeout(resolve, 2100));
140+
// wait for just 1 second to make the API calls and just over two seconds
141+
// to let the logs streamer get both the events before deactivating it
142+
await new Promise((resolve) => setTimeout(resolve, 3100));
142143
classUnderTest.pause();
143144

144145
assert.strictEqual(mockedWrite.mock.callCount(), 2);
@@ -204,7 +205,7 @@ void describe('LambdaFunctionLogStreamer', () => {
204205
classUnderTest.addLogGroups('logFriendlyName1', 'logGroupName1');
205206
classUnderTest.activate();
206207
// wait just a bit to let the logs streamer run before deactivating it
207-
await new Promise((resolve) => setTimeout(resolve, 100));
208+
await new Promise((resolve) => setTimeout(resolve, 1100));
208209
classUnderTest.pause();
209210

210211
// 100 events + 1 informational for the user that 100 messages limit is hit
@@ -258,7 +259,7 @@ void describe('LambdaFunctionLogStreamer', () => {
258259
classUnderTest.addLogGroups('logFriendlyName1', 'logGroupName1');
259260
// activate it again and it should fetch the second event now
260261
classUnderTest.activate();
261-
await new Promise((resolve) => setTimeout(resolve, 100));
262+
await new Promise((resolve) => setTimeout(resolve, 600));
262263
classUnderTest.pause();
263264

264265
assert.strictEqual(mockedWrite.mock.callCount(), 2);

packages/sandbox/src/cloudwatch_logs_monitor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ export class CloudWatchLogEventMonitor {
192192
* Reads all new log events from a set of CloudWatch Log Groups in parallel
193193
*/
194194
private readNewEvents = async (): Promise<Array<CloudWatchLogEvent>> => {
195-
const promises: Array<Promise<Array<CloudWatchLogEvent>>> = [];
195+
const results: Array<Array<CloudWatchLogEvent>> = [];
196196
for (const logGroup of this.allLogGroups) {
197-
promises.push(this.readEventsFromLogGroup(logGroup));
197+
results.push(await this.readEventsFromLogGroup(logGroup));
198+
// There is a hard limit on TPS for `FilterLogEvents` API that is Account wide.
199+
// We don't want to get throttled is customers are filtering for a lot of functions.
200+
await new Promise((resolve) => setTimeout(resolve, 500));
198201
}
199-
return (await Promise.all(promises)).flat();
202+
return results.flat();
200203
};
201204

202205
/**

0 commit comments

Comments
 (0)