-
Notifications
You must be signed in to change notification settings - Fork 564
Introduce HandlerTrace callbacks #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce HandlerTrace callbacks #141
Conversation
Codecov Report
@@ Coverage Diff @@
## master #141 +/- ##
=========================================
+ Coverage 68.89% 69.8% +0.91%
=========================================
Files 17 18 +1
Lines 659 679 +20
=========================================
+ Hits 454 474 +20
Misses 165 165
Partials 40 40
Continue to review full report at Codecov.
|
Extra thoughts! The context parameter to the callbacks might not be necessary (people can put state in anonymous functions), but it seems like a convenient thing to have. I chose functions (over interface values) for the callbacks because they are easier to compose. Note that if |
eabfd69
to
e53cceb
Compare
This PR has been rebased on the latest master. It is ready to go! |
Hey Will, apologies for the wait. I've got 2 open concerns.
Anyways, looks good, even tough it makes me wish for generics! |
e53cceb
to
dba5c05
Compare
Hi @bmoffatt Great!! I have rebased this PR and moved the new functions into a new I don't know what the best middleware story would be, but I think this is a good forward step! I expect that only middleware authors are going to be using |
dba5c05
to
88abe6a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 with renames
lambda/handlertrace/trace.go
Outdated
// WithHandlerTrace adds callbacks to the provided context which allows handlers | ||
// which wrap the return value of lambda.NewHandler to access to the request and | ||
// response events. | ||
func WithHandlerTrace(ctx context.Context, trace HandlerTrace) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename as NewContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lambda/handlertrace/trace.go
Outdated
|
||
// ContextHandlerTrace returns the HandlerTrace associated with the provided | ||
// context. | ||
func ContextHandlerTrace(ctx context.Context) HandlerTrace { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename as FromContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@bmoffatt Functions renamed. Back to you! |
Thanks! This'll get included in a release tag soon as well. |
Hi
Now that
NewHandler
is public, it is possible to write "middleware" instrumentation that wraps the handler. eg.lambda.Start(myHandler)
becomeslambda.StartHandler(addMiddleware(myHandler))
. Fantastic!Unfortunately, since the internal
lambdaHandler
does the JSON serialization and deserialization, the middleware doesn't have access to the request and response events. This is unfortunate because middleware instrumentation would really love to record request and response fields (such as the request event source ARN).Consumers could be asked to modify their handlers to add callbacks, but it sure would be nice if the middleware could access the request and response automatically!!
Thinking through this problem, I was reminded of the httptrace package which allows people to add http instrumentation by adding callbacks to the context. That's the idea presented here!
Example use:
Thanks for your consideration!