-
Notifications
You must be signed in to change notification settings - Fork 36
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
Only wrap the handler once #57
Conversation
src/index.ts
Outdated
@@ -65,6 +68,7 @@ let currentTraceListener: TraceListener | undefined; | |||
*/ | |||
export function datadog<TEvent, TResult>( | |||
handler: Handler<TEvent, TResult>, | |||
forceWrap = false, |
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.
Can we add forceWrap to the config object instead? Changing the signature of this handler means any customer using the config parameter will have to update their code. It also means we have to go in and update the serverless-plugin to use the new property.
@@ -73,6 +81,12 @@ export function datadog<TEvent, TResult>( | |||
const traceListener = new TraceListener(finalConfig, handlerName); | |||
const listeners = [metricsListener, traceListener]; | |||
|
|||
// Only wrap the handler once unless forced |
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.
This is my bad, but I just realized this might be kind of fragile, if we have two handlers in a single file, I think this will break.
eg.
const firstFunction = datadatog(() => {..});
const secondFunction = datadatog(() => {..}); // This won't get wrapped, customer would have to use forceWrap
I think you might have to do some fancy logic inside the bottom lambdas
Codecov Report
@@ Coverage Diff @@
## master #57 +/- ##
==========================================
- Coverage 93% 92.35% -0.66%
==========================================
Files 25 25
Lines 715 719 +4
Branches 116 117 +1
==========================================
- Hits 665 664 -1
- Misses 30 32 +2
- Partials 20 23 +3
Continue to review full report at Codecov.
|
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.
LGTM
What does this PR do?
When user uses the datadog serverless plugin and also have the handler wrapped, it will result in the handler being wrapped twice and cause enhanced metrics being emitted twice on each invocation (there are probably other side effects). The result looks like
Fix this by wrapping the handler only once, while
forceWrap
allow wrapping to be forced, which is useful for unit tests.