Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created March 1, 2019 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlafeldt/0ba38a819030e18d053cedb144839aea to your computer and use it in GitHub Desktop.
Save mlafeldt/0ba38a819030e18d053cedb144839aea to your computer and use it in GitHub Desktop.
Lambda handlertrace example
func main() {
lambda.StartHandler(middleware(handler))
}
type handlerFunc func(ctx context.Context, payload []byte) ([]byte, error)
func (h handlerFunc) Invoke(ctx context.Context, input []byte) ([]byte, error) {
log.Printf("[DEBUG] input = %s", string(input))
output, err := h(ctx, input)
log.Printf("[DEBUG] output = %s", string(output))
return output, err
}
func middleware(originalHandler interface{}) lambda.Handler {
handler := lambda.NewHandler(originalHandler)
return handlerFunc(func(ctx context.Context, payload []byte) ([]byte, error) {
ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{
RequestEvent: func(c context.Context, e interface{}) {
log.Printf("[DEBUG] input = %+v", e)
},
ResponseEvent: func(c context.Context, e interface{}) {
log.Printf("[DEBUG] output = %+v", e)
},
})
return handler.Invoke(ctx, payload)
})
}
func handler(input Input) (*Output, error) {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment