Skip to content
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

improv: add support for custom lambda handlers with kwargs #242 #269

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
5 changes: 3 additions & 2 deletions aws_lambda_powertools/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down