Skip to content

Commit

Permalink
Add cloudwatch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Jan 24, 2020
1 parent c01ed72 commit efe01c7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cloudwatch_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 CloudWatchContext struct {
Context context.Context
Event events.CloudWatchEvent
Logger zerolog.Logger
NewRelicTx newrelic.Transaction
CorrelationID string
}

type CloudWatchHandlerFunc func(c *CloudWatchContext) error

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

// the resource that triggered the event, e.g. "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule"
var cloudWatchResource string
if len(event.Resources) > 0 {
cloudWatchResource = event.Resources[0]
}

logger := configureLogger(conf).
Str("cloud_watch_resource", cloudWatchResource).
Str("correlation_id", correlationID).
Logger()

c := &CloudWatchContext{
Context: ctx,
Event: event,
Logger: logger,
NewRelicTx: newrelic.FromContext(ctx),
CorrelationID: correlationID,
}

c.AddNewRelicAttribute("functionName", conf.FunctionName)
c.AddNewRelicAttribute("buildVersion", conf.BuildVersion)
c.AddNewRelicAttribute("correlationID", correlationID)
c.AddNewRelicAttribute("cloudWatchResource", cloudWatchResource)

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

func CloudWatchHandlerWithNewRelic(h CloudWatchHandlerFunc, conf HandlerConfig) lambda.Handler {
return nrlambda.Wrap(CloudWatchHandler(h, conf), conf.NewRelicApp)
}

func (c *CloudWatchContext) 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)
}
}
49 changes: 49 additions & 0 deletions cloudwatch_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package g8_test

import (
"context"
"io/ioutil"
"testing"

"github.com/aws/aws-lambda-go/events"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"

"github.com/JSainsburyPLC/g8"
)

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 {
timesCalled++

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

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

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

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

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

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

assert.Equal(t, assert.AnError, err)
assert.Equal(t, 1, timesCalled)
}

0 comments on commit efe01c7

Please sign in to comment.