Skip to content

Commit 80db5ee

Browse files
author
Emanuele Palazzetti
committed
[django] the tracer can be disabled using a Django settings; by default it's the opposite of DEBUG
1 parent f051d7f commit 80db5ee

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

ddtrace/contrib/django/apps.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ def ready(self):
2323
Tracing capabilities must be enabled in this function so that
2424
all Django internals are properly configured.
2525
"""
26-
tracer = settings.DEFAULT_TRACER
27-
28-
# define the service details
29-
tracer.set_service_info(
30-
service=settings.DEFAULT_SERVICE,
31-
app='django',
32-
app_type=AppTypes.web,
33-
)
34-
35-
try:
36-
# trace Django internals
37-
patch_template(tracer)
38-
patch_db(tracer)
39-
except Exception:
40-
# TODO[manu]: we can provide better details there
41-
log.exception('error patching Django internals')
26+
if settings.ENABLED:
27+
tracer = settings.DEFAULT_TRACER
28+
29+
# define the service details
30+
tracer.set_service_info(
31+
service=settings.DEFAULT_SERVICE,
32+
app='django',
33+
app_type=AppTypes.web,
34+
)
35+
36+
try:
37+
# trace Django internals
38+
patch_template(tracer)
39+
patch_db(tracer)
40+
except Exception:
41+
# TODO[manu]: we can provide better details there
42+
log.exception('error patching Django internals')

ddtrace/contrib/django/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
DEFAULTS = {
2626
'DEFAULT_TRACER': 'ddtrace.tracer',
2727
'DEFAULT_SERVICE': 'django',
28+
'ENABLED': not django_settings.DEBUG,
2829
}
2930

3031
# List of settings that may be in string import notation.

ddtrace/contrib/django/middleware.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# 3p
1010
from django.apps import apps
11+
from django.core.exceptions import MiddlewareNotUsed
1112

1213

1314
log = logging.getLogger(__name__)
@@ -17,6 +18,11 @@ class TraceMiddleware(object):
1718
"""
1819
Middleware that traces Django requests
1920
"""
21+
def __init__(self):
22+
# disable the middleware if the tracer is not enabled
23+
if not settings.ENABLED:
24+
raise MiddlewareNotUsed
25+
2026
def process_request(self, request):
2127
tracer = settings.DEFAULT_TRACER
2228

tests/contrib/django/app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@
6767
DATADOG_APM = {
6868
# tracer with a DummyWriter
6969
'DEFAULT_TRACER': 'tests.contrib.django.utils.tracer',
70+
'ENABLED': True,
7071
}

0 commit comments

Comments
 (0)