Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/pkg/cli/svc_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ func (o *svcLogsOpts) askSvcEnvName() error {
func parseSince(since time.Duration) *int64 {
sinceSec := int64(since.Round(time.Second).Seconds())
timeNow := time.Now().Add(time.Duration(-sinceSec) * time.Second)
return aws.Int64(timeNow.Unix() * 1000)
return aws.Int64(timeNow.UnixMilli())
}

func parseRFC3339(timeStr string) (int64, error) {
startTimeTmp, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return 0, fmt.Errorf("reading time value %s: %w", timeStr, err)
}
return startTimeTmp.Unix() * 1000, nil
return startTimeTmp.UnixMilli(), nil
}

// buildSvcLogsCmd builds the command for displaying service logs in an application.
Expand Down
23 changes: 21 additions & 2 deletions internal/pkg/logging/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type ServiceClient struct {
logStreamNamePrefix string
eventsGetter logGetter
w io.Writer

now func() time.Time
}

// WriteLogEventsOpts wraps the parameters to call WriteLogEvents.
Expand Down Expand Up @@ -65,14 +67,29 @@ func (o WriteLogEventsOpts) limit() *int64 {
if o.Limit != nil {
return o.Limit
}
if o.StartTime != nil || o.EndTime != nil {
if o.hasTimeFilters() {
// If time filtering is set, then set limit to be maximum number.
// https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_GetLogEvents.html#CWL-GetLogEvents-request-limit
return nil
}
return aws.Int64(defaultServiceLogsLimit)
}

func (o WriteLogEventsOpts) startTime(now func() time.Time) *int64 {
if o.StartTime != nil {
return o.StartTime
}
if o.Follow {
// Start following log events from current timestamp.
return aws.Int64(now().UnixMilli())
}
return nil
}

func (o WriteLogEventsOpts) hasTimeFilters() bool {
return o.Follow || o.StartTime != nil || o.EndTime != nil
}

// NewServiceClient returns a ServiceClient for the svc service under env and app.
// The logging client is initialized from the given sess session.
func NewServiceClient(opts *NewServiceLogsConfig) (*ServiceClient, error) {
Expand All @@ -88,6 +105,7 @@ func NewServiceClient(opts *NewServiceLogsConfig) (*ServiceClient, error) {
logStreamNamePrefix: fmt.Sprintf(fmtSvcLogStreamPrefix, opts.Svc),
eventsGetter: cloudwatchlogs.New(opts.Sess),
w: log.OutputWriter,
now: time.Now,
}, nil
}

Expand Down Expand Up @@ -126,6 +144,7 @@ func newAppRunnerServiceClient(opts *NewServiceLogsConfig) (*ServiceClient, erro
logGroupName: logGroup,
eventsGetter: cloudwatchlogs.New(opts.Sess),
w: log.OutputWriter,
now: time.Now,
}, nil
}

Expand All @@ -135,7 +154,7 @@ func (s *ServiceClient) WriteLogEvents(opts WriteLogEventsOpts) error {
LogGroup: s.logGroupName,
Limit: opts.limit(),
EndTime: opts.EndTime,
StartTime: opts.StartTime,
StartTime: opts.startTime(s.now),
}
if opts.TaskIDs != nil {
logEventsOpts.LogStreams = s.logStreams(opts.TaskIDs)
Expand Down
30 changes: 28 additions & 2 deletions internal/pkg/logging/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/copilot-cli/internal/pkg/aws/cloudwatchlogs"
Expand Down Expand Up @@ -54,9 +55,9 @@ firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "WARN some warnin
},
}
mockLimit := aws.Int64(100)
mockDefaultLimit := aws.Int64(10)
var mockNilLimit *int64
mockStartTime := aws.Int64(123456789)
mockCurrentTimestamp := time.Date(2020, 11, 23, 0, 0, 0, 0, time.UTC) // Copilot GA date :).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😻

testCases := map[string]struct {
follow bool
limit *int64
Expand Down Expand Up @@ -118,7 +119,9 @@ firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "WARN some warnin
m.logGetter.EXPECT().LogEvents(gomock.Any()).
Do(func(param cloudwatchlogs.LogEventsOpts) {
require.Equal(t, param.LogStreams, []string{"mockLogStreamPrefix/mockTaskID1", "mockLogStreamPrefix/mockTaskID2"})
require.Equal(t, param.Limit, mockDefaultLimit)
var val *int64 = nil // Explicitly mark that nil is of type *int64 otherwise require.Equal returns an error.
require.Equal(t, param.Limit, val)
require.Equal(t, param.StartTime, aws.Int64(mockCurrentTimestamp.UnixMilli()))
}).
Return(&cloudwatchlogs.LogEventsOutput{
Events: logEvents,
Expand All @@ -136,6 +139,26 @@ firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "WARN some warnin
firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "FATA some error" - -
firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "WARN some warning" - -
firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "GET / HTTP/1.1" 404 -
`,
},
"success with no filtering": {
taskIDs: []string{"mockTaskID1"},
setupMocks: func(m serviceLogsMocks) {
gomock.InOrder(
m.logGetter.EXPECT().LogEvents(gomock.Any()).
Do(func(param cloudwatchlogs.LogEventsOpts) {
require.Equal(t, param.LogStreams, []string{"mockLogStreamPrefix/mockTaskID1"})
require.Equal(t, param.Limit, aws.Int64(10))
}).
Return(&cloudwatchlogs.LogEventsOutput{
Events: logEvents,
}, nil),
)
},

wantedContent: `firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "GET / HTTP/1.1" 200 -
firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "FATA some error" - -
firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "WARN some warning" - -
`,
},
}
Expand All @@ -159,6 +182,9 @@ firelens_log_router/fcfe4 10.0.0.00 - - [01/Jan/1970 01:01:01] "GET / HTTP/1.1"
logStreamNamePrefix: mockLogStreamPrefix,
eventsGetter: mocklogGetter,
w: b,
now: func() time.Time {
return mockCurrentTimestamp
},
}

// WHEN
Expand Down