OpenTelemetry asyncio Instrumentation
The opentelemetry-instrumentation-asycnio package allows tracing asyncio applications. The metric for coroutine, future, is generated even if there is no setting to generate a span.
Run instrumented application
1. coroutine
# export OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE=sleep
import asyncio
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
AsyncioInstrumentor().instrument()
async def main():
await asyncio.create_task(asyncio.sleep(0.1))
asyncio.run(main())
2. future
# export OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED=true
loop = asyncio.get_event_loop()
future = asyncio.Future()
future.set_result(1)
task = asyncio.ensure_future(future)
loop.run_until_complete(task)
3. to_thread
import asyncio
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
AsyncioInstrumentor().instrument()
async def main():
await asyncio.to_thread(func)
def func():
pass
asyncio.run(main())
asyncio metric types
asyncio.process.duration (seconds) - Duration of asyncio process
asyncio.process.count (count) - Number of asyncio process
API
- class opentelemetry.instrumentation.asyncio.AsyncioInstrumentor(*args, **kwargs)[source]
Bases:
BaseInstrumentor
An instrumentor for asyncio
See BaseInstrumentor
- methods_with_coroutine = ['create_task', 'ensure_future', 'wait_for', 'wait', 'as_completed', 'run_coroutine_threadsafe']
- instrumentation_dependencies()[source]
Return a list of python packages with versions that the will be instrumented.
The format should be the same as used in requirements.txt or pyproject.toml.
For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype:
Collection
[str
]- def instrumentation_dependencies(self) -> Collection[str]:
return [‘requests ~= 1.0’]
This will ensure that the instrumentation will only be used when the specified library is present in the environment.