Skip to content

Commit

Permalink
Fix app logs rate limit issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nightfury1204 committed Jul 4, 2024
1 parent 36a2921 commit d9ee34d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 12 additions & 2 deletions pkg/helpers/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,10 @@ func CloudWatchLogsStream(ctx context.Context, cw cloudwatchlogsiface.CloudWatch
var start int64

if opts.Since != nil {
start = time.Now().UTC().Add((*opts.Since)*-1).Unix() * int64(time.Microsecond)
start = time.Now().UTC().Add((*opts.Since) * -1).UnixMilli()
req.StartTime = aws.Int64(start)
} else {
req.StartTime = aws.Int64(time.Now().UTC().Add(-1 * time.Hour).UnixMilli())
}

if stream != "" {
Expand All @@ -390,13 +392,21 @@ func CloudWatchLogsStream(ctx context.Context, cw cloudwatchlogsiface.CloudWatch
if err != nil {
switch AwsErrorCode(err) {
case "ThrottlingException", "ResourceNotFoundException":
time.Sleep(1 * time.Second)
time.Sleep(3 * time.Second)
continue
default:
return err
}
}

if res != nil {
if res.NextToken != nil {
time.Sleep(2 * time.Second)
} else if len(res.Events) == 0 {
time.Sleep(5 * time.Second)
}
}

es := []*cloudwatchlogs.FilteredLogEvent{}

for _, e := range res.Events {
Expand Down
7 changes: 5 additions & 2 deletions provider/aws/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestAppLogs(t *testing.T) {
Follow: options.Bool(false),
Filter: options.String("test"),
Prefix: options.Bool(true),
Since: options.Duration(time.Since(time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC))),
})

io.Copy(buf, r)
Expand Down Expand Up @@ -403,7 +404,8 @@ var cycleLogFilterLogEvents1 = awsutil.Cycle{
Body: `{
"filterPattern": "test",
"interleaved": true,
"logGroupName": "convox-httpd-LogGroup-L4V203L35WRM"
"logGroupName": "convox-httpd-LogGroup-L4V203L35WRM",
"startTime": 1293840000000
}`,
},
Response: awsutil.Response{
Expand Down Expand Up @@ -459,7 +461,8 @@ var cycleLogFilterLogEvents2 = awsutil.Cycle{
"filterPattern": "test",
"interleaved": true,
"logGroupName": "convox-httpd-LogGroup-L4V203L35WRM",
"nextToken": "ZNUEPl7FcQuXbIH4Swk9D9eFu2XBg-ijZIZlvzz4ea9zZRjw-MMtQtvcoMdmq4T29K7Q6Y1e_KvyfpcT_f_tUw"
"nextToken": "ZNUEPl7FcQuXbIH4Swk9D9eFu2XBg-ijZIZlvzz4ea9zZRjw-MMtQtvcoMdmq4T29K7Q6Y1e_KvyfpcT_f_tUw",
"startTime": 1293840000000
}`,
},
Response: awsutil.Response{
Expand Down

0 comments on commit d9ee34d

Please sign in to comment.