OpenTelemetry Asyncio Instrumentation

0.62b0 · active · verified Thu Apr 09

The `opentelemetry-instrumentation-asyncio` package provides OpenTelemetry tracing and metrics for applications built with Python's `asyncio` library. It enables the collection of duration and counts for coroutines and futures, even if no explicit tracing is configured. This library is part of the `opentelemetry-python-contrib` project, with the current version being `0.62b0`, indicating it is actively developed and in a pre-release (beta) stage.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instrument an `asyncio` application using `AsyncioInstrumentor`. It sets up a basic OpenTelemetry `TracerProvider` with an OTLP exporter, then enables the `asyncio` instrumentation. The example includes multiple concurrent `asyncio` tasks to show how traces are captured across `await` boundaries. Environment variables like `OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE` can be used for more granular control over which coroutines are traced [1, 2, 5].

import asyncio
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor

# Configure OpenTelemetry TracerProvider
resource = Resource.create({"service.name": "asyncio-example"})
provider = TracerProvider(resource=resource)
# Use os.environ.get for OTLP endpoint in quickstart
otlp_exporter = OTLPSpanExporter(
    endpoint=os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT_GRPC', 'http://localhost:4317'),
    insecure=True # Set to False for production with HTTPS
)
span_processor = BatchSpanProcessor(otlp_exporter)
provider.add_span_processor(span_processor)
trace.set_tracer_provider(provider)

# Instrument asyncio
AsyncioInstrumentor().instrument()

tracer = trace.get_tracer(__name__)

async def some_async_task(task_id: int):
    with tracer.start_as_current_span(f"some_async_task-{task_id}"):
        print(f"Task {task_id}: Starting...")
        await asyncio.sleep(0.05) # Simulated async I/O
        print(f"Task {task_id}: Done.")

async def main():
    print("Main application starting...")
    await asyncio.gather(
        some_async_task(1),
        some_async_task(2),
        some_async_task(3)
    )
    print("Main application finished.")

if __name__ == "__main__":
    # Example of configuring tracing for specific coroutines via environment variable
    # os.environ['OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE'] = 'some_async_task'
    asyncio.run(main())
    # Ensure all spans are exported before exiting
    provider.shutdown()

view raw JSON →