TracerProvider. Otherwise, it creates its own. Any spans created using the standard OpenTelemetry API or by instrumentation libraries will be captured.
Enable tracing
To enable tracing, setcapture_traces to True in your middleware configuration:
Instrument libraries
The SDK provides helper functions inapitally.otel to instrument popular libraries. These are thin wrappers around the official OpenTelemetry instrumentation packages, which need to be installed separately.
| Library | Function | Required package |
|---|---|---|
| httpx | instrument_httpx() | opentelemetry-instrumentation-httpx |
| requests | instrument_requests() | opentelemetry-instrumentation-requests |
| SQLAlchemy | instrument_sqlalchemy() | opentelemetry-instrumentation-sqlalchemy |
| psycopg 3 | instrument_psycopg() | opentelemetry-instrumentation-psycopg |
| psycopg2 | instrument_psycopg2() | opentelemetry-instrumentation-psycopg2 |
| asyncpg | instrument_asyncpg() | opentelemetry-instrumentation-asyncpg |
| mysql-connector-python | instrument_mysql() | opentelemetry-instrumentation-mysql |
| redis-py | instrument_redis() | opentelemetry-instrumentation-redis |
| PyMongo | instrument_pymongo() | opentelemetry-instrumentation-pymongo |
| botocore (AWS SDK) | instrument_botocore() | opentelemetry-instrumentation-botocore |
httpx and sqlalchemy:
Create custom spans
For custom operations that aren’t covered by library instrumentation, you can create spans manually using the helpers provided by the SDK. These are thin wrappers around the standard OpenTelemetry API, which you can also use directly.@instrument decorator
Use the @instrument decorator to automatically create a span for a function. It works with both sync and async functions:
span() context manager
Use the span() context manager to trace code blocks within a function: