Skip to content

Commit

Permalink
Add an option to skip create log stream for awslogs driver
Browse files Browse the repository at this point in the history
Added an option `awslogs-create-stream` to allow skipping log stream
creation for awslogs log driver. The default value is still true to
keep the behavior be consistent with before.

Signed-off-by: Xia Wu <xwumzn@amazon.com>
  • Loading branch information
xia-wu committed Mar 9, 2021
1 parent 8b6d9ea commit d10046f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
22 changes: 22 additions & 0 deletions daemon/logger/awslogs/cloudwatchlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
logGroupKey = "awslogs-group"
logStreamKey = "awslogs-stream"
logCreateGroupKey = "awslogs-create-group"
logCreateStreamKey = "awslogs-create-stream"
tagKey = "tag"
datetimeFormatKey = "awslogs-datetime-format"
multilinePatternKey = "awslogs-multiline-pattern"
Expand Down Expand Up @@ -71,6 +72,7 @@ type logStream struct {
logStreamName string
logGroupName string
logCreateGroup bool
logCreateStream bool
logNonBlocking bool
forceFlushInterval time.Duration
multilinePattern *regexp.Regexp
Expand All @@ -85,6 +87,7 @@ type logStreamConfig struct {
logStreamName string
logGroupName string
logCreateGroup bool
logCreateStream bool
logNonBlocking bool
forceFlushInterval time.Duration
maxBufferedEvents int
Expand Down Expand Up @@ -151,6 +154,7 @@ func New(info logger.Info) (logger.Logger, error) {
logStreamName: containerStreamConfig.logStreamName,
logGroupName: containerStreamConfig.logGroupName,
logCreateGroup: containerStreamConfig.logCreateGroup,
logCreateStream: containerStreamConfig.logCreateStream,
logNonBlocking: containerStreamConfig.logNonBlocking,
forceFlushInterval: containerStreamConfig.forceFlushInterval,
multilinePattern: containerStreamConfig.multilinePattern,
Expand Down Expand Up @@ -237,6 +241,13 @@ func newStreamConfig(info logger.Info) (*logStreamConfig, error) {
if info.Config[logStreamKey] != "" {
logStreamName = info.Config[logStreamKey]
}
logCreateStream := true
if info.Config[logCreateStreamKey] != "" {
logCreateStream, err = strconv.ParseBool(info.Config[logCreateStreamKey])
if err != nil {
return nil, err
}
}

multilinePattern, err := parseMultilineOptions(info)
if err != nil {
Expand All @@ -247,6 +258,7 @@ func newStreamConfig(info logger.Info) (*logStreamConfig, error) {
logStreamName: logStreamName,
logGroupName: logGroupName,
logCreateGroup: logCreateGroup,
logCreateStream: logCreateStream,
logNonBlocking: logNonBlocking,
forceFlushInterval: forceFlushInterval,
maxBufferedEvents: maxBufferedEvents,
Expand Down Expand Up @@ -480,6 +492,16 @@ func (l *logStream) createLogGroup() error {

// createLogStream creates a log stream for the instance of the awslogs logging driver
func (l *logStream) createLogStream() error {
// Directly return if we do not want to create log stream.
if !l.logCreateStream {
logrus.WithFields(logrus.Fields{
"logGroupName": l.logGroupName,
"logStreamName": l.logStreamName,
"logCreateStream": l.logCreateStream,
}).Info("Skipping creating log stream")
return nil
}

input := &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(l.logGroupName),
LogStreamName: aws.String(l.logStreamName),
Expand Down
59 changes: 40 additions & 19 deletions daemon/logger/awslogs/cloudwatchlogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestNewStreamConfig(t *testing.T) {
logStreamName string
logGroupName string
logCreateGroup string
logCreateStream string
logNonBlocking string
forceFlushInterval string
maxBufferedEvents string
Expand All @@ -73,13 +74,13 @@ func TestNewStreamConfig(t *testing.T) {
shouldErr bool
testName string
}{
{"", groupName, "", "", "", "", "", "", false, "defaults"},
{"", groupName, "invalid create group", "", "", "", "", "", true, "invalid create group"},
{"", groupName, "", "", "invalid flush interval", "", "", "", true, "invalid flush interval"},
{"", groupName, "", "", "", "invalid max buffered events", "", "", true, "invalid max buffered events"},
{"", groupName, "", "", "", "", "", "n{1001}", true, "invalid multiline pattern"},
{"", groupName, "", "", "15", "", "", "", false, "flush interval at 15"},
{"", groupName, "", "", "", "1024", "", "", false, "max buffered events at 1024"},
{"", groupName, "", "", "", "", "", "", "", false, "defaults"},
{"", groupName, "invalid create group", "", "", "", "", "", "", true, "invalid create group"},
{"", groupName, "", "", "", "invalid flush interval", "", "", "", true, "invalid flush interval"},
{"", groupName, "", "", "", "", "invalid max buffered events", "", "", true, "invalid max buffered events"},
{"", groupName, "", "", "", "", "", "", "n{1001}", true, "invalid multiline pattern"},
{"", groupName, "", "", "", "15", "", "", "", false, "flush interval at 15"},
{"", groupName, "", "", "", "", "1024", "", "", false, "max buffered events at 1024"},
}

for _, tc := range tests {
Expand All @@ -91,6 +92,7 @@ func TestNewStreamConfig(t *testing.T) {
forceFlushIntervalKey: tc.forceFlushInterval,
maxBufferedEventsKey: tc.maxBufferedEvents,
logStreamKey: tc.logStreamName,
logCreateStreamKey: tc.logCreateStream,
datetimeFormatKey: tc.datetimeFormat,
multilinePatternKey: tc.multilinePattern,
}
Expand Down Expand Up @@ -186,9 +188,10 @@ func TestNewAWSLogsClientRegionDetect(t *testing.T) {
func TestCreateSuccess(t *testing.T) {
mockClient := newMockClient()
stream := &logStream{
client: mockClient,
logGroupName: groupName,
logStreamName: streamName,
client: mockClient,
logGroupName: groupName,
logStreamName: streamName,
logCreateStream: true,
}
mockClient.createLogStreamResult <- &createLogStreamResult{}

Expand All @@ -212,13 +215,28 @@ func TestCreateSuccess(t *testing.T) {
}
}

func TestCreateStreamSkipped(t *testing.T) {
stream := &logStream{
logGroupName: groupName,
logStreamName: streamName,
logCreateStream: false,
}

err := stream.create()

if err != nil {
t.Errorf("Received unexpected err: %v\n", err)
}
}

func TestCreateLogGroupSuccess(t *testing.T) {
mockClient := newMockClient()
stream := &logStream{
client: mockClient,
logGroupName: groupName,
logStreamName: streamName,
logCreateGroup: true,
client: mockClient,
logGroupName: groupName,
logStreamName: streamName,
logCreateGroup: true,
logCreateStream: true,
}
mockClient.createLogGroupResult <- &createLogGroupResult{}
mockClient.createLogStreamResult <- &createLogStreamResult{}
Expand Down Expand Up @@ -246,7 +264,8 @@ func TestCreateLogGroupSuccess(t *testing.T) {
func TestCreateError(t *testing.T) {
mockClient := newMockClient()
stream := &logStream{
client: mockClient,
client: mockClient,
logCreateStream: true,
}
mockClient.createLogStreamResult <- &createLogStreamResult{
errorResult: errors.New("Error"),
Expand All @@ -262,7 +281,8 @@ func TestCreateError(t *testing.T) {
func TestCreateAlreadyExists(t *testing.T) {
mockClient := newMockClient()
stream := &logStream{
client: mockClient,
client: mockClient,
logCreateStream: true,
}
mockClient.createLogStreamResult <- &createLogStreamResult{
errorResult: awserr.New(resourceAlreadyExistsCode, "", nil),
Expand Down Expand Up @@ -1552,9 +1572,10 @@ func TestCreateTagSuccess(t *testing.T) {
t.Errorf("Error generating tag: %q", e)
}
stream := &logStream{
client: mockClient,
logGroupName: groupName,
logStreamName: logStreamName,
client: mockClient,
logGroupName: groupName,
logStreamName: logStreamName,
logCreateStream: true,
}
mockClient.createLogStreamResult <- &createLogStreamResult{}

Expand Down

0 comments on commit d10046f

Please sign in to comment.