From 2665fb640d277eda773070e3a01d55e95edd5957 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 17 Feb 2020 17:21:17 -0600 Subject: [PATCH] Several fixes --- opentelemetry-api/setup.py | 7 -- .../src/opentelemetry/patcher/__init__.py | 78 ------------------- .../examples}/README.md | 0 .../examples}/formatter.py | 0 .../examples}/hello.py | 0 .../examples}/publisher_instrumented.py | 0 .../examples}/publisher_uninstrumented.py | 0 .../examples}/utils.py | 0 opentelemetry-auto-instrumentation/setup.py | 15 +--- .../auto_instrumentation/auto_instrument.py | 8 +- ...init__.py => test_auto_instrumentation.py} | 14 ++++ .../tests/{patcher => }/test_patcher.py | 2 +- 12 files changed, 22 insertions(+), 102 deletions(-) delete mode 100644 opentelemetry-api/src/opentelemetry/patcher/__init__.py rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/README.md (100%) rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/formatter.py (100%) rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/hello.py (100%) rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/publisher_instrumented.py (100%) rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/publisher_uninstrumented.py (100%) rename {examples/auto_instrumentation => opentelemetry-auto-instrumentation/examples}/utils.py (100%) rename opentelemetry-auto-instrumentation/tests/{patcher/__init__.py => test_auto_instrumentation.py} (63%) rename opentelemetry-auto-instrumentation/tests/{patcher => }/test_patcher.py (95%) diff --git a/opentelemetry-api/setup.py b/opentelemetry-api/setup.py index 85b584d3385..ee8adf26aeb 100644 --- a/opentelemetry-api/setup.py +++ b/opentelemetry-api/setup.py @@ -40,13 +40,6 @@ "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", ], - entry_points={ - "console_scripts": [ - "opentelemetry-auto-instrument = " - "opentelemetry.auto_instrument.auto_instrument:run" - ], - "opentelemetry_patcher": [], - }, description="OpenTelemetry Python API", include_package_data=True, long_description=open("README.rst").read(), diff --git a/opentelemetry-api/src/opentelemetry/patcher/__init__.py b/opentelemetry-api/src/opentelemetry/patcher/__init__.py deleted file mode 100644 index 4e5627824f0..00000000000 --- a/opentelemetry-api/src/opentelemetry/patcher/__init__.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2019, 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. -# type: ignore - -""" -OpenTelemetry patcher - -This includes the base patcher class and a no-op implementation. -""" - -from abc import ABC, abstractmethod -from logging import getLogger - -_LOG = getLogger(__name__) - - -class BasePatcher(ABC): - """An ABC for patchers""" - - @abstractmethod - def _patch(self) -> None: - """Patch""" - - @abstractmethod - def _unpatch(self) -> None: - """Unpatch""" - - def patch(self) -> None: - """Patch""" - - if not hasattr(self, "_is_patched"): - self._is_patched = ( # pylint: disable=attribute-defined-outside-init - False - ) - - if not self._is_patched: - result = self._patch() - self._is_patched = ( # pylint: disable=attribute-defined-outside-init - True - ) - return result - - _LOG.warning("Attempting to patch while already patched") - - return None - - def unpatch(self) -> None: - """Unpatch""" - - if not hasattr(self, "_is_patched"): - self._is_patched = ( # pylint: disable=attribute-defined-outside-init - False - ) - - if self._is_patched: - result = self._unpatch() - self._is_patched = ( # pylint: disable=attribute-defined-outside-init - False - ) - return result - - _LOG.warning("Attempting to unpatch while already unpatched") - - return None - - -__all__ = ["BasePatcher"] diff --git a/examples/auto_instrumentation/README.md b/opentelemetry-auto-instrumentation/examples/README.md similarity index 100% rename from examples/auto_instrumentation/README.md rename to opentelemetry-auto-instrumentation/examples/README.md diff --git a/examples/auto_instrumentation/formatter.py b/opentelemetry-auto-instrumentation/examples/formatter.py similarity index 100% rename from examples/auto_instrumentation/formatter.py rename to opentelemetry-auto-instrumentation/examples/formatter.py diff --git a/examples/auto_instrumentation/hello.py b/opentelemetry-auto-instrumentation/examples/hello.py similarity index 100% rename from examples/auto_instrumentation/hello.py rename to opentelemetry-auto-instrumentation/examples/hello.py diff --git a/examples/auto_instrumentation/publisher_instrumented.py b/opentelemetry-auto-instrumentation/examples/publisher_instrumented.py similarity index 100% rename from examples/auto_instrumentation/publisher_instrumented.py rename to opentelemetry-auto-instrumentation/examples/publisher_instrumented.py diff --git a/examples/auto_instrumentation/publisher_uninstrumented.py b/opentelemetry-auto-instrumentation/examples/publisher_uninstrumented.py similarity index 100% rename from examples/auto_instrumentation/publisher_uninstrumented.py rename to opentelemetry-auto-instrumentation/examples/publisher_uninstrumented.py diff --git a/examples/auto_instrumentation/utils.py b/opentelemetry-auto-instrumentation/examples/utils.py similarity index 100% rename from examples/auto_instrumentation/utils.py rename to opentelemetry-auto-instrumentation/examples/utils.py diff --git a/opentelemetry-auto-instrumentation/setup.py b/opentelemetry-auto-instrumentation/setup.py index fbe81014974..4431193dd2b 100644 --- a/opentelemetry-auto-instrumentation/setup.py +++ b/opentelemetry-auto-instrumentation/setup.py @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# Copyright 2019, OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ BASE_DIR = os.path.dirname(__file__) VERSION_FILENAME = os.path.join( - BASE_DIR, "src", "opentelemetry", , "version.py" + BASE_DIR, "src", "opentelemetry", "auto_instrumentation", "version.py" ) PACKAGE_INFO = {} with open(VERSION_FILENAME) as f: @@ -40,23 +40,16 @@ "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", ], - entry_points={ - "console_scripts": [ - "opentelemetry-auto-instrument = " - "opentelemetry.auto_instrument.auto_instrument:run" - ], - "opentelemetry_patcher": [], - }, description="OpenTelemetry Python Auto Instrumentation", include_package_data=True, long_description=open("README.rst").read(), long_description_content_type="text/x-rst", - install_requires=["typing; python_version<'3.5'"], + install_requires=["opentelemetry-api==0.4.dev0"], extras_require={}, license="Apache-2.0", package_dir={"": "src"}, packages=setuptools.find_namespace_packages( - where="src", include="opentelemetry.*" + where="src", include="opentelemetry.auto_instrumentation.*" ), url=( "https://github.com/open-telemetry/opentelemetry-python" diff --git a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrument.py b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrument.py index a2b231b6c4b..9a73723553f 100755 --- a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrument.py +++ b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrument.py @@ -25,12 +25,10 @@ def run() -> None: bootstrap_dir = dirname(__file__) python_path = environ.get("PYTHONPATH", None) - # Add our bootstrap directory to the head of $PYTHONPATH to ensure - # it is loaded before program code - if python_path is not None: - environ["PYTHONPATH"] = join(bootstrap_dir, python_path) - else: + if python_path is None: environ["PYTHONPATH"] = bootstrap_dir + else: + environ["PYTHONPATH"] = join(bootstrap_dir, python_path) python3 = which(argv[1]) execl(python3, python3, *argv[2:]) # type: ignore diff --git a/opentelemetry-auto-instrumentation/tests/patcher/__init__.py b/opentelemetry-auto-instrumentation/tests/test_auto_instrumentation.py similarity index 63% rename from opentelemetry-auto-instrumentation/tests/patcher/__init__.py rename to opentelemetry-auto-instrumentation/tests/test_auto_instrumentation.py index 6ab2e961ec4..1d087dbd8c5 100644 --- a/opentelemetry-auto-instrumentation/tests/patcher/__init__.py +++ b/opentelemetry-auto-instrumentation/tests/test_auto_instrumentation.py @@ -11,3 +11,17 @@ # 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. +# type: ignore + +from unittest import TestCase +from unittest.mock import patch +import sys + +from opentelemetry.auto_instrumentation.auto_instrument import run + + +class TestAutoInstrumentation(TestCase): + def test_auto_instrumentation(self): + + with patch.object(sys, "argv", ["python3", "hello.py"]): + auto_instrument.run() diff --git a/opentelemetry-auto-instrumentation/tests/patcher/test_patcher.py b/opentelemetry-auto-instrumentation/tests/test_patcher.py similarity index 95% rename from opentelemetry-auto-instrumentation/tests/patcher/test_patcher.py rename to opentelemetry-auto-instrumentation/tests/test_patcher.py index 1572323b1c3..8e9e45b5d3e 100644 --- a/opentelemetry-auto-instrumentation/tests/patcher/test_patcher.py +++ b/opentelemetry-auto-instrumentation/tests/test_patcher.py @@ -16,7 +16,7 @@ from logging import WARNING from unittest import TestCase -from opentelemetry.patcher import BasePatcher +from opentelemetry.auto_instrumentation.patcher import BasePatcher class TestPatcher(TestCase):