Created
March 1, 2019 22:43
-
-
Save mlafeldt/0ba38a819030e18d053cedb144839aea to your computer and use it in GitHub Desktop.
Lambda handlertrace example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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