Skip to content

Commit 77d5ee1

Browse files
ecarraraxrmx
authored andcommitted
Support functools.partial functions in AsyncioInstrumentor.trace_to_thread (open-telemetry#2911)
1 parent 189566d commit 77d5ee1

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8080
([#2753](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2753))
8181
- `opentelemetry-instrumentation-grpc` Fix grpc supported version
8282
([#2845](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2845))
83+
- `opentelemetry-instrumentation-asyncio` fix `AttributeError` in
84+
`AsyncioInstrumentor.trace_to_thread` when `func` is a `functools.partial` instance
85+
([#2911](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2911))
8386

8487
## Version 1.26.0/0.47b0 (2024-07-23)
8588

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

+7-5
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

+35
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")

0 commit comments

Comments
 (0)