66package parsing
77
88import (
9+ "cmp"
910 "context"
1011 "encoding/json"
1112 "fmt"
1213 "log/slog"
1314 "regexp"
15+ "slices"
1416 "strings"
1517
1618 "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/concurrent"
@@ -19,7 +21,7 @@ import (
1921 "github.com/aws/aws-lambda-go/events"
2022)
2123
22- type s3RecordContext struct {
24+ type s3Record struct {
2325 metadata model.Metadata
2426 tags model.Tags
2527 source string
@@ -30,19 +32,31 @@ type s3RecordContext struct {
3032}
3133
3234func HandleS3 (ctx context.Context , event json.RawMessage , cfg * config.Config , out chan <- model.S3LogEntry ) error {
33- var s3Event events. S3Event
34- if err := json . Unmarshal ( event , & s3Event ); err != nil {
35- return fmt .Errorf ("unmarshal : %w" , err )
35+ client , metadata , err := setupS3 ( ctx , cfg )
36+ if err != nil {
37+ return fmt .Errorf ("setup s3 : %w" , err )
3638 }
39+ return handleS3Event (ctx , event , cfg , client , metadata , out )
40+ }
3741
42+ func setupS3 (ctx context.Context , cfg * config.Config ) (S3APIClient , model.Metadata , error ) {
3843 client , err := createS3APIClient (ctx , cfg .UseFIPS )
3944 if err != nil {
40- return fmt .Errorf ("create S3 client: %w" , err )
45+ return nil , model. Metadata {}, fmt .Errorf ("create S3 client: %w" , err )
4146 }
4247
43- forwarderMetadata , err := model .GetMetadata (ctx )
48+ metadata , err := model .GetMetadata (ctx )
4449 if err != nil {
45- return err
50+ return nil , model.Metadata {}, fmt .Errorf ("get metadata: %w" , err )
51+ }
52+
53+ return client , metadata , nil
54+ }
55+
56+ func handleS3Event (ctx context.Context , event json.RawMessage , cfg * config.Config , client S3APIClient , metadata model.Metadata , out chan <- model.S3LogEntry ) error {
57+ var s3Event events.S3Event
58+ if err := json .Unmarshal (event , & s3Event ); err != nil {
59+ return fmt .Errorf ("unmarshal: %w" , err )
4660 }
4761
4862 for _ , record := range s3Event .Records {
@@ -51,30 +65,36 @@ func HandleS3(ctx context.Context, event json.RawMessage, cfg *config.Config, ou
5165
5266 tags , service := getTagsAndService (cfg )
5367 source := getS3Source (cfg .Source , key )
54- if service == "" {
55- service = source
56- }
57-
58- rc := s3RecordContext {
59- forwarderMetadata , tags , source , service , bucket , key , cfg .S3MultilineLogRegex ,
68+ service = cmp .Or (service , source )
69+
70+ rc := s3Record {
71+ metadata : metadata ,
72+ tags : tags ,
73+ source : source ,
74+ service : service ,
75+ bucket : bucket ,
76+ key : key ,
77+ multilineRegex : cfg .S3MultilineLogRegex ,
6078 }
6179 if err := processS3Record (ctx , client , out , rc ); err != nil {
62- return fmt .Errorf ("process S3 record: %w" , err )
80+ slog .WarnContext (ctx , "skipping s3 record" ,
81+ "bucket" , bucket , "key" , key , "error" , err )
82+ continue
6383 }
6484 }
6585
6686 return nil
6787}
6888
69- func processS3Record (ctx context.Context , client S3APIClient , out chan <- model.S3LogEntry , rc s3RecordContext ) error {
89+ func processS3Record (ctx context.Context , client S3APIClient , out chan <- model.S3LogEntry , rc s3Record ) error {
7090 body , err := getS3Object (ctx , client , rc .bucket , rc .key )
7191 if err != nil {
7292 return err
7393 }
7494
7595 defer func () {
7696 if err := body .Close (); err != nil {
77- slog .Warn ( "failed to close response body" , slog .Any ("error" , err ))
97+ slog .WarnContext ( ctx , "failed to close response body" , slog .Any ("error" , err ))
7898 }
7999 }()
80100
@@ -93,15 +113,15 @@ func processS3Record(ctx context.Context, client S3APIClient, out chan<- model.S
93113 return nil
94114}
95115
96- func makeS3Entry (rc s3RecordContext , message string ) model.S3LogEntry {
116+ func makeS3Entry (rc s3Record , message string ) model.S3LogEntry {
97117 ddtags , ddtagsService , message := extractFromMessage (message )
98118
99119 entryService := rc .service
100120 if ddtagsService != "" {
101121 entryService = ddtagsService
102122 }
103123
104- ddtags = append (ddtags , "service:" + entryService )
124+ tags := slices . Concat (ddtags , model. Tags { "service:" + entryService }, rc . tags )
105125 metadata := model.S3Metadata {
106126 Metadata : rc .metadata ,
107127 S3Context : model.S3Context {
@@ -115,7 +135,7 @@ func makeS3Entry(rc s3RecordContext, message string) model.S3LogEntry {
115135 Source : rc .source ,
116136 SourceCategory : sourceCategory ,
117137 Service : entryService ,
118- Tags : append ( ddtags , rc . tags ... ) ,
138+ Tags : tags ,
119139 Metadata : metadata ,
120140 }
121141}
0 commit comments