Skip to main content
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.
The image explains monkey patching basics for instrumentation, highlighting two points: modifying existing methods at runtime and injecting logic into libraries without altering their source code.
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)
  1. The OpenTelemetry agent applies monkey patches to supported libraries at runtime.
  2. Patches wrap or modify library functions so requests, DB queries, and messaging calls generate spans and metrics automatically.
  3. If your app uses supported libraries, traces and metrics appear with no manual SDK calls.
The image explains how zero-code instrumentation operates in Python, highlighting four points: using monkey patching, modifying library functions at runtime, supporting various libraries, and eliminating the need for manual instrumentation.

Installation

Install the OpenTelemetry distro and supporting packages, then let the bootstrap tool detect and install instrumentations for the dependencies in your environment.
The bootstrap step scans installed packages and adds relevant instrumentation libraries. After this, you can run the OpenTelemetry Python agent.

Running the 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.

Configuration via environment variables

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:
Then start the instrumented process:
Python-specific configuration examples
You can control sampling, exporters, and many other settings via environment variables—most are consistent across languages, with some Python-only options shown above.
The image shows a list of OpenTelemetry agent settings with their descriptions, including service name, traces exporter, metrics exporter, and endpoint details. It appears to be from a tutorial or guide by KodeKloud.

Supported libraries (auto-instrumentation)

OpenTelemetry Python provides many built-in instrumentations and community-contributed packages. Typical coverage includes web frameworks, HTTP clients, database libraries, and messaging systems.
CategoryExamples
Web frameworksFlask, Django, FastAPI, Starlette
HTTP clientsrequests, urllib3
DatabasesSQLAlchemy and DB drivers (e.g., psycopg2)
Messaging & BackgroundKafka, Celery
This coverage gives immediate visibility across web requests, outbound HTTP calls, DB queries, and messaging layers.
The image lists popular libraries with built-in instrumentation categorized as Web, HTTP, DB, and Messaging, featuring Flask, Django, FastAPI, Starlette, urllib3, SQLAlchemy, psycopg2, Kafka, and Celery.

Debugging automatic instrumentation

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:
Run your app under the instrument wrapper and inspect logs to identify missing hooks, import order issues, or conflicts between instrumentations.

Repository & documentation

Key resources: Useful commands referenced in docs:
Installation via pip examples:
The image shows a documentation page for "General SDK Configuration" in OpenTelemetry, detailing configuration settings like OTEL_SDK_DISABLED and OTEL_ENTITIES with descriptions, default values, and notes. The page includes a navigation sidebar and various configuration details for developers.

Python vs Java (brief comparison)

TopicPython (auto-instrumentation)Java (javaagent)
MechanismMonkey patching — wrap/replace functions at runtimeBytecode instrumentation — modify classes at JVM startup (-javaagent)
Toolingopentelemetry-instrument, opentelemetry-distro, opentelemetry-bootstrapopentelemetry-javaagent.jar
ConfigurationCLI flags or environment variables (e.g., OTEL_*, OTEL_PYTHON_*)JVM agent flags and environment variables
Manual instrumentationOptional via SDK APIs for custom spansOptional via SDK APIs for custom spans
Log correlationOTEL_PYTHON_LOG_CORRELATION=true and log formatter supportSupported via JVM logging frameworks and agent config

Summary

OpenTelemetry Python zero-code instrumentation (auto-instrumentation) provides immediate observability for many common libraries without modifying application source code. Workflow summary:
  1. Install distro + exporters + instrumentation packages.
  2. Run opentelemetry-bootstrap -a install to add relevant instrumentations.
  3. Start your app with opentelemetry-instrument or use equivalent environment variables.
  4. 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.

Watch Video