Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not send AccountID as KeyAttribute for entities in PutLogEvents requests #1402

Merged
merged 2 commits into from
Oct 30, 2024
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
5 changes: 1 addition & 4 deletions extension/entitystore/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ func (e *EntityStore) CreateLogFileEntity(logFileGlob LogFileGlob, logGroupName
keyAttributes := e.createServiceKeyAttributes(serviceAttr)
attributeMap := e.createAttributeMap()
addNonEmptyToMap(attributeMap, ServiceNameSourceKey, serviceAttr.ServiceNameSource)
if _, ok := keyAttributes[entityattributes.AwsAccountId]; !ok {
return nil
}

return &cloudwatchlogs.Entity{
KeyAttributes: keyAttributes,
Attributes: attributeMap,
Expand Down Expand Up @@ -226,7 +224,6 @@ func (e *EntityStore) createServiceKeyAttributes(serviceAttr ServiceAttribute) m
}
addNonEmptyToMap(serviceKeyAttr, entityattributes.ServiceName, serviceAttr.ServiceName)
addNonEmptyToMap(serviceKeyAttr, entityattributes.DeploymentEnvironment, serviceAttr.Environment)
addNonEmptyToMap(serviceKeyAttr, entityattributes.AwsAccountId, e.ec2Info.GetAccountID())
return serviceKeyAttr
}

Expand Down
4 changes: 1 addition & 3 deletions extension/entitystore/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ func TestEntityStore_createServiceKeyAttributes(t *testing.T) {

func TestEntityStore_createLogFileRID(t *testing.T) {
instanceId := "i-abcd1234"
accountId := "123456789012"
glob := LogFileGlob("glob")
group := LogGroupName("group")
serviceAttr := ServiceAttribute{
Expand All @@ -322,7 +321,7 @@ func TestEntityStore_createLogFileRID(t *testing.T) {
sp.On("logFileServiceAttribute", glob, group).Return(serviceAttr)
e := EntityStore{
mode: config.ModeEC2,
ec2Info: EC2Info{InstanceID: instanceId, AccountID: accountId},
ec2Info: EC2Info{InstanceID: instanceId},
serviceprovider: sp,
nativeCredential: &session.Session{},
}
Expand All @@ -334,7 +333,6 @@ func TestEntityStore_createLogFileRID(t *testing.T) {
entityattributes.DeploymentEnvironment: aws.String("test-environment"),
entityattributes.ServiceName: aws.String("test-service"),
entityattributes.EntityType: aws.String(Service),
entityattributes.AwsAccountId: aws.String(accountId),
},
Attributes: map[string]*string{
InstanceIDKey: aws.String(instanceId),
Expand Down
61 changes: 6 additions & 55 deletions plugins/outputs/cloudwatchlogs/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,20 @@ import (

type mockLogSrc struct {
logs.LogSrc
returnEmpty bool
}

func (m *mockLogSrc) Entity() *cloudwatchlogs.Entity {
entity := &cloudwatchlogs.Entity{
return &cloudwatchlogs.Entity{
Attributes: map[string]*string{
"PlatformType": aws.String("AWS::EC2"),
"EC2.InstanceId": aws.String("i-123456789"),
"EC2.AutoScalingGroup": aws.String("test-group"),
},
KeyAttributes: map[string]*string{
"Name": aws.String("myService"),
"Environment": aws.String("myEnvironment"),
"AwsAccountId": aws.String("123456789"),
"Name": aws.String("myService"),
"Environment": aws.String("myEnvironment"),
},
}
if m.returnEmpty {
return nil
}
return entity
}

var wg sync.WaitGroup
Expand Down Expand Up @@ -109,7 +103,7 @@ func (e evtMock) Done() {
}
}

func TestAddSingleEvent_WithAccountId(t *testing.T) {
func TestAddSingleEvent(t *testing.T) {
var s svcMock
called := false
nst := "NEXT_SEQ_TOKEN"
Expand All @@ -120,9 +114,8 @@ func TestAddSingleEvent_WithAccountId(t *testing.T) {
"EC2.AutoScalingGroup": aws.String("test-group"),
},
KeyAttributes: map[string]*string{
"Name": aws.String("myService"),
"Environment": aws.String("myEnvironment"),
"AwsAccountId": aws.String("123456789"),
"Name": aws.String("myService"),
"Environment": aws.String("myEnvironment"),
},
}

Expand Down Expand Up @@ -162,48 +155,6 @@ func TestAddSingleEvent_WithAccountId(t *testing.T) {
wg.Wait()
}

func TestAddSingleEvent_WithoutAccountId(t *testing.T) {
var s svcMock
called := false
nst := "NEXT_SEQ_TOKEN"

s.ple = func(in *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) {
called = true

if in.SequenceToken != nil {
t.Errorf("PutLogEvents called with wrong sequenceToken, first call should not provide any token")
}

if *in.LogGroupName != "G" || *in.LogStreamName != "S" {
t.Errorf("PutLogEvents called with wrong group and stream: %v/%v", *in.LogGroupName, *in.LogStreamName)
}

if len(in.LogEvents) != 1 || *in.LogEvents[0].Message != "MSG" {
t.Errorf("PutLogEvents called with incorrect message, got: '%v'", *in.LogEvents[0].Message)
}
require.Nil(t, in.Entity)
return &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &nst,
}, nil
}

stop, p := testPreparation(-1, &s, 1*time.Hour, maxRetryTimeout)
p.logSrc = &mockLogSrc{returnEmpty: true}

p.AddEvent(evtMock{"MSG", time.Now(), nil})
require.False(t, called, "PutLogEvents has been called too fast, it should wait until FlushTimeout.")

p.FlushTimeout = 10 * time.Millisecond
p.resetFlushTimer()

time.Sleep(3 * time.Second)
require.True(t, called, "PutLogEvents has not been called after FlushTimeout has been reached.")
require.NotNil(t, nst, *p.sequenceToken, "Pusher did not capture the NextSequenceToken")

close(stop)
wg.Wait()
}

func TestStopPusherWouldDoFinalSend(t *testing.T) {
var s svcMock
called := false
Expand Down
Loading