Skip to content

Commit

Permalink
fix: updates how the logs are shown from cloudwatch (aws#142)
Browse files Browse the repository at this point in the history
fix: updates how the logs are shown from cloudwatch
  • Loading branch information
elliot-smith authored and tneely committed Nov 11, 2021
1 parent 66670b7 commit 6c00439
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
19 changes: 16 additions & 3 deletions packages/cli/internal/pkg/aws/cwl/get_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,22 @@ func (c Client) GetLogsPaginated(input GetLogsInput) LogPaginator {
}

func formatEvents(events []types.FilteredLogEvent) []string {
logs := make([]string, len(events))
for i, event := range events {
logs[i] = formatEvent(event)
logsByStream := make(map[string][]*types.FilteredLogEvent)
for index := range events {
event := events[index]
logsByStream[*event.LogStreamName] = append(logsByStream[*event.LogStreamName], &event)
}

return convertStreamLogsToLogs(logsByStream, len(events))
}

func convertStreamLogsToLogs(logsByStream map[string][]*types.FilteredLogEvent, eventSize int) []string {
logs, index := make([]string, eventSize), 0
for _, eventList := range logsByStream {
for _, event := range eventList {
logs[index] = formatEvent(*event)
index++
}
}
return logs
}
Expand Down
21 changes: 20 additions & 1 deletion packages/cli/internal/pkg/aws/cwl/get_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,27 @@ func TestClient_GetLogs(t *testing.T) {
Message: aws.String("Hello"),
Timestamp: aws.Int64(eventTime1.UnixNano() / 1000000),
},
{
EventId: aws.String("some-id-2"),
IngestionTime: aws.Int64(eventTime1.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-2"),
Message: aws.String("Hola"),
Timestamp: aws.Int64(eventTime1.UnixNano() / 1000000),
},
{
EventId: aws.String("some-other-id"),
IngestionTime: aws.Int64(eventTime2.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-1"),
Message: aws.String("world!"),
Timestamp: aws.Int64(eventTime2.UnixNano() / 1000000),
},
{
EventId: aws.String("some-other-id-2"),
IngestionTime: aws.Int64(eventTime2.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-2"),
Message: aws.String("mundo!"),
Timestamp: aws.Int64(eventTime2.UnixNano() / 1000000),
},
}}, nil)
output := client.GetLogsPaginated(GetLogsInput{
LogGroupName: testLogGroupName,
Expand All @@ -65,7 +79,12 @@ func TestClient_GetLogs(t *testing.T) {
logs, err := output.NextLogs()
assert.False(t, output.HasMoreLogs())
assert.NoError(t, err)
assert.Equal(t, []string{fmt.Sprintf("%s\tHello", eventTime1.Format(time.RFC1123Z)), fmt.Sprintf("%s\tworld!", eventTime2.Format(time.RFC1123Z))}, logs)
assert.Equal(t, []string{
fmt.Sprintf("%s\tHello", eventTime1.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tworld!", eventTime2.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tHola", eventTime1.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tmundo!", eventTime2.Format(time.RFC1123Z)),
}, logs)
}

func TestClient_GetLogs_Error(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/internal/pkg/aws/cwl/stream_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ func (c Client) StreamLogs(ctx context.Context, logGroupName string, streams ...
}

func parseEventLogs(events []types.FilteredLogEvent, latestTimestamp *int64) []string {
logs := make([]string, len(events))
for i, event := range events {
logsByStream := make(map[string][]*types.FilteredLogEvent)
for index := range events {
event := events[index]
if aws.ToInt64(event.Timestamp) > aws.ToInt64(latestTimestamp) {
latestTimestamp = event.Timestamp
}
logs[i] = formatEvent(event)
logsByStream[*event.LogStreamName] = append(logsByStream[*event.LogStreamName], &event)
}
return logs

return convertStreamLogsToLogs(logsByStream, len(events))
}
39 changes: 33 additions & 6 deletions packages/cli/internal/pkg/aws/cwl/stream_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,52 @@ import (
)

func TestClient_StreamLogs(t *testing.T) {
someTime := time.Unix(0, 773391600000000000)
someTime1 := time.Unix(0, 773391600000000000)
someTime2 := time.Unix(0, 773391600000000000)
ctx, cancel := context.WithCancel(context.Background())
client := NewMockClient()
client.cwl.(*CwlMock).On("FilterLogEvents", ctx, mock.Anything).
Return(&cloudwatchlogs.FilterLogEventsOutput{
NextToken: aws.String("Token"),
Events: []types.FilteredLogEvent{
{
EventId: aws.String("ID"),
IngestionTime: aws.Int64(someTime.UnixNano() / 1000000),
EventId: aws.String("some-id"),
IngestionTime: aws.Int64(someTime1.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-1"),
Message: aws.String("Hello!"),
Timestamp: aws.Int64(someTime.UnixNano() / 1000000),
Message: aws.String("Hello"),
Timestamp: aws.Int64(someTime1.UnixNano() / 1000000),
},
{
EventId: aws.String("some-id-2"),
IngestionTime: aws.Int64(someTime1.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-2"),
Message: aws.String("Hola"),
Timestamp: aws.Int64(someTime1.UnixNano() / 1000000),
},
{
EventId: aws.String("some-other-id"),
IngestionTime: aws.Int64(someTime2.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-1"),
Message: aws.String("world!"),
Timestamp: aws.Int64(someTime2.UnixNano() / 1000000),
},
{
EventId: aws.String("some-other-id-2"),
IngestionTime: aws.Int64(someTime2.UnixNano() / 1000000),
LogStreamName: aws.String("log-stream-2"),
Message: aws.String("mundo!"),
Timestamp: aws.Int64(someTime2.UnixNano() / 1000000),
},
}}, nil)
cancel()
stream := client.StreamLogs(ctx, testLogGroupName)
event := <-stream
assert.Equal(t, []string{fmt.Sprintf("%s\tHello!", someTime.Format(time.RFC1123Z))}, event.Logs)
assert.Equal(t, []string{
fmt.Sprintf("%s\tHello", someTime1.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tworld!", someTime2.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tHola", someTime1.Format(time.RFC1123Z)),
fmt.Sprintf("%s\tmundo!", someTime2.Format(time.RFC1123Z)),
}, event.Logs)
assert.NoError(t, event.Err)
cancel()
_, isOpen := <-stream
Expand Down

0 comments on commit 6c00439

Please sign in to comment.