Skip to content

Commit 1d0ee0e

Browse files
authored
improv: add support for custom lambda handlers with kwargs #242 (#269)
* improv: support kwargs * chore: add a comment to not forget about async tracer
1 parent 454d669 commit 1d0ee0e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

aws_lambda_powertools/tracing/tracer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def handler(event, context):
290290
)
291291

292292
@functools.wraps(lambda_handler)
293-
def decorate(event, context):
293+
def decorate(event, context, **kwargs):
294294
with self.provider.in_subsegment(name=f"## {lambda_handler_name}") as subsegment:
295295
global is_cold_start
296296
if is_cold_start:
@@ -300,7 +300,7 @@ def decorate(event, context):
300300

301301
try:
302302
logger.debug("Calling lambda handler")
303-
response = lambda_handler(event, context)
303+
response = lambda_handler(event, context, **kwargs)
304304
logger.debug("Received lambda handler response successfully")
305305
self._add_response_as_metadata(
306306
method_name=lambda_handler_name,
@@ -487,6 +487,7 @@ async def async_tasks():
487487
env=os.getenv(constants.TRACER_CAPTURE_ERROR_ENV, "true"), choice=capture_error
488488
)
489489

490+
# Maintenance: Need a factory/builder here to simplify this now
490491
if inspect.iscoroutinefunction(method):
491492
return self._decorate_async_function(
492493
method=method, capture_response=capture_response, capture_error=capture_error, method_name=method_name

tests/functional/test_tracing.py

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ def handler(event, context):
3434
handler({}, {})
3535

3636

37+
def test_capture_lambda_handler_with_additional_kwargs(dummy_response):
38+
# GIVEN tracer lambda handler decorator is used
39+
tracer = Tracer(disabled=True)
40+
41+
# WHEN a lambda handler signature has additional keyword arguments
42+
@tracer.capture_lambda_handler
43+
def handler(event, context, my_extra_option=None, **kwargs):
44+
return dummy_response
45+
46+
# THEN tracer should not raise an Exception
47+
handler({}, {}, blah="blah")
48+
49+
3750
def test_capture_method(dummy_response):
3851
# GIVEN tracer method decorator is used
3952
tracer = Tracer(disabled=True)

0 commit comments

Comments
 (0)