Skip to content

Commit 8afbce7

Browse files
authoredJun 3, 2022
Fixing inescapable character bug for Windows path string (#1100)
1 parent a8b9829 commit 8afbce7

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD)
99

1010
### Fixed
11+
- Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703).
12+
([#1100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1100))
1113
- `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4
1214
([1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109))
1315
- cleanup type hints for textmap `Getter` and `Setter` classes

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from logging import getLogger
1616
from os import environ
1717
from os.path import abspath, dirname, pathsep
18-
from re import sub
1918

2019
from pkg_resources import iter_entry_points
2120

@@ -26,6 +25,7 @@
2625
from opentelemetry.instrumentation.environment_variables import (
2726
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
2827
)
28+
from opentelemetry.instrumentation.utils import _python_path_without_directory
2929
from opentelemetry.instrumentation.version import __version__
3030

3131
logger = getLogger(__name__)
@@ -111,10 +111,8 @@ def _load_configurators():
111111

112112
def initialize():
113113
# prevents auto-instrumentation of subprocesses if code execs another python process
114-
environ["PYTHONPATH"] = sub(
115-
rf"{dirname(abspath(__file__))}{pathsep}?",
116-
"",
117-
environ["PYTHONPATH"],
114+
environ["PYTHONPATH"] = _python_path_without_directory(
115+
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
118116
)
119117

120118
try:

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

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import urllib.parse
16+
from re import escape, sub
1617
from typing import Dict, Sequence
1718

1819
from wrapt import ObjectProxy
@@ -163,3 +164,11 @@ def _generate_opentelemetry_traceparent(span: Span) -> str:
163164
_traceparent = _version + "-" + _trace_id + "-" + _span_id + "-" + _flags
164165
meta.update({"traceparent": _traceparent})
165166
return meta
167+
168+
169+
def _python_path_without_directory(python_path, directory, path_separator):
170+
return sub(
171+
rf"{escape(directory)}{path_separator}(?!$)",
172+
"",
173+
python_path,
174+
)

‎opentelemetry-instrumentation/tests/test_utils.py

+42-1
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 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

@@ -111,3 +114,41 @@ def test_http_status_to_status_code_server(self):
111114
int(status_code), server_span=True
112115
)
113116
self.assertEqual(actual, expected, status_code)
117+
118+
def test_remove_current_directory_from_python_path_windows(self):
119+
directory = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
120+
path_separator = r";"
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, directory, path_separator
124+
)
125+
expected_python_path = r"C:\Users\trayvonmartin\workplace"
126+
self.assertEqual(actual_python_path, expected_python_path)
127+
128+
def test_remove_current_directory_from_python_path_linux(self):
129+
directory = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
130+
path_separator = r":"
131+
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_directory(
133+
python_path, directory, path_separator
134+
)
135+
expected_python_path = r"/home/georgefloyd/workplace"
136+
self.assertEqual(actual_python_path, expected_python_path)
137+
138+
def test_remove_current_directory_from_python_path_windows_only_path(self):
139+
directory = 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, directory, 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+
directory = r"/home/SandraBland/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, directory, path_separator
153+
)
154+
self.assertEqual(actual_python_path, python_path)

0 commit comments

Comments
 (0)
Please sign in to comment.