-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep_function_handler.go
65 lines (53 loc) · 1.7 KB
/
step_function_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
package g8
import (
"context"
"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 StepContext struct {
Context context.Context
Event StepEvent
Logger zerolog.Logger
NewRelicTx newrelic.Transaction
CorrelationID string
}
type StepHandlerFunc func(c *StepContext) (StepEvent, error)
func StepHandler(h StepHandlerFunc, conf HandlerConfig) func(context.Context, StepEvent) (StepEvent, error) {
return func(ctx context.Context, e StepEvent) (StepEvent, error) {
correlationID := uuid.New().String()
logger := configureLogger(conf).
Str("event_source", "step_function_event").
Str("correlation_id", correlationID).
Logger()
c := &StepContext{
Context: ctx,
Event: e,
Logger: logger,
NewRelicTx: newrelic.FromContext(ctx),
CorrelationID: correlationID,
}
c.AddNewRelicAttribute("functionName", conf.FunctionName)
c.AddNewRelicAttribute("buildVersion", conf.BuildVersion)
c.AddNewRelicAttribute("correlationID", correlationID)
c.AddNewRelicAttribute("eventSource", "step_function_event")
result, err := h(c)
if err != nil {
logUnhandledError(c.Logger, err)
}
return result, err
}
}
func StepHandlerWithNewRelic(h StepHandlerFunc, conf HandlerConfig) lambda.Handler {
return nrlambda.Wrap(StepHandler(h, conf), conf.NewRelicApp)
}
func (c *StepContext) 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)
}
}