Skip to content

Commit

Permalink
[awscloudwatchreceiver] add some more logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeancal committed Oct 15, 2023
1 parent 996e3af commit b7e0458
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
29 changes: 10 additions & 19 deletions receiver/awscloudwatchreceiver/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
)

const (
noStreamName = "THIS IS INVALID STREAM"
noStreamName = "THIS IS INVALID STREAM"
maxLogGroupsPerDiscovery = int64(50)
)

type logsReceiver struct {
Expand All @@ -41,8 +42,6 @@ type logsReceiver struct {
doneChan chan bool
}

const maxLogGroupsPerDiscovery = int64(50)

type client interface {
DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, opts ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error)
FilterLogEventsWithContext(ctx context.Context, input *cloudwatchlogs.FilterLogEventsInput, opts ...request.Option) (*cloudwatchlogs.FilterLogEventsOutput, error)
Expand All @@ -62,6 +61,7 @@ func (sn *streamNames) request(limit int, nextToken string, st, et *time.Time) *
EndTime: aws.Int64(et.UnixMilli()),
Limit: aws.Int64(int64(limit)),
}

if sn.arn != "" {
base.LogGroupIdentifier = aws.String(sn.arn)
} else {
Expand Down Expand Up @@ -350,28 +350,16 @@ func (l *logsReceiver) discoverGroups(ctx context.Context, auto *AutodiscoverCon
l.logger.Debug("discovered log group", zap.String("log group", lg.GoString()))
// default behavior is to collect all if not stream filtered
if len(auto.Streams.Names) == 0 && len(auto.Streams.Prefixes) == 0 {
if lg.Arn != nil && *lg.Arn != "" {
groups = append(groups, &streamNames{group: *lg.LogGroupName, arn: transformARN(*lg.Arn), storedBytes: *lg.StoredBytes})
} else {
groups = append(groups, &streamNames{group: *lg.LogGroupName})
}
groups = append(groups, &streamNames{group: *lg.LogGroupName, arn: transformARN(*lg.Arn), storedBytes: *lg.StoredBytes})
continue
}

for _, prefix := range auto.Streams.Prefixes {
if lg.Arn != nil && *lg.Arn != "" {
groups = append(groups, &streamPrefix{group: *lg.LogGroupName, arn: *lg.Arn, prefix: prefix, storedBytes: *lg.StoredBytes})
} else {
groups = append(groups, &streamPrefix{group: *lg.LogGroupName, prefix: prefix})
}
groups = append(groups, &streamPrefix{group: *lg.LogGroupName, arn: transformARN(*lg.Arn), prefix: prefix, storedBytes: *lg.StoredBytes})
}

if len(auto.Streams.Names) > 0 {
if lg.Arn != nil && *lg.Arn != "" {
groups = append(groups, &streamNames{group: *lg.LogGroupName, arn: *lg.Arn, names: auto.Streams.Names, storedBytes: *lg.StoredBytes})
} else {
groups = append(groups, &streamNames{group: *lg.LogGroupName, names: auto.Streams.Names})
}
groups = append(groups, &streamNames{group: *lg.LogGroupName, arn: transformARN(*lg.Arn), names: auto.Streams.Names, storedBytes: *lg.StoredBytes})
}
}
}
Expand All @@ -381,7 +369,10 @@ func (l *logsReceiver) discoverGroups(ctx context.Context, auto *AutodiscoverCon
}

func transformARN(arn string) string {
return arn[:len(arn)-2]
if arn != "" {
return arn[:len(arn)-2]
}
return ""
}

func (l *logsReceiver) ensureSession() error {
Expand Down
3 changes: 3 additions & 0 deletions receiver/awscloudwatchreceiver/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestAutodiscoverLimit(t *testing.T) {
for i := 0; i <= 100; i++ {
logGroups = append(logGroups, &cloudwatchlogs.LogGroup{
LogGroupName: aws.String(fmt.Sprintf("test log group: %d", i)),
Arn: &testLogArn,
StoredBytes: aws.Int64(storedBytes),
})
}
Expand Down Expand Up @@ -220,6 +221,7 @@ func defaultMockClient() client {
LogGroups: []*cloudwatchlogs.LogGroup{
{
LogGroupName: &testLogGroupName,
Arn: &testLogArn,
StoredBytes: &storedBytes,
},
},
Expand Down Expand Up @@ -264,6 +266,7 @@ func defaultMockClient() client {

var (
testLogGroupName = "test-log-group-name"
testLogArn = "arn:aws:iam::123456789:role/monitoring-EKS-NodeInstanceRole/*"
testLogStreamName = "test-log-stream-name"
testLogStreamName2 = "test-log-stream-name-2"
testLogStreamPrefix = "test-log-stream"
Expand Down

0 comments on commit b7e0458

Please sign in to comment.