Skip to content

Commit

Permalink
Add result to lambda to support step functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Jan 25, 2020
1 parent efe01c7 commit 0a0e8a9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
12 changes: 6 additions & 6 deletions cloudwatch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type CloudWatchContext struct {
CorrelationID string
}

type CloudWatchHandlerFunc func(c *CloudWatchContext) error
type CloudWatchHandlerFunc func(c *CloudWatchContext) (LambdaResult, error)

func CloudWatchHandler(h CloudWatchHandlerFunc, conf HandlerConfig) func(context.Context, events.CloudWatchEvent) error {
return func(ctx context.Context, event events.CloudWatchEvent) error {
func CloudWatchHandler(h CloudWatchHandlerFunc, conf HandlerConfig) func(context.Context, events.CloudWatchEvent) (LambdaResult, error) {
return func(ctx context.Context, event events.CloudWatchEvent) (LambdaResult, error) {
correlationID := uuid.New().String()

// the resource that triggered the event, e.g. "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule"
Expand All @@ -49,11 +49,11 @@ func CloudWatchHandler(h CloudWatchHandlerFunc, conf HandlerConfig) func(context
c.AddNewRelicAttribute("correlationID", correlationID)
c.AddNewRelicAttribute("cloudWatchResource", cloudWatchResource)

if err := h(c); err != nil {
result, err := h(c)
if err != nil {
logUnhandledError(c.Logger, err)
return err
}
return nil
return result, err
}
}

Expand Down
13 changes: 7 additions & 6 deletions cloudwatch_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@ import (
func TestCloudWatchHandler_SingleMessage(t *testing.T) {
timesCalled := 0
resourceArn := "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule"
h := g8.CloudWatchHandler(func(c *g8.CloudWatchContext) error {
h := g8.CloudWatchHandler(func(c *g8.CloudWatchContext) (g8.LambdaResult, error) {
timesCalled++

assert.Equal(t, resourceArn, c.Event.Resources[0])
assert.NotEmpty(t, c.CorrelationID)

return nil
return "finished", nil
}, g8.HandlerConfig{Logger: zerolog.New(ioutil.Discard)})

err := h(context.Background(), events.CloudWatchEvent{
result, err := h(context.Background(), events.CloudWatchEvent{
Resources: []string{resourceArn},
})

assert.Nil(t, err)
assert.Equal(t, "finished", result)
assert.Equal(t, 1, timesCalled)
}

func TestCloudWatchHandler_HandlerError(t *testing.T) {
timesCalled := 0
handlerFunc := func(c *g8.CloudWatchContext) error {
handlerFunc := func(c *g8.CloudWatchContext) (g8.LambdaResult, error) {
timesCalled++
return assert.AnError
return nil, assert.AnError
}

h := g8.CloudWatchHandler(handlerFunc, g8.HandlerConfig{
Logger: zerolog.New(ioutil.Discard),
})
err := h(context.Background(), events.CloudWatchEvent{})
_, err := h(context.Background(), events.CloudWatchEvent{})

assert.Equal(t, assert.AnError, err)
assert.Equal(t, 1, timesCalled)
Expand Down
2 changes: 2 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type HandlerConfig struct {
NewRelicApp newrelic.Application
}

type LambdaResult interface{}

type Validatable interface {
Validate() error
}
Expand Down

0 comments on commit 0a0e8a9

Please sign in to comment.