From ea0cf3f8d4408cf56aa8d3be63d5fea022ad439b Mon Sep 17 00:00:00 2001 From: Jeremy Voss Date: Thu, 19 May 2022 16:03:58 -0700 Subject: [PATCH 01/16] Adding escape call to sitecustomize sub call. Fixes auto-instrumentation not producing spans on Windows. See the following issue: https://github.com/open-telemetry/opentelemetry-python/issues/2703 --- CHANGELOG.md | 3 +++ .../instrumentation/auto_instrumentation/sitecustomize.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa2badae2..0309007417 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.12.0rc1-0.31b0...HEAD) +- Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703). + ([#](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/)) + ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 25ee3fe048..37c74a2338 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -15,7 +15,7 @@ from logging import getLogger from os import environ from os.path import abspath, dirname, pathsep -from re import sub +from re import sub, escape from pkg_resources import iter_entry_points @@ -112,7 +112,7 @@ def _load_configurators(): def initialize(): # prevents auto-instrumentation of subprocesses if code execs another python process environ["PYTHONPATH"] = sub( - rf"{dirname(abspath(__file__))}{pathsep}?", + escape(rf"{dirname(abspath(__file__))}{pathsep}?"), "", environ["PYTHONPATH"], ) From 764a784e1fd1aea5528762efe6e2edaacbd50032 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Thu, 19 May 2022 16:46:19 -0700 Subject: [PATCH 02/16] Update CHANGLOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0309007417..d02d4523d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) - Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703). - ([#](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/)) + ([#1098](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1098)) ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 From 1dfd15059f8ec30d1cc7162804b4ccbe3fe659ac Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Fri, 20 May 2022 16:54:54 -0700 Subject: [PATCH 03/16] Ran tox -e generate which corrected import order --- .../instrumentation/auto_instrumentation/sitecustomize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 37c74a2338..3beb688fb5 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -15,7 +15,7 @@ from logging import getLogger from os import environ from os.path import abspath, dirname, pathsep -from re import sub, escape +from re import escape, sub from pkg_resources import iter_entry_points From b1782821cd67145903e0e9e2dd0e6d57e9f8a754 Mon Sep 17 00:00:00 2001 From: jerevoss Date: Mon, 23 May 2022 10:35:30 -0700 Subject: [PATCH 04/16] Update CHANGELOG.md Co-authored-by: Srikanth Chekuri --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02d4523d2..2847eabca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) - Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703). - ([#1098](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1098)) + ([#1100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1100)) ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 From a0b731c395928d0a7d000fb0b2e0d6dc2eca4ece Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Wed, 25 May 2022 11:56:24 -0700 Subject: [PATCH 05/16] Refactored removal of dir from PYTHONPATH into separate function --- .../instrumentation/auto_instrumentation/sitecustomize.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 3beb688fb5..c3f0967ff1 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -109,7 +109,7 @@ def _load_configurators(): raise exc -def initialize(): +def _remove_current_directory_from_python_path(): # prevents auto-instrumentation of subprocesses if code execs another python process environ["PYTHONPATH"] = sub( escape(rf"{dirname(abspath(__file__))}{pathsep}?"), @@ -117,6 +117,10 @@ def initialize(): environ["PYTHONPATH"], ) + +def initialize(): + _remove_current_directory_from_python_path() + try: distro = _load_distros() distro.configure() From 1aed29b885fd2b2f7676e8aaf04931a3f5a36a38 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Fri, 27 May 2022 12:43:59 -0700 Subject: [PATCH 06/16] Fixed bug where ? is escaped instead of just the directory path --- .../instrumentation/auto_instrumentation/sitecustomize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index c3f0967ff1..0bd78a0f1c 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -112,7 +112,7 @@ def _load_configurators(): def _remove_current_directory_from_python_path(): # prevents auto-instrumentation of subprocesses if code execs another python process environ["PYTHONPATH"] = sub( - escape(rf"{dirname(abspath(__file__))}{pathsep}?"), + rf"{escape(dirname(abspath(__file__)))}{pathsep}?", "", environ["PYTHONPATH"], ) From 25a17e6b6fee150a38c20c18e234c3e50b800e99 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Fri, 27 May 2022 14:12:21 -0700 Subject: [PATCH 07/16] Added Fixed tag that was accidentally removed it Resolve Conflicts --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6064d0a959..ed31b4efa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) +### Fixed - Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703). ([#1100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1100)) From b04bc61646a4bb66f17e86d58c9da42396c5c6e0 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Tue, 31 May 2022 16:21:52 -0700 Subject: [PATCH 08/16] Tests run. Assert statement fails --- .../auto_instrumentation/sitecustomize.py | 17 +++------- .../opentelemetry/instrumentation/utils.py | 14 ++++++++ .../tests/test_utils.py | 33 ++++++++++++++++++- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 0bd78a0f1c..4af07037e5 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -14,8 +14,8 @@ from logging import getLogger from os import environ -from os.path import abspath, dirname, pathsep -from re import escape, sub +from os.path import pathsep +from opentelemetry.instrumentation.utils import _python_path_without_current_directory from pkg_resources import iter_entry_points @@ -109,17 +109,10 @@ def _load_configurators(): raise exc -def _remove_current_directory_from_python_path(): - # prevents auto-instrumentation of subprocesses if code execs another python process - environ["PYTHONPATH"] = sub( - rf"{escape(dirname(abspath(__file__)))}{pathsep}?", - "", - environ["PYTHONPATH"], - ) - - def initialize(): - _remove_current_directory_from_python_path() + # prevents auto-instrumentation of subprocesses if code execs another python process + environ["PYTHONPATH"] = _python_path_without_current_directory(environ["PYTHONPATH"], __file__, pathsep) + print("expected_python_path = " + environ["PYTHONPATH"]) try: distro = _load_distros() diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index 56f174d758..4141917af4 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -13,6 +13,8 @@ # limitations under the License. import urllib.parse +from os.path import abspath, dirname, pathsep +from re import escape, sub from typing import Dict, Sequence from wrapt import ObjectProxy @@ -163,3 +165,15 @@ def _generate_opentelemetry_traceparent(span: Span) -> str: _traceparent = _version + "-" + _trace_id + "-" + _span_id + "-" + _flags meta.update({"traceparent": _traceparent}) return meta + +def _python_path_without_current_directory(python_path, file, path_separator): + print("file = " + file) + print("path_separator = " + path_separator) + print("python_path = " + python_path) + dir = dirname(abspath(file)) + print("dir = " + dir) + return sub( + rf"{escape(dir)}{path_separator}?", + "", + escape(python_path), + ) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index ac8b9268c3..4208279d2a 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -14,7 +14,7 @@ from http import HTTPStatus -from opentelemetry.instrumentation.utils import http_status_to_status_code +from opentelemetry.instrumentation.utils import _python_path_without_current_directory, http_status_to_status_code from opentelemetry.test.test_base import TestBase from opentelemetry.trace import StatusCode @@ -111,3 +111,34 @@ def test_http_status_to_status_code_server(self): int(status_code), server_span=True ) self.assertEqual(actual, expected, status_code) + + def test_windows(self): + test_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" + test_file = r"C:\Users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" + test_pathsep = ";" + file = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" + path_separator = r";" + python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" + test_expected_python_path = r"C:\Users\jeremyvoss\workplace" + # self._test_remove_current_directory_from_python_path_helper(test_python_path, test_file, test_pathsep, test_expected_python_path) + assert(test_expected_python_path == + _python_path_without_current_directory( + python_path, + file, + path_separator + ) + ) + + # def test_linux(self): + # test_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" + # test_file = r"C:\Users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" + # test_pathsep = ";" + # test_expected_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;" + # self._test_remove_current_directory_from_python_path_helper(test_python_path, test_file, test_pathsep, test_expected_python_path) + + # def _test_remove_current_directory_from_python_path_helper(self, test_python_path, test_file, test_pathsep, test_expected_python_path): + # assert(test_expected_python_path == + # opentelemetry.instrumentation.auto_instrumentation.sitecustomize._remove_current_directory_from_python_path( + # test_python_path, + # test_file, + # test_pathsep)) \ No newline at end of file From 5cfc06ccd7da7b326266bb325fba1cbbfd26267e Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Wed, 1 Jun 2022 11:32:00 -0700 Subject: [PATCH 09/16] Removed os-specific calls out of function so tests can run on any os --- .../auto_instrumentation/sitecustomize.py | 11 +++-- .../opentelemetry/instrumentation/utils.py | 11 ++--- .../tests/test_utils.py | 48 ++++++++----------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 4af07037e5..47575a7429 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -14,8 +14,7 @@ from logging import getLogger from os import environ -from os.path import pathsep -from opentelemetry.instrumentation.utils import _python_path_without_current_directory +from os.path import abspath, dirname, pathsep from pkg_resources import iter_entry_points @@ -26,6 +25,9 @@ from opentelemetry.instrumentation.environment_variables import ( OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, ) +from opentelemetry.instrumentation.utils import ( + _python_path_without_current_directory, +) from opentelemetry.instrumentation.version import __version__ logger = getLogger(__name__) @@ -111,8 +113,9 @@ def _load_configurators(): def initialize(): # prevents auto-instrumentation of subprocesses if code execs another python process - environ["PYTHONPATH"] = _python_path_without_current_directory(environ["PYTHONPATH"], __file__, pathsep) - print("expected_python_path = " + environ["PYTHONPATH"]) + environ["PYTHONPATH"] = _python_path_without_current_directory( + environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep + ) try: distro = _load_distros() diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index 4141917af4..8af8c0d949 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -13,7 +13,6 @@ # limitations under the License. import urllib.parse -from os.path import abspath, dirname, pathsep from re import escape, sub from typing import Dict, Sequence @@ -166,14 +165,10 @@ def _generate_opentelemetry_traceparent(span: Span) -> str: meta.update({"traceparent": _traceparent}) return meta -def _python_path_without_current_directory(python_path, file, path_separator): - print("file = " + file) - print("path_separator = " + path_separator) - print("python_path = " + python_path) - dir = dirname(abspath(file)) - print("dir = " + dir) + +def _python_path_without_current_directory(python_path, dir, path_separator): return sub( rf"{escape(dir)}{path_separator}?", "", - escape(python_path), + python_path, ) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 4208279d2a..c9a06ed7e0 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -112,33 +112,27 @@ def test_http_status_to_status_code_server(self): ) self.assertEqual(actual, expected, status_code) - def test_windows(self): - test_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" - test_file = r"C:\Users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" - test_pathsep = ";" - file = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" + def test_remove_current_directory_from_python_path_windows(self): + dir = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" path_separator = r";" - python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" - test_expected_python_path = r"C:\Users\jeremyvoss\workplace" - # self._test_remove_current_directory_from_python_path_helper(test_python_path, test_file, test_pathsep, test_expected_python_path) - assert(test_expected_python_path == - _python_path_without_current_directory( - python_path, - file, - path_separator - ) + python_path = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace" + actual_python_path = _python_path_without_current_directory( + python_path, + dir, + path_separator + ) + expected_python_path = r"C:\Users\trayvonmartin\workplace" + assert(expected_python_path == actual_python_path ) - # def test_linux(self): - # test_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\jeremyvoss\workplace" - # test_file = r"C:\Users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation\sitecustomize.py" - # test_pathsep = ";" - # test_expected_python_path = r"c:\users\jeremyvoss\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;" - # self._test_remove_current_directory_from_python_path_helper(test_python_path, test_file, test_pathsep, test_expected_python_path) - - # def _test_remove_current_directory_from_python_path_helper(self, test_python_path, test_file, test_pathsep, test_expected_python_path): - # assert(test_expected_python_path == - # opentelemetry.instrumentation.auto_instrumentation.sitecustomize._remove_current_directory_from_python_path( - # test_python_path, - # test_file, - # test_pathsep)) \ No newline at end of file + def test_remove_current_directory_from_python_path_linux(self): + dir = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + path_separator = r":" + python_path = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation:/home/georgefloyd/workplace" + actual_python_path = _python_path_without_current_directory( + python_path, + dir, + path_separator + ) + expected_python_path = r"/home/georgefloyd/workplace" + assert(expected_python_path == actual_python_path) From 15580e575fa68f22e8dd73039f383c4f2de96875 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Wed, 1 Jun 2022 11:32:00 -0700 Subject: [PATCH 10/16] Removed os-specific calls out of function so tests can run on any os Fixed negative look ahead to prevent removing path when it is the only one. --- .../auto_instrumentation/sitecustomize.py | 6 +-- .../opentelemetry/instrumentation/utils.py | 4 +- .../tests/test_utils.py | 44 +++++++++++++------ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 47575a7429..9504e359af 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -25,9 +25,7 @@ from opentelemetry.instrumentation.environment_variables import ( OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, ) -from opentelemetry.instrumentation.utils import ( - _python_path_without_current_directory, -) +from opentelemetry.instrumentation.utils import _python_path_without_directory from opentelemetry.instrumentation.version import __version__ logger = getLogger(__name__) @@ -113,7 +111,7 @@ def _load_configurators(): def initialize(): # prevents auto-instrumentation of subprocesses if code execs another python process - environ["PYTHONPATH"] = _python_path_without_current_directory( + environ["PYTHONPATH"] = _python_path_without_directory( environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep ) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index 8af8c0d949..b30092e6ac 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -166,9 +166,9 @@ def _generate_opentelemetry_traceparent(span: Span) -> str: return meta -def _python_path_without_current_directory(python_path, dir, path_separator): +def _python_path_without_directory(python_path, dir, path_separator): return sub( - rf"{escape(dir)}{path_separator}?", + rf"{escape(dir)}{path_separator}(?!$)", "", python_path, ) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index c9a06ed7e0..946ab12fc6 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -14,7 +14,10 @@ from http import HTTPStatus -from opentelemetry.instrumentation.utils import _python_path_without_current_directory, http_status_to_status_code +from opentelemetry.instrumentation.utils import ( + _python_path_without_directory, + http_status_to_status_code, +) from opentelemetry.test.test_base import TestBase from opentelemetry.trace import StatusCode @@ -113,26 +116,39 @@ def test_http_status_to_status_code_server(self): self.assertEqual(actual, expected, status_code) def test_remove_current_directory_from_python_path_windows(self): - dir = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" + dir = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" path_separator = r";" - python_path = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace" - actual_python_path = _python_path_without_current_directory( - python_path, - dir, - path_separator + python_path = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace" + actual_python_path = _python_path_without_directory( + python_path, dir, path_separator ) expected_python_path = r"C:\Users\trayvonmartin\workplace" - assert(expected_python_path == actual_python_path - ) + self.assertEqual(actual_python_path, expected_python_path) def test_remove_current_directory_from_python_path_linux(self): dir = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" path_separator = r":" python_path = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation:/home/georgefloyd/workplace" - actual_python_path = _python_path_without_current_directory( - python_path, - dir, - path_separator + actual_python_path = _python_path_without_directory( + python_path, dir, path_separator ) expected_python_path = r"/home/georgefloyd/workplace" - assert(expected_python_path == actual_python_path) + self.assertEqual(actual_python_path, expected_python_path) + + def test_remove_current_directory_from_python_path_windows_only_path(self): + dir = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" + path_separator = r";" + python_path = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" + actual_python_path = _python_path_without_directory( + python_path, dir, path_separator + ) + self.assertEqual(actual_python_path, python_path) + + def test_remove_current_directory_from_python_path_linux_only_path(self): + dir = r"/home/sandranland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + path_separator = r":" + python_path = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + actual_python_path = _python_path_without_directory( + python_path, dir, path_separator + ) + self.assertEqual(actual_python_path, python_path) \ No newline at end of file From e6c5b3a3a312912b2c4a172b5f99f6d505c5c49b Mon Sep 17 00:00:00 2001 From: "Paul \"TBBle\" Hampson" Date: Fri, 27 May 2022 05:48:24 +1000 Subject: [PATCH 11/16] Narrow protobuf dependency to exclude protobuf >= 4 (#1109) --- CHANGELOG.md | 1 - opentelemetry-instrumentation/tests/test_utils.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e26131618..99adbac3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703). ([#1100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1100)) - - `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4 ([1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109)) - cleanup type hints for textmap `Getter` and `Setter` classes diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 946ab12fc6..461c2fefa2 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -151,4 +151,5 @@ def test_remove_current_directory_from_python_path_linux_only_path(self): actual_python_path = _python_path_without_directory( python_path, dir, path_separator ) - self.assertEqual(actual_python_path, python_path) \ No newline at end of file + self.assertEqual(actual_python_path, python_path) + \ No newline at end of file From cd66c715697cbc1be5259795a7808a04026b9c51 Mon Sep 17 00:00:00 2001 From: Alan Isaac Date: Sat, 28 May 2022 20:32:14 -0400 Subject: [PATCH 12/16] updated type hints for getters and setters (#1106) --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99adbac3eb..c4bd480bbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109)) - cleanup type hints for textmap `Getter` and `Setter` classes ([1106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1106)) +<<<<<<< HEAD ### Added - `opentelemetry-instrumentation-remoulade` Initial release ([#1082](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1082)) +======= +>>>>>>> b4440e8d (updated type hints for getters and setters (#1106)) ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 From 17fbb6df0529b2fecabe50c43828c00a67c975ec Mon Sep 17 00:00:00 2001 From: jerevoss Date: Thu, 2 Jun 2022 13:05:24 -0700 Subject: [PATCH 13/16] Update CHANGELOG.md Co-authored-by: Srikanth Chekuri --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4bd480bbf..99adbac3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,13 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109)) - cleanup type hints for textmap `Getter` and `Setter` classes ([1106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1106)) -<<<<<<< HEAD ### Added - `opentelemetry-instrumentation-remoulade` Initial release ([#1082](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1082)) -======= ->>>>>>> b4440e8d (updated type hints for getters and setters (#1106)) ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 From 66a8c2d9fc99179533911b30b7bb57ed401e9732 Mon Sep 17 00:00:00 2001 From: jerevoss Date: Thu, 2 Jun 2022 13:30:28 -0700 Subject: [PATCH 14/16] Update opentelemetry-instrumentation/tests/test_utils.py Co-authored-by: Srikanth Chekuri --- opentelemetry-instrumentation/tests/test_utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 461c2fefa2..946ab12fc6 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -151,5 +151,4 @@ def test_remove_current_directory_from_python_path_linux_only_path(self): actual_python_path = _python_path_without_directory( python_path, dir, path_separator ) - self.assertEqual(actual_python_path, python_path) - \ No newline at end of file + self.assertEqual(actual_python_path, python_path) \ No newline at end of file From fea8e67922afe32ac955f5023b9000a8cc8d0055 Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Thu, 2 Jun 2022 15:29:45 -0700 Subject: [PATCH 15/16] Fixing newline at end of file and test typo --- opentelemetry-instrumentation/tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 946ab12fc6..a34c67163b 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -145,10 +145,10 @@ def test_remove_current_directory_from_python_path_windows_only_path(self): self.assertEqual(actual_python_path, python_path) def test_remove_current_directory_from_python_path_linux_only_path(self): - dir = r"/home/sandranland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + dir = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" path_separator = r":" python_path = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" actual_python_path = _python_path_without_directory( python_path, dir, path_separator ) - self.assertEqual(actual_python_path, python_path) \ No newline at end of file + self.assertEqual(actual_python_path, python_path) From a9d2e8660fc10549038218d3fc4fc2049ab1ba2e Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Fri, 3 Jun 2022 11:37:03 -0700 Subject: [PATCH 16/16] Fixing redifinition of built-in dir --- .../src/opentelemetry/instrumentation/utils.py | 4 ++-- .../tests/test_utils.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index b30092e6ac..fea7608388 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -166,9 +166,9 @@ def _generate_opentelemetry_traceparent(span: Span) -> str: return meta -def _python_path_without_directory(python_path, dir, path_separator): +def _python_path_without_directory(python_path, directory, path_separator): return sub( - rf"{escape(dir)}{path_separator}(?!$)", + rf"{escape(directory)}{path_separator}(?!$)", "", python_path, ) diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index a34c67163b..b7c9ecdc6e 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -116,39 +116,39 @@ def test_http_status_to_status_code_server(self): self.assertEqual(actual, expected, status_code) def test_remove_current_directory_from_python_path_windows(self): - dir = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" + directory = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" path_separator = r";" python_path = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace" actual_python_path = _python_path_without_directory( - python_path, dir, path_separator + python_path, directory, path_separator ) expected_python_path = r"C:\Users\trayvonmartin\workplace" self.assertEqual(actual_python_path, expected_python_path) def test_remove_current_directory_from_python_path_linux(self): - dir = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + directory = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" path_separator = r":" python_path = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation:/home/georgefloyd/workplace" actual_python_path = _python_path_without_directory( - python_path, dir, path_separator + python_path, directory, path_separator ) expected_python_path = r"/home/georgefloyd/workplace" self.assertEqual(actual_python_path, expected_python_path) def test_remove_current_directory_from_python_path_windows_only_path(self): - dir = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" + directory = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" path_separator = r";" python_path = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation" actual_python_path = _python_path_without_directory( - python_path, dir, path_separator + python_path, directory, path_separator ) self.assertEqual(actual_python_path, python_path) def test_remove_current_directory_from_python_path_linux_only_path(self): - dir = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" + directory = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" path_separator = r":" python_path = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation" actual_python_path = _python_path_without_directory( - python_path, dir, path_separator + python_path, directory, path_separator ) self.assertEqual(actual_python_path, python_path)