Explains OpenTelemetry Python zero-code auto-instrumentation using monkey patching to capture traces and metrics for common libraries, plus installation, configuration, and debugging steps.
Zero-code (or auto-) instrumentation lets you add observability to Python applications without modifying source code. In Python this is achieved primarily via monkey patching: at runtime, the agent replaces or wraps existing functions and methods in popular libraries so that traces and metrics are collected automatically. While other languages (like Java) commonly use bytecode manipulation, Python’s runtime patching delivers the same outcome by altering call behavior as the program runs.
OpenTelemetry for Python provides auto-instrumentation for many widely used libraries and frameworks (Flask, Django, Requests, SQLAlchemy, Redis, etc.), making it simple to gain observability with zero code changes. See the official docs: OpenTelemetry for Python.How zero-code instrumentation works (high level)
The OpenTelemetry agent applies monkey patches to supported libraries at runtime.
Patches wrap or modify library functions so requests, DB queries, and messaging calls generate spans and metrics automatically.
If your app uses supported libraries, traces and metrics appear with no manual SDK calls.
Install the OpenTelemetry distro and supporting packages, then let the bootstrap tool detect and install instrumentations for the dependencies in your environment.
pip install opentelemetry-distro opentelemetry-exporter-otlp opentelemetry-instrumentation opentelemetry-bootstrapopentelemetry-bootstrap -a install
The bootstrap step scans installed packages and adds relevant instrumentation libraries. After this, you can run the OpenTelemetry Python agent.
Start your app with the opentelemetry-instrument wrapper to enable auto-instrumentation. Example that exports traces and metrics to the console (for development) and to an OTLP endpoint (typical for sending to an OTel Collector):
For production, send telemetry to an OTLP Collector or backend instead of the console. Use the console exporter only for quick testing or troubleshooting.
Environment variables are commonly used in containers and CI/CD pipelines. They provide the same control as CLI flags and are typically easier to manage in deployments.Example environment variables:
# Exclude health/metrics endpoints from tracingexport OTEL_PYTHON_EXCLUDED_URLS="/health,/metrics"# Trace specific Django request attributesexport OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS="path_info,content_type"# Enable log correlation and format logs to include trace/span idsexport OTEL_PYTHON_LOG_CORRELATION=trueexport OTEL_PYTHON_LOG_FORMAT="%(msg)s [trace_id=%(trace_id)s span_id=%(span_id)s]"export OTEL_PYTHON_LOG_LEVEL=debug# Disable specific auto-instrumentations when neededexport OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=redis,grpc,kafka
You can control sampling, exporters, and many other settings via environment variables—most are consistent across languages, with some Python-only options shown above.
OpenTelemetry Python provides many built-in instrumentations and community-contributed packages. Typical coverage includes web frameworks, HTTP clients, database libraries, and messaging systems.
Category
Examples
Web frameworks
Flask, Django, FastAPI, Starlette
HTTP clients
requests, urllib3
Databases
SQLAlchemy and DB drivers (e.g., psycopg2)
Messaging & Background
Kafka, Celery
This coverage gives immediate visibility across web requests, outbound HTTP calls, DB queries, and messaging layers.
If a library isn’t being instrumented or spans/attributes are missing, enable debug logging to see detailed messages about what instrumentations are applied and any errors:
OpenTelemetry Python zero-code instrumentation (auto-instrumentation) provides immediate observability for many common libraries without modifying application source code. Workflow summary:
Run opentelemetry-bootstrap -a install to add relevant instrumentations.
Start your app with opentelemetry-instrument or use equivalent environment variables.
Tune Python-specific options (exclusions, log correlation, disabled instrumentations) as needed.
These steps let you quickly capture telemetry (traces and metrics) across web, HTTP client, DB, and messaging layers with minimal operational overhead.