Skip to content
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

Merged
merged 5 commits into from
Mar 9, 2020
Merged

Conversation

tianchu
Copy link
Contributor

@tianchu tianchu commented Mar 6, 2020

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

module.exports.handler = datadog(datadog(handler));

Fix this by wrapping the handler only once, while forceWrap allow wrapping to be forced, which is useful for unit tests.

@tianchu tianchu requested a review from a team as a code owner March 6, 2020 21:12
src/index.ts Outdated
@@ -65,6 +68,7 @@ let currentTraceListener: TraceListener | undefined;
*/
export function datadog<TEvent, TResult>(
handler: Handler<TEvent, TResult>,
forceWrap = false,
Copy link
Contributor

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
Copy link
Contributor

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-io
Copy link

Codecov Report

Merging #57 into master will decrease coverage by 0.65%.
The diff coverage is 50%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/index.ts 82.66% <50%> (-6.07%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 21cabbf...dfba699. Read the comment docs.

Copy link
Contributor

@DarcyRaynerDD DarcyRaynerDD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tianchu tianchu merged commit def3186 into master Mar 9, 2020
@tianchu tianchu deleted the tian.chu/only-wrap-handler-once branch March 9, 2020 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants