-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3_handler.go
67 lines (56 loc) · 1.76 KB
/
s3_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package g8
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/google/uuid"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrlambda"
"github.com/rs/zerolog"
)
type S3Context struct {
Context context.Context
EventRecord events.S3EventRecord
Logger zerolog.Logger
NewRelicTx newrelic.Transaction
CorrelationID string
}
type S3HandlerFunc func(c *S3Context) error
func S3Handler(h S3HandlerFunc, conf HandlerConfig) func(context.Context, events.S3Event) error {
return func(ctx context.Context, e events.S3Event) error {
for _, record := range e.Records {
correlationID := uuid.New().String()
logger := configureLogger(conf).
Str("s3_event_source", record.EventSource).
Str("correlation_id", correlationID).
Logger()
c := &S3Context{
Context: ctx,
EventRecord: record,
Logger: logger,
NewRelicTx: newrelic.FromContext(ctx),
CorrelationID: correlationID,
}
c.AddNewRelicAttribute("functionName", conf.FunctionName)
c.AddNewRelicAttribute("buildVersion", conf.BuildVersion)
c.AddNewRelicAttribute("correlationID", correlationID)
c.AddNewRelicAttribute("s3EventSource", record.EventSource)
if err := h(c); err != nil {
logUnhandledError(c.Logger, err)
return err
}
}
return nil
}
}
func S3HandlerWithNewRelic(h S3HandlerFunc, conf HandlerConfig) lambda.Handler {
return nrlambda.Wrap(S3Handler(h, conf), conf.NewRelicApp)
}
func (c *S3Context) AddNewRelicAttribute(key string, val interface{}) {
if c.NewRelicTx == nil {
return
}
if err := c.NewRelicTx.AddAttribute(key, val); err != nil {
c.Logger.Error().Msgf("failed to add attr '%s' to new relic tx: %+v", key, err)
}
}