From dd5b2972d566f6068f6494ecf9adacc915957ab3 Mon Sep 17 00:00:00 2001 From: Mohammed Ehab Elsaeed <33024315+M-Elsaeed@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:24:11 +0100 Subject: [PATCH 1/2] Lazy Load RuntimeClient --- awslambdaric/bootstrap.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index cb8d5c3..d35f8d4 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -11,7 +11,6 @@ import traceback from .lambda_context import LambdaContext -from .lambda_runtime_client import LambdaRuntimeClient from .lambda_runtime_exception import FaultException from .lambda_runtime_log_utils import ( _DATETIME_FORMAT, @@ -158,7 +157,6 @@ def handle_event_request( cognito_identity_json, invoked_function_arn, epoch_deadline_time_in_ms, - tenant_id, log_sink, ): error_result = None @@ -169,7 +167,6 @@ def handle_event_request( epoch_deadline_time_in_ms, invoke_id, invoked_function_arn, - tenant_id, ) event = lambda_runtime_client.marshaller.unmarshal_request( event_body, content_type @@ -201,7 +198,9 @@ def handle_event_request( ) if error_result is not None: + from .lambda_literals import lambda_unhandled_exception_warning_message + logging.warning(lambda_unhandled_exception_warning_message) log_error(error_result, log_sink) lambda_runtime_client.post_invocation_error( invoke_id, to_json(error_result), to_json(xray_fault) @@ -229,7 +228,6 @@ def create_lambda_context( epoch_deadline_time_in_ms, invoke_id, invoked_function_arn, - tenant_id, ): client_context = None if client_context_json: @@ -244,7 +242,6 @@ def create_lambda_context( cognito_identity, epoch_deadline_time_in_ms, invoked_function_arn, - tenant_id, ) @@ -339,7 +336,6 @@ def emit(self, record): class LambdaLoggerFilter(logging.Filter): def filter(self, record): record.aws_request_id = _GLOBAL_AWS_REQUEST_ID or "" - record.tenant_id = _GLOBAL_TENANT_ID return True @@ -448,7 +444,6 @@ def create_log_sink(): _GLOBAL_AWS_REQUEST_ID = None -_GLOBAL_TENANT_ID = None def _setup_logging(log_format, log_level, log_sink): @@ -487,16 +482,17 @@ def run(app_root, handler, lambda_runtime_api_addr): } with create_log_sink() as log_sink: - lambda_runtime_client = LambdaRuntimeClient( - lambda_runtime_api_addr, use_thread_for_polling_next - ) error_result = None try: _setup_logging(_AWS_LAMBDA_LOG_FORMAT, _AWS_LAMBDA_LOG_LEVEL, log_sink) - global _GLOBAL_AWS_REQUEST_ID, _GLOBAL_TENANT_ID + global _GLOBAL_AWS_REQUEST_ID request_handler = _get_handler(handler) + + # import and initialize the LambdaRuntimeClient lazily. + from .lambda_runtime_client import LambdaRuntimeClient + lambda_runtime_client = LambdaRuntimeClient(lambda_runtime_api_addr, use_thread_for_polling_next) except FaultException as e: error_result = make_error( e.msg, @@ -505,11 +501,8 @@ def run(app_root, handler, lambda_runtime_api_addr): ) except Exception: error_result = build_fault_result(sys.exc_info(), None) - + if error_result is not None: - from .lambda_literals import lambda_unhandled_exception_warning_message - - logging.warning(lambda_unhandled_exception_warning_message) log_error(error_result, log_sink) lambda_runtime_client.post_init_error(error_result) @@ -522,7 +515,6 @@ def run(app_root, handler, lambda_runtime_api_addr): event_request = lambda_runtime_client.wait_next_invocation() _GLOBAL_AWS_REQUEST_ID = event_request.invoke_id - _GLOBAL_TENANT_ID = event_request.tenant_id update_xray_env_variable(event_request.x_amzn_trace_id) @@ -536,6 +528,5 @@ def run(app_root, handler, lambda_runtime_api_addr): event_request.cognito_identity, event_request.invoked_function_arn, event_request.deadline_time_in_ms, - event_request.tenant_id, log_sink, ) From ffd199d774d57f33de049dd0b5e602b739628a2a Mon Sep 17 00:00:00 2001 From: Mohammed Ehab Elsaeed <33024315+M-Elsaeed@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:26:33 +0100 Subject: [PATCH 2/2] Lazy Load RuntimeClient --- awslambdaric/bootstrap.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index d35f8d4..3650be0 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -157,6 +157,7 @@ def handle_event_request( cognito_identity_json, invoked_function_arn, epoch_deadline_time_in_ms, + tenant_id, log_sink, ): error_result = None @@ -167,6 +168,7 @@ def handle_event_request( epoch_deadline_time_in_ms, invoke_id, invoked_function_arn, + tenant_id, ) event = lambda_runtime_client.marshaller.unmarshal_request( event_body, content_type @@ -198,9 +200,7 @@ def handle_event_request( ) if error_result is not None: - from .lambda_literals import lambda_unhandled_exception_warning_message - logging.warning(lambda_unhandled_exception_warning_message) log_error(error_result, log_sink) lambda_runtime_client.post_invocation_error( invoke_id, to_json(error_result), to_json(xray_fault) @@ -228,6 +228,7 @@ def create_lambda_context( epoch_deadline_time_in_ms, invoke_id, invoked_function_arn, + tenant_id, ): client_context = None if client_context_json: @@ -242,6 +243,7 @@ def create_lambda_context( cognito_identity, epoch_deadline_time_in_ms, invoked_function_arn, + tenant_id, ) @@ -336,6 +338,7 @@ def emit(self, record): class LambdaLoggerFilter(logging.Filter): def filter(self, record): record.aws_request_id = _GLOBAL_AWS_REQUEST_ID or "" + record.tenant_id = _GLOBAL_TENANT_ID return True @@ -444,6 +447,7 @@ def create_log_sink(): _GLOBAL_AWS_REQUEST_ID = None +_GLOBAL_TENANT_ID = None def _setup_logging(log_format, log_level, log_sink): @@ -486,11 +490,9 @@ def run(app_root, handler, lambda_runtime_api_addr): try: _setup_logging(_AWS_LAMBDA_LOG_FORMAT, _AWS_LAMBDA_LOG_LEVEL, log_sink) - global _GLOBAL_AWS_REQUEST_ID + global _GLOBAL_AWS_REQUEST_ID, _GLOBAL_TENANT_ID request_handler = _get_handler(handler) - - # import and initialize the LambdaRuntimeClient lazily. from .lambda_runtime_client import LambdaRuntimeClient lambda_runtime_client = LambdaRuntimeClient(lambda_runtime_api_addr, use_thread_for_polling_next) except FaultException as e: @@ -501,8 +503,11 @@ def run(app_root, handler, lambda_runtime_api_addr): ) except Exception: error_result = build_fault_result(sys.exc_info(), None) - + if error_result is not None: + from .lambda_literals import lambda_unhandled_exception_warning_message + + logging.warning(lambda_unhandled_exception_warning_message) log_error(error_result, log_sink) lambda_runtime_client.post_init_error(error_result) @@ -515,6 +520,7 @@ def run(app_root, handler, lambda_runtime_api_addr): event_request = lambda_runtime_client.wait_next_invocation() _GLOBAL_AWS_REQUEST_ID = event_request.invoke_id + _GLOBAL_TENANT_ID = event_request.tenant_id update_xray_env_variable(event_request.x_amzn_trace_id) @@ -528,5 +534,6 @@ def run(app_root, handler, lambda_runtime_api_addr): event_request.cognito_identity, event_request.invoked_function_arn, event_request.deadline_time_in_ms, + event_request.tenant_id, log_sink, )