From b2b042262473fc1e72097e2285930764c4383696 Mon Sep 17 00:00:00 2001 From: "(Eliseo) Nathaniel Ruiz Nowell" Date: Mon, 11 Oct 2021 09:59:43 -0700 Subject: [PATCH] Add instrumentation for AWS Lambda Service --- CHANGELOG.md | 3 + .../LICENSE | 201 +++++++++++ .../MANIFEST.in | 8 + .../README.rst | 69 ++++ .../scripts/configure_otel_sdk_on_lambda.py | 177 ++++++++++ ...patch-with-configure_otel_sdk_on_lambda.py | 53 +++ .../setup.cfg | 54 +++ .../setup.py | 99 ++++++ .../instrumentation/aws_lambda/__init__.py | 256 ++++++++++++++ .../instrumentation/aws_lambda/package.py | 16 + .../instrumentation/aws_lambda/version.py | 15 + .../tests/mocks/mock_user_lambda.py | 2 + .../tests/test_aws_lambda_instrumentation.py | 319 ++++++++++++++++++ tox.ini | 6 + 14 files changed, 1278 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/LICENSE create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/MANIFEST.in create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/README.rst create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/configure_otel_sdk_on_lambda.py create mode 100755 instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/patch-with-configure_otel_sdk_on_lambda.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/setup.cfg create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/setup.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/package.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/version.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/tests/mocks/mock_user_lambda.py create mode 100644 instrumentation/opentelemetry-instrumentation-aws_lambda/tests/test_aws_lambda_instrumentation.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5853849236..4814c301da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.0-0.25b0...HEAD) +- `opentelemetry-instrumentation-aws_lambda` Add Instrumentation for AWS Lambda package + ([#739](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/739)) + ## [1.6.0-0.25b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.0-0.25b0) - 2021-10-13 diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/LICENSE b/instrumentation/opentelemetry-instrumentation-aws_lambda/LICENSE new file mode 100644 index 0000000000..1ef7dad2c5 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright The OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/MANIFEST.in b/instrumentation/opentelemetry-instrumentation-aws_lambda/MANIFEST.in new file mode 100644 index 0000000000..faee277146 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/MANIFEST.in @@ -0,0 +1,8 @@ +graft src +graft tests +global-exclude *.pyc +global-exclude *.pyo +global-exclude __pycache__/* +include MANIFEST.in +include README.rst +include LICENSE diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/README.rst b/instrumentation/opentelemetry-instrumentation-aws_lambda/README.rst new file mode 100644 index 0000000000..6f97f8bb06 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/README.rst @@ -0,0 +1,69 @@ +OpenTelemetry AWS Lambda Tracing +================================ + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-aws-lambda.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-aws-lambda/ + +This library provides an Instrumentor used to trace requests made by the Lambda +functions on the AWS Lambda service. + +It also provides scripts used by AWS Lambda Layers to automatically initialize +the OpenTelemetry SDK. Learn more on the AWS Distro for OpenTelemetry (ADOT) +`documentation for the Python Lambda Layer https://aws-otel.github.io/docs/getting-started/lambda/lambda-python`_. + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-aws-lambda + + +Usage +----- + +.. code:: python + # Copy this snippet into an AWS Lambda function + + import boto3 + from opentelemetry.instrumentation.botocore import AwsBotocoreInstrumentor + from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor + + + # Enable instrumentation + AwsBotocoreInstrumentor().instrument() + AwsLambdaInstrumentor().instrument() + + # Lambda function + def lambda_handler(event, context): + s3 = boto3.resource('s3') + for bucket in s3.buckets.all(): + print(bucket.name) + + return "200 OK" + +Using a custom `event_context_extractor` to parent traces with a Trace Context +found in the Lambda Event. + +.. code: python + + from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor + + def custom_event_context_extractor(lambda_event): + # If the `TraceContextTextMapPropagator` is the global propagator, we + # can use it to parse out the context from the HTTP Headers. + return get_global_textmap().extract(lambda_event["foo"]["headers"]) + + AwsLambdaInstrumentor().instrument( + event_context_extractor=custom_event_context_extractor + ) + +References +---------- + +* `OpenTelemetry AWS Lambda Tracing `_ +* `OpenTelemetry Project `_ +* `OpenTelemetry Python Examples `_ +* `ADOT Python Lambda Layer Documentation https://aws-otel.github.io/docs/getting-started/lambda/lambda-python`_ diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/configure_otel_sdk_on_lambda.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/configure_otel_sdk_on_lambda.py new file mode 100644 index 0000000000..e30d21a0fa --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/configure_otel_sdk_on_lambda.py @@ -0,0 +1,177 @@ +import logging +import os +from importlib import import_module +from os import environ + +from pkg_resources import iter_entry_points + +from opentelemetry.instrumentation.aws_lambda import ORIG_HANDLER +from opentelemetry.instrumentation.dependencies import ( + get_dist_dependency_conflicts, +) +from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro +from opentelemetry.instrumentation.environment_variables import ( + OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, +) + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def configure_otel_env_vars(): + aws_lambda_function_name = "AWS_LAMBDA_FUNCTION_NAME" + otel_resource_attributes = "OTEL_RESOURCE_ATTRIBUTES" + + # Set the default Trace Exporter + environ.setdefault("OTEL_TRACES_EXPORTER", "otlp_proto_grpc_span") + + # Set the service name + service_name_resource_attribute = "service.name" + if environ.get(otel_resource_attributes) is None: + environ[otel_resource_attributes] = "%s=%s" % ( + service_name_resource_attribute, + environ.get(aws_lambda_function_name), + ) + elif service_name_resource_attribute not in environ.get( + otel_resource_attributes + ): + environ[otel_resource_attributes] = "%s=%s,%s" % ( + service_name_resource_attribute, + environ.get(aws_lambda_function_name), + environ.get(otel_resource_attributes), + ) + + # Set the Resource Detectors (Resource Attributes) + # TODO: waiting on OTel Python support for configuring Resource Detectors from + # an environment variable. Replace the bottom code with the following when this + # is possible. + # + # environ["OTEL_RESOURCE_DETECTORS"] = "aws_lambda" + # + lambda_resource_attributes = ( + "cloud.region=%s,cloud.provider=aws,faas.name=%s,faas.version=%s" + % ( + environ.get("AWS_REGION"), + environ.get(aws_lambda_function_name), + environ.get("AWS_LAMBDA_FUNCTION_VERSION"), + ) + ) + environ[otel_resource_attributes] = "%s,%s" % ( + lambda_resource_attributes, + environ.get(otel_resource_attributes), + ) + + # Set the default propagators + environ.setdefault("OTEL_PROPAGATORS", "tracecontext,b3,xray") + + +def setup_otel(): + def _load_configurators(): + configured = None + for entry_point in iter_entry_points("opentelemetry_configurator"): + if configured is not None: + logger.warning( + "Configuration of %s not loaded, %s already loaded", + entry_point.name, + configured, + ) + continue + try: + entry_point.load()().configure() # type: ignore + configured = entry_point.name + except Exception as exc: # pylint: disable=broad-except + logger.debug( + "Configuration of %s failed", + entry_point.name, + exc_info=exc, + ) + + def _load_distros() -> BaseDistro: + for entry_point in iter_entry_points("opentelemetry_distro"): + try: + distro = entry_point.load()() + if not isinstance(distro, BaseDistro): + logger.debug( + "%s is not an OpenTelemetry Distro. Skipping", + entry_point.name, + ) + continue + logger.debug( + "Distribution %s will be configured", entry_point.name + ) + return distro + except Exception as exc: # pylint: disable=broad-except + logger.debug( + "Distribution %s configuration failed", + entry_point.name, + exc_info=exc, + ) + return DefaultDistro() + + def _load_instrumentors(distro): + package_to_exclude = os.environ.get( + OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [] + ) + if isinstance(package_to_exclude, str): + package_to_exclude = package_to_exclude.split(",") + # to handle users entering "requests , flask" or "requests, flask" with spaces + package_to_exclude = [x.strip() for x in package_to_exclude] + + for entry_point in iter_entry_points("opentelemetry_instrumentor"): + if entry_point.name in package_to_exclude: + logger.debug( + "Instrumentation skipped for library %s", entry_point.name + ) + continue + + try: + conflict = get_dist_dependency_conflicts(entry_point.dist) + if conflict: + logger.debug( + "Skipping instrumentation %s: %s", + entry_point.name, + conflict, + ) + continue + + # tell instrumentation to not run dep checks again as we already did it above + distro.load_instrumentor(entry_point, skip_dep_check=True) + logger.info("Instrumented %s", entry_point.name) + except Exception as exc: # pylint: disable=broad-except + logger.debug( + "Instrumenting of %s failed", + entry_point.name, + exc_info=exc, + ) + + distro = _load_distros() + distro.configure() + _load_configurators() + _load_instrumentors(distro) + + +configure_otel_env_vars() +setup_otel() + + +class HandlerError(Exception): + pass + + +path = os.environ.get(ORIG_HANDLER, None) +if path is None: + raise HandlerError(f"{ORIG_HANDLER} is not defined.") +parts = path.rsplit(".", 1) +if len(parts) != 2: + raise HandlerError(f"Value {path} for {ORIG_HANDLER} has invalid format.") + + +def modify_module_name(module_name): + """Returns a valid modified module to get imported""" + return ".".join(module_name.split("/")) + + +(mod_name, handler_name) = parts +modified_mod_name = modify_module_name(mod_name) +handler_module = import_module(modified_mod_name) +lambda_handler = getattr(handler_module, handler_name) diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/patch-with-configure_otel_sdk_on_lambda.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/patch-with-configure_otel_sdk_on_lambda.py new file mode 100755 index 0000000000..ea8f2f55c9 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/scripts/patch-with-configure_otel_sdk_on_lambda.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +`patch-with-configure_otel_sdk_on_lambda.py` + +This script patches the AWS Service `_HANDLER` environment variable to call our +`configure_otel_sdk_on_lambda.py` `lambda_handler` instead of the user-defined +Lambda Handler. This way, we can automatically setup OpenTelemetry Python for +the user with some defaults. We use what we expect will be the most common user +configuration. + +We save the original user-defined Lambda Handler in `ORIG_HANDLER` which is used +by `AwsLambdaInstrumentor` during instrumentation. + +Usage +----- +In the configuration of an AWS Lambda function with +`patch-with-configure_otel_sdk_on_lambda.py` at the root level of a Lambda +Layer: + +.. code:: + + AWS_LAMBDA_EXEC_WRAPPER = /opt/patch-with-configure_otel_sdk_on_lambda.py + +""" + +import sys +from os import environ, system + +# Patch the Lambda Handler with our own "Configure OTel SDK Wrapper" Lambda +# Handler. Simply importing the `configure_otel_sdk_on_lambda.py` module +# configures the OTel SDK, we let Lambda call the `lambda_handler` when it is +# ready to do so. +environ["ORIG_HANDLER"] = environ.get("_HANDLER") +environ["_HANDLER"] = "configure_otel_sdk_on_lambda.lambda_handler" + +# Start the program with the originally intended arguments. The system _is_ the +# Python interpreter so we don't need to include it. +system(" ".join(sys.argv[1:])) diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.cfg b/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.cfg new file mode 100644 index 0000000000..ee1dd7a90d --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.cfg @@ -0,0 +1,54 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +[metadata] +name = opentelemetry-instrumentation-aws_lambda +description = OpenTelemetry AWS Lambda instrumentation +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-aws_lambda +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.6 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-instrumentation == 0.25b0 + opentelemetry-propagator-aws-xray == 1.0.0 + opentelemetry-semantic-conventions == 0.25b0 + +[options.extras_require] +test = + opentelemetry-test == 0.25b0 + +[options.packages.find] +where = src + +[options.entry_points] +opentelemetry_instrumentor = + aws_lambda = opentelemetry.instrumentation.aws_lambda:AwsLambdaInstrumentor diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.py new file mode 100644 index 0000000000..d1a8664236 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/setup.py @@ -0,0 +1,99 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# DO NOT EDIT. THIS FILE WAS AUTOGENERATED FROM templates/instrumentation_setup.py.txt. +# RUN `python scripts/generate_setup.py` TO REGENERATE. + + +import distutils.cmd +import json +import os +from configparser import ConfigParser + +import setuptools + +config = ConfigParser() +config.read("setup.cfg") + +# We provide extras_require parameter to setuptools.setup later which +# overwrites the extra_require section from setup.cfg. To support extra_require +# secion in setup.cfg, we load it here and merge it with the extra_require param. +extras_require = {} +if "options.extras_require" in config: + for key, value in config["options.extras_require"].items(): + extras_require[key] = [v for v in value.split("\n") if v.strip()] + +BASE_DIR = os.path.dirname(__file__) +PACKAGE_INFO = {} + +VERSION_FILENAME = os.path.join( + BASE_DIR, + "src", + "opentelemetry", + "instrumentation", + "aws_lambda", + "version.py", +) +with open(VERSION_FILENAME, encoding="utf-8") as f: + exec(f.read(), PACKAGE_INFO) + +PACKAGE_FILENAME = os.path.join( + BASE_DIR, + "src", + "opentelemetry", + "instrumentation", + "aws_lambda", + "package.py", +) +with open(PACKAGE_FILENAME, encoding="utf-8") as f: + exec(f.read(), PACKAGE_INFO) + +# Mark any instruments/runtime dependencies as test dependencies as well. +extras_require["instruments"] = PACKAGE_INFO["_instruments"] +test_deps = extras_require.get("test", []) +for dep in extras_require["instruments"]: + test_deps.append(dep) + +extras_require["test"] = test_deps + + +class JSONMetadataCommand(distutils.cmd.Command): + + description = ( + "print out package metadata as JSON. This is used by OpenTelemetry dev scripts to ", + "auto-generate code in other places", + ) + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + metadata = { + "name": config["metadata"]["name"], + "version": PACKAGE_INFO["__version__"], + "instruments": PACKAGE_INFO["_instruments"], + } + print(json.dumps(metadata)) + + +setuptools.setup( + cmdclass={"meta": JSONMetadataCommand}, + version=PACKAGE_INFO["__version__"], + extras_require=extras_require, +) 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 new file mode 100644 index 0000000000..35b41f3c3f --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -0,0 +1,256 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +The opentelemetry-instrumentation-aws-lambda package provides an `Instrumentor` +to traces calls whithin a Python AWS Lambda function. + +Usage +----- + +.. code:: python + # Copy this snippet into an AWS Lambda function + + import boto3 + from opentelemetry.instrumentation.botocore import AwsBotocoreInstrumentor + from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor + + + # Enable instrumentation + AwsBotocoreInstrumentor().instrument() + AwsLambdaInstrumentor().instrument() + + # Lambda function + def lambda_handler(event, context): + s3 = boto3.resource('s3') + for bucket in s3.buckets.all(): + print(bucket.name) + + return "200 OK" + +API +--- + +The `instrument` method accepts the following keyword args: + +tracer_provider (TracerProvider) - an optional tracer provider +event_context_extractor (Callable) - a function that returns an OTel Trace + Context given the Lambda Event the AWS Lambda was invoked with + this function signature is: def event_context_extractor(lambda_event: Any) -> Context +for example: + +.. code: python + + from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor + + def custom_event_context_extractor(lambda_event): + # If the `TraceContextTextMapPropagator` is the global propagator, we + # can use it to parse out the context from the HTTP Headers. + return get_global_textmap().extract(lambda_event["foo"]["headers"]) + + AwsLambdaInstrumentor().instrument( + event_context_extractor=custom_event_context_extractor + ) +""" + +import logging +import os +from importlib import import_module +from typing import Any, Collection + +from wrapt import wrap_function_wrapper + +from opentelemetry.context.context import Context +from opentelemetry.instrumentation.aws_lambda.package import _instruments +from opentelemetry.instrumentation.aws_lambda.version import __version__ +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.utils import unwrap +from opentelemetry.propagate import get_global_textmap +from opentelemetry.propagators.aws.aws_xray_propagator import ( + TRACE_HEADER_KEY, + AwsXRayPropagator, +) +from opentelemetry.semconv.resource import ResourceAttributes +from opentelemetry.semconv.trace import SpanAttributes +from opentelemetry.trace import ( + SpanKind, + TracerProvider, + get_tracer, + get_tracer_provider, +) +from opentelemetry.trace.propagation import get_current_span + +logger = logging.getLogger(__name__) + +_HANDLER = "_HANDLER" +_X_AMZN_TRACE_ID = "_X_AMZN_TRACE_ID" +ORIG_HANDLER = "ORIG_HANDLER" + + +class AwsLambdaInstrumentor(BaseInstrumentor): + def instrumentation_dependencies(self) -> Collection[str]: + return _instruments + + def _instrument(self, **kwargs): + """Instruments Lambda Handlers on AWS Lambda. + + See more: + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#instrumenting-aws-lambda + + Args: + **kwargs: Optional arguments + ``tracer_provider``: a TracerProvider, defaults to global + ``event_context_extractor``: a method which takes the Lambda + Event as input and extracts an OTel Context from it. By default, + the context is extracted from the HTTP headers of an API Gateway + request. + """ + lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER)) + wrapped_names = lambda_handler.rsplit(".", 1) + self._wrapped_module_name = wrapped_names[0] + self._wrapped_function_name = wrapped_names[1] + + _instrument( + self._wrapped_module_name, + self._wrapped_function_name, + tracer_provider=kwargs.get("tracer_provider"), + event_context_extractor=kwargs.get("event_context_extractor"), + ) + + def _uninstrument(self, **kwargs): + unwrap( + import_module(self._wrapped_module_name), + self._wrapped_function_name, + ) + + +def _default_event_context_extractor(lambda_event: Any) -> Context: + """Default way of extracting the context from the Lambda Event. + + Assumes the Lambda Event is a map with the headers under the 'headers' key. + This is the mapping to use when the Lambda is invoked by an API Gateway + REST API where API Gateway is acting as a pure proxy for the request. + + See more: + https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + Args: + lambda_event: user-defined, so it could be anything, but this + method counts it being a map with a 'headers' key + Returns: + A Context with configuration found in the event. + """ + try: + headers = lambda_event["headers"] + except (TypeError, KeyError): + logger.debug( + "Extracting context from Lambda Event failed: either enable X-Ray active tracing or configure API Gateway to trigger this Lambda function as a pure proxy. Otherwise, generated spans will have an invalid (empty) parent context." + ) + headers = {} + return get_global_textmap().extract(headers) + + +def _instrument( + wrapped_module_name, + wrapped_function_name, + tracer_provider: TracerProvider = None, + event_context_extractor=None, +): + def _determine_parent_context(lambda_event: Any) -> Context: + """Determine the parent context for the current Lambda invocation. + + See more: + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#determining-the-parent-of-a-span + + Args: + lambda_event: user-defined, so it could be anything, but this + method counts it being a map with a 'headers' key + Returns: + A Context with configuration found in the carrier. + """ + parent_context = None + + xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) + + if xray_env_var: + parent_context = AwsXRayPropagator().extract( + {TRACE_HEADER_KEY: xray_env_var} + ) + + if ( + parent_context + and get_current_span(parent_context) + .get_span_context() + .trace_flags.sampled + ): + return parent_context + + if event_context_extractor: + parent_context = event_context_extractor(lambda_event) + else: + parent_context = _default_event_context_extractor(lambda_event) + + return parent_context + + def _instrumented_lambda_handler_call( + call_wrapped, instance, args, kwargs + ): + orig_handler_name = ".".join( + [wrapped_module_name, wrapped_function_name] + ) + + lambda_event = args[0] + + parent_context = _determine_parent_context(lambda_event) + + tracer = get_tracer(__name__, __version__, tracer_provider) + + with tracer.start_as_current_span( + name=orig_handler_name, + context=parent_context, + kind=SpanKind.SERVER, + ) as span: + if span.is_recording(): + lambda_context = args[1] + # NOTE: The specs mention an exception here, allowing the + # `ResourceAttributes.FAAS_ID` attribute to be set as a span + # attribute instead of a resource attribute. + # + # See more: + # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/faas.md#example + span.set_attribute( + ResourceAttributes.FAAS_ID, + lambda_context.invoked_function_arn, + ) + span.set_attribute( + SpanAttributes.FAAS_EXECUTION, + lambda_context.aws_request_id, + ) + + result = call_wrapped(*args, **kwargs) + + _tracer_provider = tracer_provider or get_tracer_provider() + if hasattr(_tracer_provider, "force_flush"): + # NOTE: force_flush before function quit in case of Lambda freeze. + # Assumes we are using the OpenTelemetry SDK implementation of the + # `TracerProvider`. + _tracer_provider.force_flush() + + return result + + wrap_function_wrapper( + wrapped_module_name, + wrapped_function_name, + _instrumented_lambda_handler_call, + ) diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/package.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/package.py new file mode 100644 index 0000000000..211727b883 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/package.py @@ -0,0 +1,16 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +_instruments = () diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/version.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/version.py new file mode 100644 index 0000000000..2a05c9b361 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/src/opentelemetry/instrumentation/aws_lambda/version.py @@ -0,0 +1,15 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +__version__ = "0.25b0" diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/mocks/mock_user_lambda.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/mocks/mock_user_lambda.py new file mode 100644 index 0000000000..2761db8f06 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/mocks/mock_user_lambda.py @@ -0,0 +1,2 @@ +def handler(event, context): + return "200 ok" diff --git a/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/test_aws_lambda_instrumentation.py b/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/test_aws_lambda_instrumentation.py new file mode 100644 index 0000000000..06da52eb3e --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aws_lambda/tests/test_aws_lambda_instrumentation.py @@ -0,0 +1,319 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import sys +from importlib import import_module +from unittest import mock + +from opentelemetry.environment_variables import OTEL_PROPAGATORS +from opentelemetry.instrumentation.aws_lambda import ( + _HANDLER, + _X_AMZN_TRACE_ID, + ORIG_HANDLER, + AwsLambdaInstrumentor, +) +from opentelemetry.propagate import get_global_textmap +from opentelemetry.propagators.aws.aws_xray_propagator import ( + TRACE_ID_FIRST_PART_LENGTH, + TRACE_ID_VERSION, +) +from opentelemetry.semconv.resource import ResourceAttributes +from opentelemetry.semconv.trace import SpanAttributes +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace import SpanKind +from opentelemetry.trace.propagation.tracecontext import ( + TraceContextTextMapPropagator, +) + +AWS_LAMBDA_EXEC_WRAPPER = "AWS_LAMBDA_EXEC_WRAPPER" +CONFIGURE_OTEL_SDK_SCRIPTS_DIR = os.path.join( + *(os.path.dirname(__file__), "..", "scripts") +) + + +class MockLambdaContext: + def __init__(self, aws_request_id, invoked_function_arn): + self.invoked_function_arn = invoked_function_arn + self.aws_request_id = aws_request_id + + +MOCK_LAMBDA_CONTEXT = MockLambdaContext( + aws_request_id="mock_aws_request_id", + invoked_function_arn="arn://mock-lambda-function-arn", +) + +MOCK_XRAY_TRACE_ID = 0x5FB7331105E8BB83207FA31D4D9CDB4C +MOCK_XRAY_TRACE_ID_STR = f"{MOCK_XRAY_TRACE_ID:x}" +MOCK_XRAY_PARENT_SPAN_ID = 0x3328B8445A6DBAD2 +MOCK_XRAY_TRACE_CONTEXT_COMMON = f"Root={TRACE_ID_VERSION}-{MOCK_XRAY_TRACE_ID_STR[:TRACE_ID_FIRST_PART_LENGTH]}-{MOCK_XRAY_TRACE_ID_STR[TRACE_ID_FIRST_PART_LENGTH:]};Parent={MOCK_XRAY_PARENT_SPAN_ID:x}" +MOCK_XRAY_TRACE_CONTEXT_SAMPLED = f"{MOCK_XRAY_TRACE_CONTEXT_COMMON};Sampled=1" +MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED = ( + f"{MOCK_XRAY_TRACE_CONTEXT_COMMON};Sampled=0" +) + +# Read more: +# https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers +MOCK_W3C_TRACE_ID = 0x5CE0E9A56015FEC5AADFA328AE398115 +MOCK_W3C_PARENT_SPAN_ID = 0xAB54A98CEB1F0AD2 +MOCK_W3C_TRACE_CONTEXT_SAMPLED = ( + f"00-{MOCK_W3C_TRACE_ID:x}-{MOCK_W3C_PARENT_SPAN_ID:x}-01" +) + +MOCK_W3C_TRACE_STATE_KEY = "vendor_specific_key" +MOCK_W3C_TRACE_STATE_VALUE = "test_value" + + +def mock_aws_lambda_exec_wrapper(): + """Mocks automatically instrumenting user Lambda function by pointing + `AWS_LAMBDA_EXEC_WRAPPER` to the + `patch-with-configure_otel_sdk_on_lambda.py` script. + + TODO: It would be better if `moto`'s `mock_lambda` supported setting + AWS_LAMBDA_EXEC_WRAPPER so we could make the call to Lambda instead. + + See more: + https://aws-otel.github.io/docs/getting-started/lambda/lambda-python + """ + + with open( + os.path.join( + CONFIGURE_OTEL_SDK_SCRIPTS_DIR, + "patch-with-configure_otel_sdk_on_lambda.py", + ) + ) as config_otel_script: + exec(config_otel_script.read()) + + +def mock_execute_lambda(event=None): + """Mocks Lambda importing and then calling the method at the current + `_HANDLER` environment variable. Like the real Lambda, if + `AWS_LAMBDA_EXEC_WRAPPER` is defined, if executes that before `_HANDLER`. + + NOTE: Because this function **imports** the + `configure_otel_sdk_on_lambda.py` script, subsequent calls will just use the + cache and not call the functions in that file again. The consequence is that + the configured OTel Python SDK package stays configured and all + Instrumentors stay instrumented across different unit tests. + + See more: + https://aws-otel.github.io/docs/getting-started/lambda/lambda-python + """ + if os.environ[AWS_LAMBDA_EXEC_WRAPPER]: + globals()[os.environ[AWS_LAMBDA_EXEC_WRAPPER]]() + + # NOTE: We can only call this once because of Python import caching! + # Fortunately, the setup it performs is the same across tests. + module_name, handler_name = os.environ[_HANDLER].rsplit(".", maxsplit=1) + handler_module = import_module(module_name.replace("/", ".")) + getattr(handler_module, handler_name)(event, MOCK_LAMBDA_CONTEXT) + + +class TestAwsLambdaInstrumentor(TestBase): + """AWS Lambda Instrumentation Testsuite""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + sys.path.append(CONFIGURE_OTEL_SDK_SCRIPTS_DIR) + + def setUp(self): + super().setUp() + self.common_env_patch = mock.patch.dict( + "os.environ", + { + AWS_LAMBDA_EXEC_WRAPPER: "mock_aws_lambda_exec_wrapper", + "AWS_LAMBDA_FUNCTION_NAME": "test-python-lambda-function", + "AWS_LAMBDA_FUNCTION_VERSION": "2", + "AWS_REGION": "us-east-1", + _HANDLER: "mocks.mock_user_lambda.handler", + }, + ) + self.common_env_patch.start() + + def tearDown(self): + super().tearDown() + self.common_env_patch.stop() + # NOTE: We do not uninstrument the `AwsLambdaInstrumentor` here + # because python caching means once we import + # `configure_otel_sdk_on_lambda.py`, we cannot import it again and + # therefore cannot instrument it again. + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + sys.path.remove(CONFIGURE_OTEL_SDK_SCRIPTS_DIR) + + def test_active_tracing(self): + test_env_patch = mock.patch.dict( + "os.environ", + { + **os.environ, + # Using Active tracing + _X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_SAMPLED, + }, + ) + test_env_patch.start() + + mock_execute_lambda() + + spans = self.memory_exporter.get_finished_spans() + + assert spans + + self.assertEqual(len(spans), 1) + span = spans[0] + self.assertEqual(span.name, os.environ[ORIG_HANDLER]) + self.assertEqual(span.get_span_context().trace_id, MOCK_XRAY_TRACE_ID) + self.assertEqual(span.kind, SpanKind.SERVER) + self.assertSpanHasAttributes( + span, + { + ResourceAttributes.FAAS_ID: MOCK_LAMBDA_CONTEXT.invoked_function_arn, + SpanAttributes.FAAS_EXECUTION: MOCK_LAMBDA_CONTEXT.aws_request_id, + }, + ) + + # TODO: Waiting on OTel Python support for setting Resource Detectors + # using environment variables. Auto Instrumentation (used by this Lambda + # Instrumentation) sets up the global TracerProvider which is the only + # time Resource Detectors can be configured. + # + # We would configure this environment variable in + # `patch-with-configure_otel_sdk_on_lambda.py`. + # + # resource_atts = span.resource.attributes + # self.assertEqual(resource_atts[ResourceAttributes.CLOUD_PLATFORM], CloudPlatformValues.AWS_LAMBDA.value) + # self.assertEqual(resource_atts[ResourceAttributes.CLOUD_PROVIDER], CloudProviderValues.AWS.value) + # self.assertEqual(resource_atts[ResourceAttributes.CLOUD_REGION], os.environ["AWS_REGION"]) + # self.assertEqual(resource_atts[ResourceAttributes.FAAS_NAME], os.environ["AWS_LAMBDA_FUNCTION_NAME"]) + # self.assertEqual(resource_atts[ResourceAttributes.FAAS_VERSION], os.environ["AWS_LAMBDA_FUNCTION_VERSION"]) + + parent_context = span.parent + self.assertEqual( + parent_context.trace_id, span.get_span_context().trace_id + ) + self.assertEqual(parent_context.span_id, MOCK_XRAY_PARENT_SPAN_ID) + self.assertTrue(parent_context.is_remote) + + test_env_patch.stop() + + def test_parent_context_from_lambda_event(self): + test_env_patch = mock.patch.dict( + "os.environ", + { + **os.environ, + # NOT Active Tracing + _X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, + # NOT using the X-Ray Propagator + OTEL_PROPAGATORS: "tracecontext", + }, + ) + test_env_patch.start() + + mock_execute_lambda( + { + "headers": { + TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED, + TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2", + } + } + ) + + spans = self.memory_exporter.get_finished_spans() + + assert spans + + self.assertEqual(len(spans), 1) + span = spans[0] + self.assertEqual(span.get_span_context().trace_id, MOCK_W3C_TRACE_ID) + + parent_context = span.parent + self.assertEqual( + parent_context.trace_id, span.get_span_context().trace_id + ) + self.assertEqual(parent_context.span_id, MOCK_W3C_PARENT_SPAN_ID) + self.assertEqual(len(parent_context.trace_state), 3) + self.assertEqual( + parent_context.trace_state.get(MOCK_W3C_TRACE_STATE_KEY), + MOCK_W3C_TRACE_STATE_VALUE, + ) + self.assertTrue(parent_context.is_remote) + + test_env_patch.stop() + + def test_using_custom_extractor(self): + def custom_event_context_extractor(lambda_event): + return get_global_textmap().extract(lambda_event["foo"]["headers"]) + + test_env_patch = mock.patch.dict( + "os.environ", + { + **os.environ, + # DO NOT use `patch-with-configure_otel_sdk_on_lambda.py` + # script, resort to "manual" instrumentation below + AWS_LAMBDA_EXEC_WRAPPER: "", + # NOT Active Tracing + _X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, + # NOT using the X-Ray Propagator + OTEL_PROPAGATORS: "tracecontext", + }, + ) + test_env_patch.start() + + # NOTE: We uninstrument anticipating that other tests might have left it + # instrumented. + AwsLambdaInstrumentor().uninstrument() + + # NOTE: Instead of using `AWS_LAMBDA_EXEC_WRAPPER` to point `_HANDLER` + # to a module which instruments and calls the user `ORIG_HANDLER`, we + # leave `_HANDLER` as is and replace `AWS_LAMBDA_EXEC_WRAPPER` with this + # line below. This is like "manual" instrumentation for Lambda. + AwsLambdaInstrumentor().instrument( + event_context_extractor=custom_event_context_extractor, + ) + + mock_execute_lambda( + { + "foo": { + "headers": { + TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED, + TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2", + } + } + } + ) + + spans = self.memory_exporter.get_finished_spans() + + assert spans + + self.assertEqual(len(spans), 1) + span = spans[0] + self.assertEqual(span.get_span_context().trace_id, MOCK_W3C_TRACE_ID) + + parent_context = span.parent + self.assertEqual( + parent_context.trace_id, span.get_span_context().trace_id + ) + self.assertEqual(parent_context.span_id, MOCK_W3C_PARENT_SPAN_ID) + self.assertEqual(len(parent_context.trace_state), 3) + self.assertEqual( + parent_context.trace_state.get(MOCK_W3C_TRACE_STATE_KEY), + MOCK_W3C_TRACE_STATE_VALUE, + ) + self.assertTrue(parent_context.is_remote) + + test_env_patch.stop() + + AwsLambdaInstrumentor().uninstrument() diff --git a/tox.ini b/tox.ini index 20ac03b53e..d743fb2d9f 100644 --- a/tox.ini +++ b/tox.ini @@ -17,6 +17,9 @@ envlist = py3{6,7,8,9}-test-instrumentation-aiopg ; instrumentation-aiopg intentionally excluded from pypy3 + ; opentelemetry-instrumentation-aws_lambda + py3{6,7,8,9}-test-instrumentation-aws_lambda + ; opentelemetry-instrumentation-botocore py3{6,7,8,9}-test-instrumentation-botocore pypy3-test-instrumentation-botocore @@ -203,6 +206,7 @@ changedir = test-instrumentation-aiopg: instrumentation/opentelemetry-instrumentation-aiopg/tests test-instrumentation-asgi: instrumentation/opentelemetry-instrumentation-asgi/tests test-instrumentation-asyncpg: instrumentation/opentelemetry-instrumentation-asyncpg/tests + test-instrumentation-aws_lambda: instrumentation/opentelemetry-instrumentation-aws_lambda/tests test-instrumentation-boto: instrumentation/opentelemetry-instrumentation-boto/tests test-instrumentation-botocore: instrumentation/opentelemetry-instrumentation-botocore/tests test-instrumentation-celery: instrumentation/opentelemetry-instrumentation-celery/tests @@ -262,6 +266,8 @@ commands_pre = asyncpg: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asyncpg[test] + aws_lambda: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-aws_lambda[test] + boto: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-botocore[test] boto: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-boto[test]