Datadog Python APM Tracer (ddtrace)

4.6.4 · active · verified Wed Mar 25

Datadog APM Python tracing library. Current version: 4.6.4 (Mar 2026). Three breaking major versions since 2023: v2 (dropped Python 2.7/3.5/3.6, removed DD_CALL_BASIC_CONFIG), v3 (dropped Python 3.7, removed deprecated config names), v4 (dropped Python 3.8, removed Pin class, removed Span.set_tag_str, removed ddtrace.settings package). Preferred usage is ddtrace-run CLI wrapper — NOT importing patch_all() in code. Requires Datadog Agent running separately. Auto-instrumentation patches libraries at import time.

Warnings

Install

Imports

Quickstart

ddtrace — auto-instrumentation via ddtrace-run and custom spans.

# pip install ddtrace
# Requires Datadog Agent running at localhost:8126

# Option 1: ddtrace-run (recommended)
# DD_SERVICE=my-app DD_ENV=prod ddtrace-run python app.py

# Option 2: manual patch at top of entry point (before all other imports)
from ddtrace import patch
patch(all=True)  # must be before any library imports

# OR selectively:
# patch(requests=True, sqlalchemy=True, redis=True)

import flask  # patched because patch() was called first

from ddtrace import tracer

app = flask.Flask(__name__)

@app.route('/checkout')
def checkout():
    with tracer.trace('checkout.process', resource='checkout') as span:
        span.set_tag('user.id', flask.request.args.get('user_id'))
        # Unified Service Tagging via env vars:
        # DD_SERVICE, DD_ENV, DD_VERSION
        return 'ok'

# Check agent connectivity:
# ddtrace-run --info

view raw JSON →