Skip to content

Commit f5ef197

Browse files
authored
Merge branch 'main' into teach-unwrap-about-strings
2 parents 7a76656 + 5145a07 commit f5ef197

File tree

5 files changed

+111
-26
lines changed

5 files changed

+111
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8282
([#2753](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2753))
8383
- `opentelemetry-instrumentation-grpc` Fix grpc supported version
8484
([#2845](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2845))
85+
- `opentelemetry-instrumentation-asyncio` fix `AttributeError` in
86+
`AsyncioInstrumentor.trace_to_thread` when `func` is a `functools.partial` instance
87+
([#2911](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2911))
8588

8689
## Version 1.26.0/0.47b0 (2024-07-23)
8790

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def func():
7878
"""
7979

8080
import asyncio
81+
import functools
8182
import sys
8283
from asyncio import futures
8384
from timeit import default_timer
@@ -231,14 +232,15 @@ def wrap_taskgroup_create_task(method, instance, args, kwargs) -> None:
231232
def trace_to_thread(self, func: callable):
232233
"""Trace a function."""
233234
start = default_timer()
235+
func_name = getattr(func, "__name__", None)
236+
if func_name is None and isinstance(func, functools.partial):
237+
func_name = func.func.__name__
234238
span = (
235-
self._tracer.start_span(
236-
f"{ASYNCIO_PREFIX} to_thread-" + func.__name__
237-
)
238-
if func.__name__ in self._to_thread_name_to_trace
239+
self._tracer.start_span(f"{ASYNCIO_PREFIX} to_thread-" + func_name)
240+
if func_name in self._to_thread_name_to_trace
239241
else None
240242
)
241-
attr = {"type": "to_thread", "name": func.__name__}
243+
attr = {"type": "to_thread", "name": func_name}
242244
exception = None
243245
try:
244246
attr["state"] = "finished"

instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_to_thread.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import asyncio
15+
import functools
1516
import sys
1617
from unittest import skipIf
1718
from unittest.mock import patch
@@ -72,3 +73,37 @@ async def to_thread():
7273
for point in metric.data.data_points:
7374
self.assertEqual(point.attributes["type"], "to_thread")
7475
self.assertEqual(point.attributes["name"], "multiply")
76+
77+
@skipIf(
78+
sys.version_info < (3, 9), "to_thread is only available in Python 3.9+"
79+
)
80+
def test_to_thread_partial_func(self):
81+
def multiply(x, y):
82+
return x * y
83+
84+
double = functools.partial(multiply, 2)
85+
86+
async def to_thread():
87+
result = await asyncio.to_thread(double, 3)
88+
assert result == 6
89+
90+
with self._tracer.start_as_current_span("root"):
91+
asyncio.run(to_thread())
92+
spans = self.memory_exporter.get_finished_spans()
93+
94+
self.assertEqual(len(spans), 2)
95+
assert spans[0].name == "asyncio to_thread-multiply"
96+
for metric in (
97+
self.memory_metrics_reader.get_metrics_data()
98+
.resource_metrics[0]
99+
.scope_metrics[0]
100+
.metrics
101+
):
102+
if metric.name == "asyncio.process.duration":
103+
for point in metric.data.data_points:
104+
self.assertEqual(point.attributes["type"], "to_thread")
105+
self.assertEqual(point.attributes["name"], "multiply")
106+
if metric.name == "asyncio.process.created":
107+
for point in metric.data.data_points:
108+
self.assertEqual(point.attributes["type"], "to_thread")
109+
self.assertEqual(point.attributes["name"], "multiply")

opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
SubprocessError,
2323
check_call,
2424
)
25+
from typing import Optional
2526

2627
from packaging.requirements import Requirement
2728

2829
from opentelemetry.instrumentation.bootstrap_gen import (
29-
default_instrumentations,
30-
libraries,
30+
default_instrumentations as gen_default_instrumentations,
31+
)
32+
from opentelemetry.instrumentation.bootstrap_gen import (
33+
libraries as gen_libraries,
3134
)
3235
from opentelemetry.instrumentation.version import __version__
3336
from opentelemetry.util._importlib_metadata import (
@@ -75,7 +78,7 @@ def _sys_pip_install(package):
7578
print(error)
7679

7780

78-
def _pip_check():
81+
def _pip_check(libraries):
7982
"""Ensures none of the instrumentations have dependency conflicts.
8083
Clean check reported as:
8184
'No broken requirements found.'
@@ -113,7 +116,7 @@ def _is_installed(req):
113116
return True
114117

115118

116-
def _find_installed_libraries():
119+
def _find_installed_libraries(default_instrumentations, libraries):
117120
for lib in default_instrumentations:
118121
yield lib
119122

@@ -122,18 +125,25 @@ def _find_installed_libraries():
122125
yield lib["instrumentation"]
123126

124127

125-
def _run_requirements():
128+
def _run_requirements(default_instrumentations, libraries):
126129
logger.setLevel(logging.ERROR)
127-
print("\n".join(_find_installed_libraries()))
130+
print(
131+
"\n".join(
132+
_find_installed_libraries(default_instrumentations, libraries)
133+
)
134+
)
128135

129136

130-
def _run_install():
131-
for lib in _find_installed_libraries():
137+
def _run_install(default_instrumentations, libraries):
138+
for lib in _find_installed_libraries(default_instrumentations, libraries):
132139
_sys_pip_install(lib)
133-
_pip_check()
140+
_pip_check(libraries)
134141

135142

136-
def run() -> None:
143+
def run(
144+
default_instrumentations: Optional[list] = None,
145+
libraries: Optional[list] = None,
146+
) -> None:
137147
action_install = "install"
138148
action_requirements = "requirements"
139149

@@ -163,8 +173,14 @@ def run() -> None:
163173
)
164174
args = parser.parse_args()
165175

176+
if libraries is None:
177+
libraries = gen_libraries
178+
179+
if default_instrumentations is None:
180+
default_instrumentations = gen_default_instrumentations
181+
166182
cmd = {
167183
action_install: _run_install,
168184
action_requirements: _run_requirements,
169185
}[args.action]
170-
cmd()
186+
cmd(default_instrumentations, libraries)

opentelemetry-instrumentation/tests/test_bootstrap.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
from unittest.mock import call, patch
2020

2121
from opentelemetry.instrumentation import bootstrap
22-
from opentelemetry.instrumentation.bootstrap_gen import libraries
22+
from opentelemetry.instrumentation.bootstrap_gen import (
23+
default_instrumentations,
24+
libraries,
25+
)
2326

2427

2528
def sample_packages(packages, rate):
@@ -56,15 +59,15 @@ def setUpClass(cls):
5659
"opentelemetry.instrumentation.bootstrap._pip_check",
5760
)
5861

59-
cls.pkg_patcher.start()
60-
cls.mock_pip_install = cls.pip_install_patcher.start()
61-
cls.mock_pip_check = cls.pip_check_patcher.start()
62+
def setUp(self):
63+
super().setUp()
64+
self.mock_pip_check = self.pip_check_patcher.start()
65+
self.mock_pip_install = self.pip_install_patcher.start()
6266

63-
@classmethod
64-
def tearDownClass(cls):
65-
cls.pip_check_patcher.start()
66-
cls.pip_install_patcher.start()
67-
cls.pkg_patcher.stop()
67+
def tearDown(self):
68+
super().tearDown()
69+
self.pip_check_patcher.stop()
70+
self.pip_install_patcher.stop()
6871

6972
@patch("sys.argv", ["bootstrap", "-a", "pipenv"])
7073
def test_run_unknown_cmd(self):
@@ -73,18 +76,44 @@ def test_run_unknown_cmd(self):
7376

7477
@patch("sys.argv", ["bootstrap", "-a", "requirements"])
7578
def test_run_cmd_print(self):
79+
self.pkg_patcher.start()
7680
with patch("sys.stdout", new=StringIO()) as fake_out:
7781
bootstrap.run()
7882
self.assertEqual(
7983
fake_out.getvalue(),
8084
"\n".join(self.installed_libraries) + "\n",
8185
)
86+
self.pkg_patcher.stop()
8287

8388
@patch("sys.argv", ["bootstrap", "-a", "install"])
8489
def test_run_cmd_install(self):
90+
self.pkg_patcher.start()
8591
bootstrap.run()
8692
self.mock_pip_install.assert_has_calls(
8793
[call(i) for i in self.installed_libraries],
8894
any_order=True,
8995
)
90-
self.assertEqual(self.mock_pip_check.call_count, 1)
96+
self.mock_pip_check.assert_called_once()
97+
self.pkg_patcher.stop()
98+
99+
@patch("sys.argv", ["bootstrap", "-a", "install"])
100+
def test_can_override_available_libraries(self):
101+
bootstrap.run(libraries=[])
102+
self.mock_pip_install.assert_has_calls(
103+
[call(i) for i in default_instrumentations],
104+
any_order=True,
105+
)
106+
self.mock_pip_check.assert_called_once()
107+
108+
@patch("sys.argv", ["bootstrap", "-a", "install"])
109+
def test_can_override_available_default_instrumentations(self):
110+
with patch(
111+
"opentelemetry.instrumentation.bootstrap._is_installed",
112+
return_value=True,
113+
):
114+
bootstrap.run(default_instrumentations=[])
115+
self.mock_pip_install.assert_has_calls(
116+
[call(i) for i in self.installed_libraries],
117+
any_order=True,
118+
)
119+
self.mock_pip_check.assert_called_once()

0 commit comments

Comments
 (0)