Skip to content

Fix duplicate enhanced metrics #46

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

Merged
merged 1 commit into from
Mar 9, 2020
Merged

Conversation

tianchu
Copy link
Collaborator

@tianchu tianchu commented Mar 6, 2020

What does this PR do?

If the datadog serverless plugin is used, which automatically wraps the function's handler with @datadog_lambda_wrapper, and the handler function is also decorated with @datadog_lambda_wrapper, the resulted code is equivalent to the following, and two wrappers get created.

@datadog_lambda_wrapper
def lambda_handler(event, context):
    pass

def plugin_generated_lambda_handler(event, context):
    datadog_lambda_wrapper(lambda_handler)(event, context)

This results in __init__ get called twice on cold start and __call__ gets called twice on every invocation. Because not every operation in those two methods are idempotent, this causes unexpected behaviors. E.g., aws.lambda.enhanced.invocations will get submitted twice for every single invocation.

Let @datadog_lambda_wrapper instantiate a no-op decorator _NoopDecorator when a wrapper is already instantiated. But allow _force_new to force create a new/real wrapper every time, which is needed by the unit tests.

@tianchu tianchu requested a review from a team as a code owner March 6, 2020 03:30
Copy link
Contributor

@sfirrin sfirrin left a comment

Choose a reason for hiding this comment

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

looks good

cls._instance = super(_LambdaDecorator, cls).__new__(cls)
return cls._instance
else:
return _NoopDecorator(func)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice idea

@tianchu tianchu merged commit 0c8b405 into master Mar 9, 2020
@tianchu tianchu deleted the tian.chu/fix-dup-enhanced-metrics branch March 9, 2020 14:00
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.

2 participants