Python Instrumentation in Containers
Instrumentation of Python applications in containers with the OpenTelemetry agent
This documentation explains how to instrument Python applications in containers using the zero-code model, with the official OpenTelemetry agent, adapted for use with Elven Observability. The idea is to ensure the collection of traces (and optionally metrics) without code changes, only by adjusting the Dockerfile and environment variables.
Agent installation in the Dockerfile
Add only the necessary packages to the Dockerfile according to your framework. See below for the common packages and those specific to each framework:
Common packages (always install):
RUN pip install \
opentelemetry-distro \
opentelemetry-exporter-otlp \
opentelemetry-instrumentation-requestsFastAPI:
RUN pip install opentelemetry-instrumentation-fastapiFlask:
RUN pip install opentelemetry-instrumentation-flaskDjango:
RUN pip install opentelemetry-instrumentation-djangoImportant: Do not install unnecessary instrumentations. Ex: apps using Django do not need opentelemetry-instrumentation-fastapi.
Example Dockerfile:
Environment Variables Configuration
Set the following variables to enable automatic instrumentation and send data to the Elven collector:
Explanation:
OTEL_SERVICE_NAME: name of the service displayed in Grafana/Tempo.OTEL_RESOURCE_ATTRIBUTES: extra tags to enrich the traces.OTEL_EXPORTER_OTLP_*: endpoints for sending the data.
How to start the app with the agent (CMD)
Use the opentelemetry-instrument command as a wrapper for the application.
See below for examples by framework:
FastAPI (with Uvicorn)
Flask
Django
Info: You can adapt the command according to your entrypoint: gunicorn, uvicorn, waitress, etc.
Examples by Environment
Docker Compose
Kubernetes
ECS (Fargate ou EC2)
Troubleshooting
Traces not showing: check if the endpoint is accessible and if the CMD uses opentelemetry-instrument correctly.
Application timeout: verify if the collector is online and responding on port 4318.
Duplicate instrumentation: avoid mixing manual instrumentation with the automatic mode.
Dependency error: each framework requires its specific instrumentation, review the pip install.
Best Practices
Give meaningful names to services (
OTEL_SERVICE_NAME=projeto-env-modulo).Separate environments (
environment=dev,staging,production).Do not expose credentials in environment variables.
Do not use
OTEL_LOG_LEVEL=debugin production.Test locally with
otel-clioucurlto validate the collector.
Example of .env
.envYour Python app is now instrumented with Elven Observability, using auto-instrumentation based on OpenTelemetry.
Last updated
Was this helpful?

