Skip to content

Fix wrapping for multiple handler functions in one file #47

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions datadog_lambda/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ class _LambdaDecorator(object):
and extracts/injects trace context.
"""

_instance = None
_force_new = False

def __new__(cls, func):
"""
If the decorator is accidentally applied to multiple functions or
the same function multiple times, only the first one takes effect.
If the decorator is accidentally applied to the same function multiple times,
only the first one takes effect.

If _force_new, always return a real decorator, useful for unit tests.
"""
if cls._force_new or cls._instance is None:
cls._instance = super(_LambdaDecorator, cls).__new__(cls)
return cls._instance
if cls._force_new or not getattr(func, "_dd_wrapped", False):
wrapped = super(_LambdaDecorator, cls).__new__(cls)
wrapped._dd_wrapped = True
return wrapped
else:
return _NoopDecorator(func)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,11 @@ def lambda_handler(event, context):
# the second @datadog_lambda_wrapper should actually be no-op.
datadog_lambda_wrapper._force_new = False

@datadog_lambda_wrapper
def lambda_handler_wrapper(event, context):
lambda_handler(event, context)
lambda_handler_double_wrapped = datadog_lambda_wrapper(lambda_handler)

lambda_event = {}

lambda_handler_wrapper(lambda_event, get_mock_context())
lambda_handler_double_wrapped(lambda_event, get_mock_context())

self.mock_patch_all.assert_called_once()
self.mock_wrapper_lambda_stats.flush.assert_called_once()
Expand Down