Skip to content

Commit 15580e5

Browse files
committed
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.
1 parent 5cfc06c commit 15580e5

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
from opentelemetry.instrumentation.environment_variables import (
2626
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
2727
)
28-
from opentelemetry.instrumentation.utils import (
29-
_python_path_without_current_directory,
30-
)
28+
from opentelemetry.instrumentation.utils import _python_path_without_directory
3129
from opentelemetry.instrumentation.version import __version__
3230

3331
logger = getLogger(__name__)
@@ -113,7 +111,7 @@ def _load_configurators():
113111

114112
def initialize():
115113
# prevents auto-instrumentation of subprocesses if code execs another python process
116-
environ["PYTHONPATH"] = _python_path_without_current_directory(
114+
environ["PYTHONPATH"] = _python_path_without_directory(
117115
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
118116
)
119117

opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def _generate_opentelemetry_traceparent(span: Span) -> str:
166166
return meta
167167

168168

169-
def _python_path_without_current_directory(python_path, dir, path_separator):
169+
def _python_path_without_directory(python_path, dir, path_separator):
170170
return sub(
171-
rf"{escape(dir)}{path_separator}?",
171+
rf"{escape(dir)}{path_separator}(?!$)",
172172
"",
173173
python_path,
174174
)

opentelemetry-instrumentation/tests/test_utils.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
from http import HTTPStatus
1616

17-
from opentelemetry.instrumentation.utils import _python_path_without_current_directory, http_status_to_status_code
17+
from opentelemetry.instrumentation.utils import (
18+
_python_path_without_directory,
19+
http_status_to_status_code,
20+
)
1821
from opentelemetry.test.test_base import TestBase
1922
from opentelemetry.trace import StatusCode
2023

@@ -113,26 +116,39 @@ def test_http_status_to_status_code_server(self):
113116
self.assertEqual(actual, expected, status_code)
114117

115118
def test_remove_current_directory_from_python_path_windows(self):
116-
dir = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
119+
dir = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
117120
path_separator = r";"
118-
python_path = r"c:\users\trayvonmartin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace"
119-
actual_python_path = _python_path_without_current_directory(
120-
python_path,
121-
dir,
122-
path_separator
121+
python_path = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace"
122+
actual_python_path = _python_path_without_directory(
123+
python_path, dir, path_separator
123124
)
124125
expected_python_path = r"C:\Users\trayvonmartin\workplace"
125-
assert(expected_python_path == actual_python_path
126-
)
126+
self.assertEqual(actual_python_path, expected_python_path)
127127

128128
def test_remove_current_directory_from_python_path_linux(self):
129129
dir = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
130130
path_separator = r":"
131131
python_path = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation:/home/georgefloyd/workplace"
132-
actual_python_path = _python_path_without_current_directory(
133-
python_path,
134-
dir,
135-
path_separator
132+
actual_python_path = _python_path_without_directory(
133+
python_path, dir, path_separator
136134
)
137135
expected_python_path = r"/home/georgefloyd/workplace"
138-
assert(expected_python_path == actual_python_path)
136+
self.assertEqual(actual_python_path, expected_python_path)
137+
138+
def test_remove_current_directory_from_python_path_windows_only_path(self):
139+
dir = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
140+
path_separator = r";"
141+
python_path = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
142+
actual_python_path = _python_path_without_directory(
143+
python_path, dir, path_separator
144+
)
145+
self.assertEqual(actual_python_path, python_path)
146+
147+
def test_remove_current_directory_from_python_path_linux_only_path(self):
148+
dir = r"/home/sandranland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
149+
path_separator = r":"
150+
python_path = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
151+
actual_python_path = _python_path_without_directory(
152+
python_path, dir, path_separator
153+
)
154+
self.assertEqual(actual_python_path, python_path)

0 commit comments

Comments
 (0)