Skip to content

Commit ade6b37

Browse files
committed
simplify event bridge source
1 parent d193ca5 commit ade6b37

2 files changed

Lines changed: 17 additions & 21 deletions

File tree

aws/logs_monitoring_go/internal/handling/eventbridge.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ func NewEventBridge(cfg *config.Config) *EventBridgeHandler {
3030
}
3131

3232
func (h *EventBridgeHandler) Handle(ctx context.Context, event json.RawMessage, out chan<- model.LogEntry) error {
33-
ebSource, err := decodeEventBridgeSource(event)
34-
if err != nil {
35-
return err
36-
}
37-
3833
lambdaOrigin, err := model.GetLambdaOrigin(ctx)
3934
if err != nil {
4035
return fmt.Errorf("get lambda origin: %w", err)
4136
}
4237

43-
source := cmp.Or(h.cfg.Source, EventBridgeSource(ebSource))
38+
ebSource, err := decodeEventBridgeSource(event)
39+
if err != nil {
40+
return err
41+
}
42+
source := cmp.Or(h.cfg.Source, ebSource)
4443
service := cmp.Or(h.cfg.Service, source)
4544

4645
entry := model.NewLogEntry()
@@ -58,14 +57,6 @@ func (h *EventBridgeHandler) Handle(ctx context.Context, event json.RawMessage,
5857
return concurrent.SafeSender(ctx, out, entry)
5958
}
6059

61-
func EventBridgeSource(source string) string {
62-
_, after, found := strings.Cut(source, ".")
63-
if found {
64-
return after
65-
}
66-
return sourceCloudwatch
67-
}
68-
6960
func decodeEventBridgeSource(event json.RawMessage) (string, error) {
7061
dec := json.NewDecoder(bytes.NewReader(event))
7162

@@ -83,7 +74,7 @@ func decodeEventBridgeSource(event json.RawMessage) (string, error) {
8374
if err := dec.Decode(&source); err != nil {
8475
return "", fmt.Errorf("decode eventbridge source: %w", err)
8576
}
86-
return source, nil
77+
return eventBridgeSource(source), nil
8778
}
8879
var skip json.RawMessage
8980
if err := dec.Decode(&skip); err != nil {
@@ -93,3 +84,11 @@ func decodeEventBridgeSource(event json.RawMessage) (string, error) {
9384

9485
return "", nil
9586
}
87+
88+
func eventBridgeSource(source string) string {
89+
_, after, found := strings.Cut(source, ".")
90+
if found {
91+
return after
92+
}
93+
return sourceCloudwatch
94+
}

aws/logs_monitoring_go/internal/handling/eventbridge_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func TestEventBridgeHandler_Handle(t *testing.T) {
3535
Source: "events",
3636
SourceCategory: "aws",
3737
Service: "events",
38-
Tags: model.Tags{"service:events"},
3938
Metadata: testutil.LambdaOrigin(),
4039
},
4140
},
@@ -49,7 +48,6 @@ func TestEventBridgeHandler_Handle(t *testing.T) {
4948
Source: "ec2",
5049
SourceCategory: "aws",
5150
Service: "ec2",
52-
Tags: model.Tags{"service:ec2"},
5351
Metadata: testutil.LambdaOrigin(),
5452
},
5553
},
@@ -63,7 +61,6 @@ func TestEventBridgeHandler_Handle(t *testing.T) {
6361
Source: "custom-source",
6462
SourceCategory: "aws",
6563
Service: "custom-source",
66-
Tags: model.Tags{"service:custom-source"},
6764
Metadata: testutil.LambdaOrigin(),
6865
},
6966
},
@@ -119,14 +116,14 @@ func TestEventBridgeSource(t *testing.T) {
119116
"aws.ec2": {source: "aws.ec2", want: "ec2"},
120117
"aws.s3": {source: "aws.s3", want: "s3"},
121118
"custom.app": {source: "custom.app", want: "app"},
122-
"no dot": {source: "nodot", want: "cloudwatch"},
123-
"empty string": {source: "", want: "cloudwatch"},
119+
"no dot": {source: "nodot", want: "cloudwatch"},
120+
"empty string": {source: "", want: "cloudwatch"},
124121
}
125122

126123
for name, tc := range tests {
127124
t.Run(name, func(t *testing.T) {
128125
t.Parallel()
129-
got := EventBridgeSource(tc.source)
126+
got := eventBridgeSource(tc.source)
130127
if got != tc.want {
131128
t.Errorf("got %q, want %q", got, tc.want)
132129
}

0 commit comments

Comments
 (0)