Python zero-code instrumentation (original) (raw)
Automatic instrumentation with Python uses a Python agent that can be attached to any Python application. This agent primarily usesmonkey patching to modify library functions at runtime, allowing for the capture of telemetry data from many popular libraries and frameworks.
Setup
Run the following commands to install the appropriate packages.
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install
The opentelemetry-distro
package installs the API, SDK, and theopentelemetry-bootstrap
and opentelemetry-instrument
tools.
The opentelemetry-bootstrap -a install
command reads through the list of packages installed in your active site-packages
folder, and installs the corresponding instrumentation libraries for these packages, if applicable. For example, if you already installed the flask
package, runningopentelemetry-bootstrap -a install
will installopentelemetry-instrumentation-flask
for you. The OpenTelemetry Python agent will use monkey patching to modify functions in these libraries at runtime.
Running opentelemetry-bootstrap
without arguments lists the recommended instrumentation libraries to be installed. For more information, seeopentelemetry-bootstrap.
Configuring the agent
The agent is highly configurable.
One option is to configure the agent by way of configuration properties from the CLI:
opentelemetry-instrument \
--traces_exporter console,otlp \
--metrics_exporter console \
--service_name your-service-name \
--exporter_otlp_endpoint 0.0.0.0:4317 \
python myapp.py
Alternatively, you can use environment variables to configure the agent:
OTEL_SERVICE_NAME=your-service-name \
OTEL_TRACES_EXPORTER=console,otlp \
OTEL_METRICS_EXPORTER=console \
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=0.0.0.0:4317
opentelemetry-instrument \
python myapp.py
To see the full range of configuration options, seeAgent Configuration.
Supported libraries and frameworks
A number of popular Python libraries are auto-instrumented, includingFlaskandDjango. For the full list, see theRegistry.
Troubleshooting
For general troubleshooting steps and solutions to specific issues, seeTroubleshooting.
Feedback
Was this page helpful?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!