Skip to content

Commit 018bb13

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #77 from palazzem/django-configure
[django] users can configure HOSTNAME and PORT in their settings
2 parents 9065536 + f5b512f commit 018bb13

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

ddtrace/contrib/django/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@
4242
tracer is used.
4343
* ``DEFAULT_SERVICE`` (default: ``django``): set the service name used by the
4444
tracer. Usually this configuration must be updated with a meaningful name.
45-
* ``ENABLED``: (default: ``not django_settings.DEBUG``): set if the tracer
46-
is enabled or not. When a tracer is disabled, Django internals are not
47-
automatically instrumented and the requests are not traced even if the
48-
``TraceMiddleware`` is properly installed. This setting cannot be changed
49-
at runtime and a restart is required. By default the tracer is disabled
50-
when in ``DEBUG`` mode, enabled otherwise.
45+
* ``ENABLED`` (default: ``not django_settings.DEBUG``): defines if the tracer is
46+
enabled or not. If set to false, the code is still instrumented but no spans
47+
are sent to the trace agent. This setting cannot be changed at runtime
48+
and a restart is required. By default the tracer is disabled when in ``DEBUG``
49+
mode, enabled otherwise.
50+
* ``AUTO_INSTRUMENT`` (default: ``True``): if set to false the code will not be
51+
instrumented, while the tracer may be active for your internal usage. This could
52+
be useful if you want to use the Django integration, but you want to trace only
53+
particular functions or views. If set to False, the request middleware will be
54+
disabled even if present.
55+
* ``AGENT_HOSTNAME`` (default: ``localhost``): define the hostname of the trace agent.
56+
* ``AGENT_PORT`` (default: ``7777``): define the port of the trace agent.
5157
"""
5258
from ..util import require_modules
5359

ddtrace/contrib/django/apps.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ def ready(self):
2424
Tracing capabilities must be enabled in this function so that
2525
all Django internals are properly configured.
2626
"""
27-
if settings.ENABLED:
28-
tracer = settings.TRACER
29-
30-
# define the service details
31-
tracer.set_service_info(
32-
app='django',
33-
app_type=AppTypes.web,
34-
service=settings.DEFAULT_SERVICE,
35-
)
36-
27+
tracer = settings.TRACER
28+
29+
# define the service details
30+
tracer.set_service_info(
31+
app='django',
32+
app_type=AppTypes.web,
33+
service=settings.DEFAULT_SERVICE,
34+
)
35+
36+
# configure the tracer instance
37+
# TODO[manu]: we may use configure() but because it creates a new
38+
# AgentWriter, it breaks all tests. The configure() behavior must
39+
# be changed to use it in this integration
40+
tracer.enabled = settings.ENABLED
41+
tracer.writer._reporter.transport.hostname = settings.AGENT_HOSTNAME
42+
tracer.writer._reporter.transport.port = settings.AGENT_PORT
43+
44+
if settings.AUTO_INSTRUMENT:
3745
# trace Django internals
3846
try:
3947
patch_db(tracer)

ddtrace/contrib/django/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
'TRACER': 'ddtrace.tracer',
2525
'DEFAULT_SERVICE': 'django',
2626
'ENABLED': True,
27+
'AUTO_INSTRUMENT': True,
28+
'AGENT_HOSTNAME': 'localhost',
29+
'AGENT_PORT': '7777',
2730
}
2831

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

ddtrace/contrib/django/middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class TraceMiddleware(object):
2020
"""
2121
def __init__(self):
2222
# disable the middleware if the tracer is not enabled
23-
if not settings.ENABLED:
23+
# or if the auto instrumentation is disabled
24+
if not settings.AUTO_INSTRUMENT:
2425
raise MiddlewareNotUsed
2526

2627
def process_request(self, request):

tests/contrib/django/app/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@
9696
# tracer with a DummyWriter
9797
'TRACER': 'tests.contrib.django.utils.tracer',
9898
'ENABLED': True,
99+
'AGENT_HOSTNAME': 'agent.service.consul',
100+
'AGENT_PORT': '8777',
99101
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
3+
# 3rd party
4+
from nose.tools import eq_, ok_
5+
from django.test import override_settings
6+
7+
# testing
8+
from .utils import DjangoTraceTestCase
9+
10+
11+
class DjangoInstrumentationTest(DjangoTraceTestCase):
12+
"""
13+
Ensures that Django is correctly configured according to
14+
users settings
15+
"""
16+
def test_enabled_flag(self):
17+
eq_(self.tracer.writer._reporter.transport.hostname, 'agent.service.consul')
18+
eq_(self.tracer.writer._reporter.transport.port, '8777')

0 commit comments

Comments
 (0)