Skip to content

Commit 584f2e3

Browse files
committed
Fix wrapping for multiple handler functions in one file
1 parent 0c8b405 commit 584f2e3

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

datadog_lambda/wrapper.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ class _LambdaDecorator(object):
5353
and extracts/injects trace context.
5454
"""
5555

56-
_instance = None
5756
_force_new = False
5857

5958
def __new__(cls, func):
6059
"""
61-
If the decorator is accidentally applied to multiple functions or
62-
the same function multiple times, only the first one takes effect.
60+
If the decorator is accidentally applied to the same function multiple times,
61+
only the first one takes effect.
6362
6463
If _force_new, always return a real decorator, useful for unit tests.
6564
"""
66-
if cls._force_new or cls._instance is None:
67-
cls._instance = super(_LambdaDecorator, cls).__new__(cls)
68-
return cls._instance
65+
if cls._force_new or not getattr(func, "_dd_wrapped", False):
66+
wrapped = super(_LambdaDecorator, cls).__new__(cls)
67+
wrapped._dd_wrapped = True
68+
return wrapped
6969
else:
7070
return _NoopDecorator(func)
7171

tests/test_wrapper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,11 @@ def lambda_handler(event, context):
269269
# the second @datadog_lambda_wrapper should actually be no-op.
270270
datadog_lambda_wrapper._force_new = False
271271

272-
@datadog_lambda_wrapper
273-
def lambda_handler_wrapper(event, context):
274-
lambda_handler(event, context)
272+
lambda_handler_double_wrapped = datadog_lambda_wrapper(lambda_handler)
275273

276274
lambda_event = {}
277275

278-
lambda_handler_wrapper(lambda_event, get_mock_context())
276+
lambda_handler_double_wrapped(lambda_event, get_mock_context())
279277

280278
self.mock_patch_all.assert_called_once()
281279
self.mock_wrapper_lambda_stats.flush.assert_called_once()

0 commit comments

Comments
 (0)