diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index 668c8f2752..4544206073 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -97,6 +97,8 @@ def custom_event_context_extractor(lambda_event): from opentelemetry.trace.status import Status, StatusCode from opentelemetry.propagate import extract +set_trace + logger = logging.getLogger(__name__) _HANDLER = "_HANDLER" @@ -128,7 +130,6 @@ def _default_event_context_extractor(lambda_event: Any) -> Context: Returns: A Context with configuration found in the event. """ - set_trace() headers = None try: headers = lambda_event["headers"] @@ -164,10 +165,14 @@ def _determine_parent_context( Returns: A Context with configuration found in the carrier. """ - set_trace() parent_context = None extract + # By Tyler: ensure the event context extractor is not invoked if there is an + # already active span context. + + # Related PR: https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2573 + if ( parent_context and get_current_span(parent_context) @@ -283,13 +288,16 @@ def _instrument( meter_provider: MeterProvider = None, ): - # pylint: disable=too-many-locals # pylint: disable=too-many-statements def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches call_wrapped, instance, args, kwargs ): + # This function gets another one in the call_wrapped argument. It then + # instantiates a span and calls the call_wrapped function. + set_trace() + orig_handler_name = ".".join( [wrapped_module_name, wrapped_function_name] ) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/mocks/lambda_function.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/mocks/lambda_function.py index 539c896a0b..fdf2a4c297 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/mocks/lambda_function.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/mocks/lambda_function.py @@ -12,8 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. - def handler(event, context): + # The purpose of this function is to be used as the call_wrapped function + # of _instrumented_lambda_handler_call. return "200 ok" diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py index 4cf6b7fc40..19c00b36c0 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py @@ -42,6 +42,7 @@ MOCK_LAMBDA_API_GATEWAY_HTTP_API_EVENT, ) from .mocks.api_gateway_proxy_event import MOCK_LAMBDA_API_GATEWAY_PROXY_EVENT +from ipdb import set_trace class MockLambdaContext: @@ -128,8 +129,15 @@ def test_active_tracing(self): ) test_env_patch.start() + # This calls AwsLambdaInstrumentor._instrument which finds a function + # from os.environ[_HANDLER] and wraps it with + # _instrumented_lambda_handler_call. AwsLambdaInstrumentor().instrument() + set_trace() + + # This finds the _instrumented_lambda_hanlder_call uising + # os.environ[_HANDLER] and calls it. mock_execute_lambda() spans = self.memory_exporter.get_finished_spans()