Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions ddtrace/contrib/internal/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
Consider using this option if your application is performance-sensitive and the additional
Django-layer spans are not required for your observability needs.

Default: ``False``
Default: ``True``

*New in version v3.15.0.*

Expand All @@ -120,40 +120,25 @@

Whether or not to instrument template rendering.

Can also be enabled with the ``DD_DJANGO_INSTRUMENT_TEMPLATES`` environment variable.
Can be enabled with the ``DD_DJANGO_INSTRUMENT_TEMPLATES=true`` or ``DD_DJANGO_TRACING_MINIMAL=false`` environment variables.

Default: ``True``
Default: ``False``

.. py:data:: ddtrace.config.django['instrument_databases']

Whether or not to instrument databases.

Can also be enabled with the ``DD_DJANGO_INSTRUMENT_DATABASES`` environment variable.

Default: ``True``

.. py:data:: ddtrace.config.django['always_create_database_spans']

Whether or not to enforce that a Django database span is created regardless of other
database instrumentation.
Can be enabled with the ``DD_DJANGO_INSTRUMENT_DATABASES=true`` or ``DD_DJANGO_TRACING_MINIMAL=false`` environment variables.

Enabling this will provide database spans when the database engine is not yet supported
by ``ddtrace``, however it may result in duplicate database spans when the database
engine is supported and enabled.

Can also be enabled with the ``DD_DJANGO_ALWAYS_CREATE_DATABASE_SPANS`` environment variable.

Default: ``True``

*New in version v3.13.0.*
Default: ``False``

.. py:data:: ddtrace.config.django['instrument_caches']

Whether or not to instrument caches.

Can also be enabled with the ``DD_DJANGO_INSTRUMENT_CACHES`` environment variable.
Can be enabled with the ``DD_DJANGO_INSTRUMENT_CACHES=true`` or ``DD_DJANGO_TRACING_MINIMAL=false`` environment variables.

Default: ``True``
Default: ``False``

.. py:data:: ddtrace.config.django.http['trace_query_string']

Expand Down
6 changes: 3 additions & 3 deletions ddtrace/contrib/internal/django/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def cursor(func: FunctionType, args: Tuple[Any], kwargs: Dict[str, Any]) -> Any:
# Don't double wrap Django database cursors:
# If the underlying cursor is already wrapped (e.g. by another library),
# we just add the Django tags to the existing Pin (if any) and return
if is_wrapted(cursor.cursor) and not config_django.always_create_database_spans:
if is_wrapted(cursor.cursor):
instance = args[0]
tags = {
"django.db.vendor": getattr(instance, "vendor", "db"),
Expand All @@ -86,8 +86,8 @@ def cursor(func: FunctionType, args: Tuple[Any], kwargs: Dict[str, Any]) -> Any:
return cursor

# Always wrap Django database cursors:
# If the underlying cursor is not already wrapped, or if `always_create_database_spans`
# is set to True, we wrap the underlying cursor with our TracedCursor class
# If the underlying cursor is not already wrapped,
# we wrap the underlying cursor with our TracedCursor class
#
# This allows us to get Database spans for any query executed where we don't
# have an integration for the database library in use, or in the case that
Expand Down
5 changes: 1 addition & 4 deletions ddtrace/contrib/internal/django/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@

log = get_logger(__name__)

# TODO[4.0]: Change this to True by default
DJANGO_TRACING_MINIMAL = asbool(_get_config("DD_DJANGO_TRACING_MINIMAL", default=False))
DJANGO_TRACING_MINIMAL = asbool(_get_config("DD_DJANGO_TRACING_MINIMAL", default=True))

config._add(
"django",
Expand All @@ -55,8 +54,6 @@
instrument_middleware=asbool(os.getenv("DD_DJANGO_INSTRUMENT_MIDDLEWARE", default=True)),
instrument_templates=asbool(os.getenv("DD_DJANGO_INSTRUMENT_TEMPLATES", default=not DJANGO_TRACING_MINIMAL)),
instrument_databases=asbool(os.getenv("DD_DJANGO_INSTRUMENT_DATABASES", default=not DJANGO_TRACING_MINIMAL)),
# TODO[4.0]: remove this option and make it the default behavior when databases are instrumented
always_create_database_spans=asbool(os.getenv("DD_DJANGO_ALWAYS_CREATE_DATABASE_SPANS", default=True)),
instrument_caches=asbool(os.getenv("DD_DJANGO_INSTRUMENT_CACHES", default=not DJANGO_TRACING_MINIMAL)),
trace_query_string=None, # Default to global config
include_user_name=asm_config._django_include_user_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
upgrade:
- |
Django: This upgrades the default tracing behavior to enable minimal tracing mode by default (``DD_DJANGO_TRACING_MINIMAL`` now defaults to ``true``). Django ORM, cache, and template instrumentation are disabled by default to eliminate duplicate span creation since library integrations for database drivers (psycopg, MySQLdb, sqlite3), cache clients (redis, memcached), template renderers (Jinja2), and other supported libraries continue to be traced. This reduces performance overhead by removing redundant Django-layer instrumentation. To restore all Django instrumentation, set ``DD_DJANGO_TRACING_MINIMAL=false``, or enable individual features using ``DD_DJANGO_INSTRUMENT_DATABASES=true``, ``DD_DJANGO_INSTRUMENT_CACHES=true``, and ``DD_DJANGO_INSTRUMENT_TEMPLATES=true``.
- |
Django: When ``DD_DJANGO_INSTRUMENT_DATABASES=true`` (default ``false``), database instrumentation now merges Django-specific tags into database driver spans created by supported integrations (psycopg, sqlite3, MySQLdb, etc.) instead of creating duplicate Django database spans. If the database cursor is not already wrapped by a supported integration, Django wraps it and creates a span. This change reduces overhead and duplicate spans while preserving visibility into database operations.
2 changes: 2 additions & 0 deletions riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
env={
"DD_CIVISIBILITY_ITR_ENABLED": "0",
"DD_IAST_REQUEST_SAMPLING": "100", # Override default 30% to analyze all IAST requests
# TODO: Remove once pkg_resources warnings are no longer emitted from this internal module
"PYTHONWARNINGS": "ignore::UserWarning:ddtrace.internal.module",
},
venvs=[
Venv(
Expand Down
4 changes: 4 additions & 0 deletions tests/appsec/integrations/django_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from ddtrace.appsec._iast._taint_tracking._context import debug_context_array_free_slots_number
from ddtrace.appsec._iast.main import patch_iast
from ddtrace.contrib.internal.django.patch import patch as django_patch
from ddtrace.contrib.internal.psycopg.patch import patch as psycopg_patch
from ddtrace.contrib.internal.requests.patch import patch as requests_patch
from ddtrace.contrib.internal.sqlite3.patch import patch as sqlite3_patch
from ddtrace.internal import core
from tests.utils import DummyTracer
from tests.utils import TracerSpanContainer
Expand All @@ -35,7 +37,9 @@ def pytest_configure():
settings.DEBUG = False
patch_iast()
load_iast()
psycopg_patch()
requests_patch()
sqlite3_patch()
django_patch()
enable_iast_propagation()
django.setup()
Expand Down
Loading
Loading