From 6cd28bca807b5febfc2a66201a08d4ac1dac3b7c Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Mon, 11 Feb 2019 11:08:58 -0800 Subject: [PATCH] Renamed config module name to the global name (#129) sdk_config_module to global_sdk_config Added unit test to test non-initialized lambda context. --- aws_xray_sdk/core/lambda_launcher.py | 2 +- tests/test_lambda_context.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/aws_xray_sdk/core/lambda_launcher.py b/aws_xray_sdk/core/lambda_launcher.py index 79ab8338..f35b2d99 100644 --- a/aws_xray_sdk/core/lambda_launcher.py +++ b/aws_xray_sdk/core/lambda_launcher.py @@ -71,7 +71,7 @@ def put_subsegment(self, subsegment): current_entity = self.get_trace_entity() if not self._is_subsegment(current_entity) and current_entity.initializing: - if sdk_config_module.sdk_enabled(): + if global_sdk_config.sdk_enabled(): log.warning("Subsegment %s discarded due to Lambda worker still initializing" % subsegment.name) return diff --git a/tests/test_lambda_context.py b/tests/test_lambda_context.py index 90f405e8..29b1cc42 100644 --- a/tests/test_lambda_context.py +++ b/tests/test_lambda_context.py @@ -60,3 +60,22 @@ def test_disable(): global_sdk_config.set_sdk_enabled(False) segment = context.get_trace_entity() assert not segment.sampled + + +def test_non_initialized(): + # Context that hasn't been initialized by lambda container should not add subsegments to the facade segment. + temp_header_var = os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] + del os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] + + temp_context = lambda_launcher.LambdaContext() + facade_segment = temp_context.get_trace_entity() + subsegment = Subsegment("TestSubsegment", "local", facade_segment) + temp_context.put_subsegment(subsegment) + + assert temp_context.get_trace_entity() == facade_segment + + # "Lambda" container added metadata now. Should see subsegment now. + os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = temp_header_var + temp_context.put_subsegment(subsegment) + + assert temp_context.get_trace_entity() == subsegment