@@ -52,8 +52,6 @@ async def main():
52
52
-------------
53
53
.. code:: python
54
54
55
- # export OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE=func
56
-
57
55
import asyncio
58
56
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
59
57
@@ -103,18 +101,43 @@ def func():
103
101
from timeit import default_timer
104
102
from typing import Collection
105
103
106
- from opentelemetry .metrics import get_meter
107
- from opentelemetry .trace import get_tracer
108
- from opentelemetry .trace .status import Status , StatusCode
109
104
from wrapt import wrap_function_wrapper as _wrap
110
105
111
- from opentelemetry .instrumentation .asyncio .metrics import *
106
+ from opentelemetry .instrumentation .asyncio .metrics import (
107
+ ASYNCIO_COROUTINE_ACTIVE ,
108
+ ASYNCIO_COROUTINE_CANCELLED ,
109
+ ASYNCIO_COROUTINE_CREATED ,
110
+ ASYNCIO_COROUTINE_DURATION ,
111
+ ASYNCIO_COROUTINE_EXCEPTIONS ,
112
+ ASYNCIO_COROUTINE_FINISHED ,
113
+ ASYNCIO_COROUTINE_NAME ,
114
+ ASYNCIO_COROUTINE_TIMEOUTS ,
115
+ ASYNCIO_EXCEPTIONS_NAME ,
116
+ ASYNCIO_FUTURES_ACTIVE ,
117
+ ASYNCIO_FUTURES_CANCELLED ,
118
+ ASYNCIO_FUTURES_CREATED ,
119
+ ASYNCIO_FUTURES_DURATION ,
120
+ ASYNCIO_FUTURES_EXCEPTIONS ,
121
+ ASYNCIO_FUTURES_FINISHED ,
122
+ ASYNCIO_FUTURES_TIMEOUTS ,
123
+ ASYNCIO_TO_THREAD_ACTIVE ,
124
+ ASYNCIO_TO_THREAD_CREATED ,
125
+ ASYNCIO_TO_THREAD_DURATION ,
126
+ ASYNCIO_TO_THREAD_EXCEPTIONS ,
127
+ ASYNCIO_TO_THREAD_FINISHED ,
128
+ )
112
129
from opentelemetry .instrumentation .asyncio .package import _instruments
113
- from opentelemetry .instrumentation .asyncio .utils import get_coros_to_trace , get_future_trace_enabled , \
114
- get_to_thread_to_trace
130
+ from opentelemetry .instrumentation .asyncio .utils import (
131
+ get_coros_to_trace ,
132
+ get_future_trace_enabled ,
133
+ get_to_thread_to_trace ,
134
+ )
115
135
from opentelemetry .instrumentation .asyncio .version import __version__
116
136
from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
117
137
from opentelemetry .instrumentation .utils import unwrap
138
+ from opentelemetry .metrics import get_meter
139
+ from opentelemetry .trace import get_tracer
140
+ from opentelemetry .trace .status import Status , StatusCode
118
141
119
142
ASYNCIO_PREFIX = "asyncio."
120
143
@@ -170,9 +193,7 @@ def instrumentation_dependencies(self) -> Collection[str]:
170
193
171
194
def _instrument (self , ** kwargs ):
172
195
tracer_provider = kwargs .get ("tracer_provider" )
173
- self ._tracer = get_tracer (
174
- __name__ , __version__ , tracer_provider
175
- )
196
+ self ._tracer = get_tracer (__name__ , __version__ , tracer_provider )
176
197
self ._meter = get_meter (
177
198
__name__ , __version__ , kwargs .get ("meter_provider" )
178
199
)
@@ -211,11 +232,15 @@ def wrap_coro_or_future(method, instance, args, kwargs):
211
232
if args and len (args ) > 0 :
212
233
first_arg = args [0 ]
213
234
# Check if it's a coroutine or future and wrap it
214
- if asyncio .iscoroutine (first_arg ) or futures .isfuture (first_arg ):
235
+ if asyncio .iscoroutine (first_arg ) or futures .isfuture (
236
+ first_arg
237
+ ):
215
238
args = (self .trace_item (first_arg ),) + args [1 :]
216
239
# Check if it's a list and wrap each item
217
240
elif isinstance (first_arg , list ):
218
- args = ([self .trace_item (item ) for item in first_arg ],) + args [1 :]
241
+ args = (
242
+ [self .trace_item (item ) for item in first_arg ],
243
+ ) + args [1 :]
219
244
return method (* args , ** kwargs )
220
245
221
246
_wrap (asyncio , method_name , wrap_coro_or_future )
@@ -227,7 +252,6 @@ def uninstrument_method_with_coroutine(self, method_name):
227
252
unwrap (asyncio , method_name )
228
253
229
254
def instrument_gather (self ):
230
-
231
255
def wrap_coros_or_futures (method , instance , args , kwargs ):
232
256
if args and len (args ) > 0 :
233
257
# Check if it's a coroutine or future and wrap it
@@ -286,8 +310,13 @@ def trace_to_thread(self, func):
286
310
start = default_timer ()
287
311
self .to_thread_created_metric .add (1 )
288
312
self .to_thread_active_metric .add (1 )
289
- span = self ._tracer .start_span (
290
- f"{ ASYNCIO_PREFIX } to_thread_func-" + func .__name__ ) if func .__name__ in self ._to_thread_name_to_trace else None
313
+ span = (
314
+ self ._tracer .start_span (
315
+ f"{ ASYNCIO_PREFIX } to_thread_func-" + func .__name__
316
+ )
317
+ if func .__name__ in self ._to_thread_name_to_trace
318
+ else None
319
+ )
291
320
exception = None
292
321
try :
293
322
return func
@@ -326,8 +355,11 @@ async def trace_coroutine(self, coro):
326
355
self .coro_created_metric .add (1 , coro_attr )
327
356
self .coro_active_metric .add (1 , coro_attr )
328
357
329
- span = self ._tracer .start_span (
330
- f"{ ASYNCIO_PREFIX } coro-" + coro .__name__ ) if coro .__name__ in self ._coros_name_to_trace else None
358
+ span = (
359
+ self ._tracer .start_span (f"{ ASYNCIO_PREFIX } coro-" + coro .__name__ )
360
+ if coro .__name__ in self ._coros_name_to_trace
361
+ else None
362
+ )
331
363
332
364
exception = None
333
365
try :
@@ -343,7 +375,9 @@ async def trace_coroutine(self, coro):
343
375
except Exception as exc :
344
376
exception = exc
345
377
coro_exception_attr = coro_attr .copy ()
346
- coro_exception_attr [ASYNCIO_EXCEPTIONS_NAME ] = exc .__class__ .__name__
378
+ coro_exception_attr [
379
+ ASYNCIO_EXCEPTIONS_NAME
380
+ ] = exc .__class__ .__name__
347
381
self .coro_exception_metric .add (1 , coro_exception_attr )
348
382
raise
349
383
finally :
@@ -362,7 +396,11 @@ def trace_future(self, future):
362
396
start = default_timer ()
363
397
self .future_created_metric .add (1 )
364
398
self .future_active_metric .add (1 )
365
- span = self ._tracer .start_span (f"{ ASYNCIO_PREFIX } future" ) if self ._future_active_enabled else None
399
+ span = (
400
+ self ._tracer .start_span (f"{ ASYNCIO_PREFIX } future" )
401
+ if self ._future_active_enabled
402
+ else None
403
+ )
366
404
367
405
def callback (f ):
368
406
exception = f .exception ()
@@ -371,7 +409,9 @@ def callback(f):
371
409
elif isinstance (exception , asyncio .TimeoutError ):
372
410
self .future_timeout_metric .add (1 )
373
411
elif exception :
374
- exception_attr = {ASYNCIO_EXCEPTIONS_NAME : exception .__class__ .__name__ }
412
+ exception_attr = {
413
+ ASYNCIO_EXCEPTIONS_NAME : exception .__class__ .__name__
414
+ }
375
415
self .future_exception_metric .add (1 , exception_attr )
376
416
377
417
duration = max (round ((default_timer () - start ) * 1000 ), 0 )
0 commit comments