Adding Datadog APM to Python - How to Use ddtrace-run

Adopting APM in Python starts with installing ddtrace with pip and putting ddtrace-run at the head of the startup command. If you cannot add a prefix, switch to import ddtrace.auto, and in setups that multiply workers by forking, as gunicorn and uWSGI do, match the timing of when it is loaded.

Wrap with ddtrace-run, Load It in Code, or Leave It to Injection

To have a Python app emit APM traces, the SDK ddtrace must be loaded right after the process starts. There are three routes: putting ddtrace-run at the head of the startup command, placing import ddtrace.auto on the first line the app reads, and Single Step Instrumentation, in which the operations side injects the SDK. Which one to choose is decided less by differences in features than by who controls the startup command. If you can write the startup line yourself, it is a one-word prefix; if you cannot add a prefix, it is one line of code. If you want to add it to multiple services at once without touching the apps, choose injection (as of October 2026; source: Tracing Python Applications / Integrations - ddtrace python documentation).

The three entry points for Python instrumentation
Entry pointWhere to write itWhen it fits
ddtrace-runThe start of the startup commandYou can edit the python or gunicorn startup line yourself
import ddtrace.autoThe module the app executes firstYou cannot touch the startup line, or ddtrace-run cannot be used, as with uWSGI
Single Step InstrumentationAgent-side configurationYou want to add it to multiple services without touching the app code or startup lines
Organized based on Tracing Python Applications / Advanced Usage - ddtrace python documentation

The injection mechanism and how it behaves when a manually installed SDK is present are left to the automatic instrumentation entry; from here on, this article follows the two forms in which you install the SDK yourself.

ddtrace-run and import ddtrace.auto Differ Only in Where You Write Them

Install the SDK with pip install ddtrace, and if you were running the app with python app.py, all you do is add ddtrace-run at the head. pip 18.0.0 or later is a requirement, so if the standard pip on Ubuntu or Debian is old, upgrade it first (as of October 2026; source: Tracing Python Applications).

pip install ddtrace
ddtrace-run python app.py

If you start it from the code side, write the single line import ddtrace.auto at the top of the module the app executes first. In either form, the integrations that are applied automatically are the same (Configuring the Python SDK). The thing to watch is not to stack the two: in its gunicorn section, the ddtrace documentation states explicitly that you should avoid using ddtrace-run and import ddtrace.auto together.

gunicorn and uWSGI Split on Before and After Workers Are Forked

Django / Flask / FastAPI are included in the compatibility table as targets of automatic instrumentation, and if you go through ddtrace-run or import ddtrace.auto, no additional code is needed. The dividing line lies before the framework, in how the application server multiplies its workers. For gunicorn, the ddtrace documentation has the example ddtrace-run gunicorn myapp.wsgi:application, and gunicorn's own arguments can be lined up after it as they are. If you use gevent workers in a setup that cannot be wrapped with ddtrace-run, place import ddtrace.auto as early as possible in the app (as of October 2026; source: Python Compatibility Requirements / Integrations - ddtrace python documentation).

How to load it for each application server
Serverddtrace-runRequired settings
gunicornCan be usedFor gevent workers in a setup that cannot be wrapped, import ddtrace.auto as early as possible
uWSGICannot be usedenable-threads, plus either lazy-apps or py-call-uwsgi-fork-hooks. Load ddtrace.auto with the import option
Source: Advanced Usage - ddtrace python documentation / Integrations - ddtrace python documentation

uWSGI has so many conditions because, without lazy-apps, it forks the app that the master loaded once as is, at the C layer. This fork does not go through the mechanism by which Python recreates threads and locks, so ddtrace is not initialized correctly in the child workers. Check additional settings for each version in the ddtrace documentation. The following form is the example from the ddtrace documentation with the import target changed to ddtrace.auto.

uwsgi --enable-threads --lazy-apps --import=ddtrace.auto --master --processes=5 --http 127.0.0.1:8000 --module wsgi:app

When you use gevent, the guidance is not to rely on uWSGI's --gevent-monkey-patch but to call gevent.monkey.patch_all() inside the app.

The Destination Changes with How You Run It - Same Host or a Separate Container

On the receiving Datadog Agent, apm_config in datadog.yaml is enabled: true by default, and it listens for traces at http://localhost:8126. To receive traces from another container, add apm_non_local_traffic: true to apm_config on the Agent side (as of October 2026; source: Add the Datadog SDK). If you do not specify a destination on the SDK side, it looks for its counterpart in the following order.

  • 1. DD_TRACE_AGENT_URL - If specified, only this is used and the three below are not checked
  • 2. DD_TRACE_AGENT_HOSTNAME, or DD_AGENT_HOST if that is absent - When the Agent's hostname is specified explicitly
  • 3. /var/run/datadog/apm.socket - Used if the Unix domain socket file exists
  • 4. localhost - The final fallback. The port is the DD_TRACE_AGENT_PORT default of 8126
The order in which the Python SDK decides its destination. Source: Configuring the Python SDK / Add the Datadog SDK

The reason traces arrive with no configuration when the Agent is installed on the same host is that the last item in this order is localhost. If you place it in a separate container, nobody is at port 8126 locally, so either share the socket /var/run/datadog/apm.socket with the Agent or pass the Agent's address with DD_AGENT_HOST. Where people tend to get stuck is the precedence. If DD_TRACE_AGENT_URL is present, DD_AGENT_HOST is ignored, so changing only the host while leaving the URL written in the image has no effect (Configuring the Python SDK). How to pass it with Helm is in the Kubernetes and Helm setup article, and automatic injection of the destination into Pods is in the Cluster Agent entry.

Pass the Service Name, Destination, and Log Correlation as Environment Variables

Passing SDK settings as environment variables is the recommended form, and the Agent's host and port are also decided here. Take the three of unified service tagging and add the destination, log correlation, sampling rules, and enabling the Continuous Profiler, and you have all the values to decide on the first day of adoption.

Main environment variables to pass to the Python SDK (as of October 2026)
Environment variableWhat it decidesDefault
DD_SERVICEThe service tag attached to every span. If omitted, the SDK infers it automaticallyNot set
DD_ENVThe name of the environment, such as prod / staging / devNot set
DD_VERSIONThe app version, such as 1.2.3 or 6c44da20Not set
DD_TRACE_AGENT_URLThe destination URL. HTTP or a Unix domain socket, taking precedence over the host and port settingsNot set
DD_AGENT_HOST / DD_TRACE_AGENT_PORTThe destination host and portThe port is 8126
DD_LOGS_INJECTIONInjects trace IDs and span IDs into logstrue
DD_TRACE_SAMPLING_RULESDecides the ingestion rate per service or operation as a JSON arrayNot set
DD_PROFILING_ENABLEDStarts the profilerfalse
Source: Configuring the Python SDK

Log correlation is enabled by default, but that alone does not make IDs appear in log lines. What ddtrace does goes only as far as adding the attributes dd.trace_id / dd.span_id / dd.service / dd.env / dd.version to standard logging records. Including those attributes in the format is the app's job (source: Advanced Usage - ddtrace python documentation). When you want to turn off only specific integrations, list entries such as redis:false separated by commas in DD_PATCH_MODULES. If you omit DD_SERVICE, the SDK will decide a name for you, but because it is the key value for matching against logs and metrics, setting it explicitly rather than leaving it to inference makes later investigations faster.

First Check a Single Process with --info and Debug Logs

When the name does not appear in the service list, narrow the scope to one process and look in order from the inside of the SDK outward. The first step, ddtrace-run --info, prints the SDK's health check and a list of its settings in a readable form, but it shows only the settings from environment variables; values changed inside the code are not reflected. Next, output detailed logs with DD_TRACE_DEBUG=true and look for send failures (as of October 2026; source: Tracing Python Applications / Configuring the Python SDK).

  • 1. ddtrace-run --info - Whether the SDK can read the values passed as environment variables
  • 2. DD_TRACE_DEBUG=true - Whether send failures or the connection target appear in the logs
  • 3. DD_TRACE_DEBUG=true ddtrace-run ipython - Whether IPython, which uses SQLite, can send separately from the app
  • 4. Service Config on the APM Service Page - Whether the SDK settings received on the UI side are as intended
The order of checks in a single process. Source: Advanced Usage - ddtrace python documentation / Configuring the Python SDK

The third step is the method the ddtrace documentation gives for checking when traces are not visible in Kubernetes: because IPython uses SQLite, it is instrumented automatically, and if it cannot send, an error appears on the spot. The advantage is that it lets you separate app bugs from path problems.

Before Upgrading, Compare Production Python Against the Lower Bound in the Compatibility Table

ddtrace has support stages fixed for each release line, cycling through 12 months of general availability from the release of x.0.0, a following 6 months of maintenance, and end of support at 18 months. The minimum supported Python differs for each line, and in the compatibility table as of October 2026, CPython 3.9 is the lower bound (source: Python Compatibility Requirements). 4.0.0 dropped support for Python 3.8, removed deprecated APIs, and changed default behaviors in frameworks such as Django (Tracing Python Applications). In other words, an app that runs on an old Python cannot upgrade only the SDK; the runtime update comes first. The Agent also has a lower bound for each line, so upgrade in the order Agent, Python, ddtrace, and check the combinations in the compatibility table.

  • 1. Is the production Python version at or above the lower bound in the compatibility table - If not, plan the Python update first
  • 2. Who controls the startup command, you or the platform - If you, ddtrace-run; if the platform, the one line import ddtrace.auto
  • 3. Run ddtrace-run --info in one process for verification, and check that the values match the SDK Configuration tab in Service Config
Three questions to answer before adding it. Organized based on Python Compatibility Requirements / Tracing Python Applications

You can answer the first question today by running python --version on the production host. Once you have answers to all three, you can decide the day to add it to the production workers.

References

Share