@@ -88,6 +88,7 @@ def test_handle_event_request_happy_case(self):
8888 {},
8989 "invoked_function_arn" ,
9090 0 ,
91+ "tenant_id" ,
9192 bootstrap .StandardLogSink (),
9293 )
9394 self .lambda_runtime .post_invocation_result .assert_called_once_with (
@@ -111,6 +112,7 @@ def test_handle_event_request_invalid_client_context(self):
111112 {},
112113 "invoked_function_arn" ,
113114 0 ,
115+ "tenant_id" ,
114116 bootstrap .StandardLogSink (),
115117 )
116118 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -152,6 +154,7 @@ def test_handle_event_request_invalid_cognito_idenity(self):
152154 "invalid_cognito_identity" ,
153155 "invoked_function_arn" ,
154156 0 ,
157+ "tenant_id" ,
155158 bootstrap .StandardLogSink (),
156159 )
157160 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -194,6 +197,7 @@ def test_handle_event_request_invalid_event_body(self):
194197 {},
195198 "invoked_function_arn" ,
196199 0 ,
200+ "tenant_id" ,
197201 bootstrap .StandardLogSink (),
198202 )
199203 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -238,6 +242,7 @@ def invalid_json_response(json_input, lambda_context):
238242 {},
239243 "invoked_function_arn" ,
240244 0 ,
245+ "tenant_id" ,
241246 bootstrap .StandardLogSink (),
242247 )
243248 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -283,6 +288,7 @@ def __init__(self, message):
283288 {},
284289 "invoked_function_arn" ,
285290 0 ,
291+ "tenant_id" ,
286292 bootstrap .StandardLogSink (),
287293 )
288294 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -335,6 +341,7 @@ def __init__(self, message):
335341 {},
336342 "invoked_function_arn" ,
337343 0 ,
344+ "tenant_id" ,
338345 bootstrap .StandardLogSink (),
339346 )
340347 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -386,6 +393,7 @@ def unable_to_import_module(json_input, lambda_context):
386393 {},
387394 "invoked_function_arn" ,
388395 0 ,
396+ "tenant_id" ,
389397 bootstrap .StandardLogSink (),
390398 )
391399 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -425,6 +433,7 @@ def raise_exception_handler(json_input, lambda_context):
425433 {},
426434 "invoked_function_arn" ,
427435 0 ,
436+ "tenant_id" ,
428437 bootstrap .StandardLogSink (),
429438 )
430439 args , _ = self .lambda_runtime .post_invocation_error .call_args
@@ -475,6 +484,7 @@ def raise_exception_handler(json_input, lambda_context):
475484 {},
476485 "invoked_function_arn" ,
477486 0 ,
487+ "tenant_id" ,
478488 bootstrap .StandardLogSink (),
479489 )
480490
@@ -514,6 +524,7 @@ def raise_exception_handler(json_input, lambda_context):
514524 {},
515525 "invoked_function_arn" ,
516526 0 ,
527+ "tenant_id" ,
517528 bootstrap .StandardLogSink (),
518529 )
519530 error_logs = (
@@ -546,6 +557,7 @@ def raise_exception_handler(json_input, lambda_context):
546557 {},
547558 "invoked_function_arn" ,
548559 0 ,
560+ "tenant_id" ,
549561 bootstrap .StandardLogSink (),
550562 )
551563 error_logs = (
@@ -578,6 +590,7 @@ def raise_exception_handler(json_input, lambda_context):
578590 {},
579591 "invoked_function_arn" ,
580592 0 ,
593+ "tenant_id" ,
581594 bootstrap .StandardLogSink (),
582595 )
583596 error_logs = (
@@ -619,6 +632,7 @@ def raise_exception_handler(json_input, lambda_context):
619632 {},
620633 "invoked_function_arn" ,
621634 0 ,
635+ "tenant_id" ,
622636 bootstrap .StandardLogSink (),
623637 )
624638 error_logs = lambda_unhandled_exception_warning_message + "\n [ERROR]\r "
@@ -652,6 +666,7 @@ def raise_exception_handler(json_input, lambda_context):
652666 {},
653667 "invoked_function_arn" ,
654668 0 ,
669+ "tenant_id" ,
655670 bootstrap .StandardLogSink (),
656671 )
657672
@@ -868,6 +883,7 @@ def test_application_json(self):
868883 cognito_identity_json = None ,
869884 invoked_function_arn = "invocation-arn" ,
870885 epoch_deadline_time_in_ms = 1415836801003 ,
886+ tenant_id = None ,
871887 log_sink = bootstrap .StandardLogSink (),
872888 )
873889
@@ -887,6 +903,7 @@ def test_binary_request_binary_response(self):
887903 cognito_identity_json = None ,
888904 invoked_function_arn = "invocation-arn" ,
889905 epoch_deadline_time_in_ms = 1415836801003 ,
906+ tenant_id = None ,
890907 log_sink = bootstrap .StandardLogSink (),
891908 )
892909
@@ -906,6 +923,7 @@ def test_json_request_binary_response(self):
906923 cognito_identity_json = None ,
907924 invoked_function_arn = "invocation-arn" ,
908925 epoch_deadline_time_in_ms = 1415836801003 ,
926+ tenant_id = None ,
909927 log_sink = bootstrap .StandardLogSink (),
910928 )
911929
@@ -924,6 +942,7 @@ def test_binary_with_application_json(self):
924942 cognito_identity_json = None ,
925943 invoked_function_arn = "invocation-arn" ,
926944 epoch_deadline_time_in_ms = 1415836801003 ,
945+ tenant_id = None ,
927946 log_sink = bootstrap .StandardLogSink (),
928947 )
929948
@@ -1357,6 +1376,31 @@ def test_json_formatter(self, mock_stderr):
13571376 )
13581377 self .assertEqual (mock_stderr .getvalue (), "" )
13591378
1379+ @patch ("awslambdaric.bootstrap._GLOBAL_TENANT_ID" , "test-tenant-id" )
1380+ @patch ("sys.stderr" , new_callable = StringIO )
1381+ def test_json_formatter_with_tenant_id (self , mock_stderr ):
1382+ logger = logging .getLogger ("a.b" )
1383+ level = logging .INFO
1384+ message = "Test json formatting with tenant id"
1385+ expected = {
1386+ "level" : "INFO" ,
1387+ "logger" : "a.b" ,
1388+ "message" : message ,
1389+ "requestId" : "" ,
1390+ "tenantId" : "test-tenant-id" ,
1391+ }
1392+
1393+ with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
1394+ logger .log (level , message )
1395+
1396+ data = json .loads (mock_stdout .getvalue ())
1397+ data .pop ("timestamp" )
1398+ self .assertEqual (
1399+ data ,
1400+ expected ,
1401+ )
1402+ self .assertEqual (mock_stderr .getvalue (), "" )
1403+
13601404 @patch ("sys.stdout" , new_callable = StringIO )
13611405 @patch ("sys.stderr" , new_callable = StringIO )
13621406 def test_exception (self , mock_stderr , mock_stdout ):
0 commit comments