Skip to content

Commit

Permalink
Step function handler (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
billbell73 authored Dec 16, 2022
1 parent a73f5c2 commit 21ac178
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage.out
.idea
.DS_Store
2 changes: 2 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type HandlerConfig struct {

type LambdaResult interface{}

type StepEvent interface{}

type Validatable interface {
Validate() error
}
Expand Down
65 changes: 65 additions & 0 deletions step_function_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
}

0 comments on commit 21ac178

Please sign in to comment.