diff --git a/aws_lambda_powertools/tracing/tracer.py b/aws_lambda_powertools/tracing/tracer.py index 26e12a0fb63..20dda36c752 100644 --- a/aws_lambda_powertools/tracing/tracer.py +++ b/aws_lambda_powertools/tracing/tracer.py @@ -290,7 +290,7 @@ def handler(event, context): ) @functools.wraps(lambda_handler) - def decorate(event, context): + def decorate(event, context, **kwargs): with self.provider.in_subsegment(name=f"## {lambda_handler_name}") as subsegment: global is_cold_start if is_cold_start: @@ -300,7 +300,7 @@ def decorate(event, context): try: logger.debug("Calling lambda handler") - response = lambda_handler(event, context) + response = lambda_handler(event, context, **kwargs) logger.debug("Received lambda handler response successfully") self._add_response_as_metadata( method_name=lambda_handler_name, @@ -487,6 +487,7 @@ async def async_tasks(): env=os.getenv(constants.TRACER_CAPTURE_ERROR_ENV, "true"), choice=capture_error ) + # Maintenance: Need a factory/builder here to simplify this now if inspect.iscoroutinefunction(method): return self._decorate_async_function( method=method, capture_response=capture_response, capture_error=capture_error, method_name=method_name diff --git a/tests/functional/test_tracing.py b/tests/functional/test_tracing.py index 577d37aeffb..617bf816f86 100644 --- a/tests/functional/test_tracing.py +++ b/tests/functional/test_tracing.py @@ -34,6 +34,19 @@ def handler(event, context): handler({}, {}) +def test_capture_lambda_handler_with_additional_kwargs(dummy_response): + # GIVEN tracer lambda handler decorator is used + tracer = Tracer(disabled=True) + + # WHEN a lambda handler signature has additional keyword arguments + @tracer.capture_lambda_handler + def handler(event, context, my_extra_option=None, **kwargs): + return dummy_response + + # THEN tracer should not raise an Exception + handler({}, {}, blah="blah") + + def test_capture_method(dummy_response): # GIVEN tracer method decorator is used tracer = Tracer(disabled=True)