Skip to content

Commit d342d8c

Browse files
committed
- Add instrumentation-asyncio metric.
- Configure coroutines/to_thread func to apply trace via environment variables. - Apply trace to future using a boolean environment variable.
1 parent 9d4344a commit d342d8c

15 files changed

+743
-76
lines changed

instrumentation/opentelemetry-instrumentation-asyncio/README.rst

+83-15
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,104 @@ OpenTelemetry asyncio Instrumentation
88

99
AsyncioInstrumentor: Tracing Requests Made by the Asyncio Library
1010

11-
Primary Use Case:
12-
-----------------
13-
1. Performance and Error Monitoring:
14-
The AsyncioInstrumentor tool offers significant advantages for developers and system administrators. It's designed to monitor real-time performance bottlenecks and catch exceptions within specific asynchronous tasks.
1511

16-
When It's Not Ideal to Use AsyncioInstrumentor:
17-
------------------------------------------------
18-
1. Frameworks with Built-in Instrumentation:
19-
If you're utilizing a framework like aiohttp that already includes built-in instrumentation, you might not need this library. In such cases, leveraging the built-in tools of the framework is generally more beneficial than using external ones like AsyncioInstrumentor.
12+
The opentelemetry-instrumentation-asycnio package allows tracing asyncio applications.
13+
The metric for coroutine, future, is generated even if there is no setting to generate a span.
2014

21-
2. Libraries Lacking Instrumentation:
22-
Should you employ a library that isn't inherently instrumented, AsyncioInstrumentor can step in to fill that gap.
2315

24-
3. Concerns about Overhead:
25-
Tracing each task and future consistently can lead to added overhead. As a best practice, it's wise to enable tracing only when crucial, especially during the development stage.
16+
Set the name of the coroutine you want to trace.
17+
-----------------------------------------------
18+
.. code::
19+
export OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE=coro_name,coro_name2,coro_name3
2620
27-
Example
28-
-------
21+
If you want to keep track of which function to use in the to_thread function of asyncio, set the name of the function.
22+
----
23+
.. code::
24+
export OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE=func_name,func_name2,func_name3
25+
26+
For future, set it up like this
27+
----
28+
.. code::
29+
export OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED=true
30+
31+
Run instrumented taskcoroutine
32+
----
33+
1. coroutine
34+
----
2935
.. code:: python
3036
37+
# export OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE=sleep
38+
39+
import asyncio
3140
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
41+
3242
AsyncioInstrumentor().instrument()
3343
44+
async def main():
45+
await asyncio.create_task(asyncio.sleep(0.1))
46+
47+
asyncio.run(main())
48+
49+
2. future
50+
----
51+
.. code:: python
52+
53+
# export OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED=true
54+
55+
loop = asyncio.get_event_loop()
56+
57+
future = asyncio.Future()
58+
future.set_result(1)
59+
task = asyncio.ensure_future(future)
60+
loop.run_until_complete(task)
61+
62+
3. to_thread
63+
----
64+
.. code:: python
65+
66+
# export OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE=func
67+
3468
import asyncio
69+
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
70+
71+
AsyncioInstrumentor().instrument()
3572
3673
async def main():
37-
await asyncio.create_task(asyncio.sleep(0.1))
74+
await asyncio.to_thread(func)
75+
76+
def func():
77+
pass
3878
3979
asyncio.run(main())
4080
81+
82+
asyncio metric types
83+
-------
84+
85+
* `asyncio.futures.duration` (ms) - Duration of the future
86+
* `asyncio.futures.exceptions` (count) - Number of exceptions raised by the future
87+
* `asyncio.futures.cancelled` (count) - Number of futures cancelled
88+
* `asyncio.futures.created` (count) - Number of futures created
89+
* `asyncio.futures.active` (count) - Number of futures active
90+
* `asyncio.futures.finished` (count) - Number of futures finished
91+
* `asyncio.futures.timeouts` (count) - Number of futures timed out
92+
93+
* `asyncio.coroutine.duration` (ms) - Duration of the coroutine
94+
* `asyncio.coroutine.exceptions` (count) - Number of exceptions raised by the coroutine
95+
* `asyncio.coroutine.created` (count) - Number of coroutines created
96+
* `asyncio.coroutine.active` (count) - Number of coroutines active
97+
* `asyncio.coroutine.finished` (count) - Number of coroutines finished
98+
* `asyncio.coroutine.timeouts` (count) - Number of coroutines timed out
99+
* `asyncio.coroutine.cancelled` (count) - Number of coroutines cancelled
100+
101+
* `asyncio.to_thread.duration` (ms) - Duration of the to_thread
102+
* `asyncio.to_thread.exceptions` (count) - Number of exceptions raised by the to_thread
103+
* `asyncio.to_thread.created` (count) - Number of to_thread created
104+
* `asyncio.to_thread.active` (count) - Number of to_thread active
105+
* `asyncio.to_thread.finished` (count) - Number of to_thread finished
106+
107+
108+
41109
API
42110
---
43111

0 commit comments

Comments
 (0)