Repository: DataDog/dd-trace-py

Issue:
Error log still occurs when tracer is disabled (Django)
The tracer is logging the following error when disabled:

> 2017-07-05 12:54:36,552:[none]:[ddtrace.writer:134]:ERROR cannot send services: [Errno 111] Connection refused

This is occurring when integrated with Django with the following configuration:

```python
DATADOG_TRACE = {
    'ENABLED': False
}
```
From reading the [documentation](http://pypi.datadoghq.com/trace/docs/#module-ddtrace.contrib.django) which states:
> ENABLED (default: not django_settings.DEBUG): defines if the tracer is enabled or not. If set to false, the code is still instrumented but no spans are sent to the trace agent. This setting cannot be changed at runtime and a restart is required. By default the tracer is disabled when in DEBUG mode, enabled otherwise.

It seems this log should not occur. If no spans are sent to the trace agent then presumably a connection should not be established?

Package Info
------------------

> datadog==0.15.0
> ddtrace==0.8.5  



Discussion:
Thank you for reporting that! Actually it's true, when the tracer is disable it must not send traces but unfortunately we may have a bug in the `services` endpoint. So even if the tracer is disabled, we may try to send service metadata. We'll investigate further this issue that may impact other integrations.

Will keep you updated! Thanks!
@palazzem Great, thx! :+1: 

Resolving patch:
diff --git a/ddtrace/contrib/django/apps.py b/ddtrace/contrib/django/apps.py
--- a/ddtrace/contrib/django/apps.py
+++ b/ddtrace/contrib/django/apps.py
@@ -31,13 +31,6 @@ def ready(self):
         if settings.TAGS:
             tracer.set_tags(settings.TAGS)
 
-        # define the service details
-        tracer.set_service_info(
-            app='django',
-            app_type=AppTypes.web,
-            service=settings.DEFAULT_SERVICE,
-        )
-
         # configure the tracer instance
         # TODO[manu]: we may use configure() but because it creates a new
         # AgentWriter, it breaks all tests. The configure() behavior must
@@ -46,6 +39,13 @@ def ready(self):
         tracer.writer.api.hostname = settings.AGENT_HOSTNAME
         tracer.writer.api.port = settings.AGENT_PORT
 
+        # define the service details
+        tracer.set_service_info(
+            app='django',
+            app_type=AppTypes.web,
+            service=settings.DEFAULT_SERVICE,
+        )
+
         if settings.AUTO_INSTRUMENT:
             # trace Django internals
             insert_exception_middleware()


Test patch:
diff --git a/tests/contrib/django/test_tracing_disabled.py b/tests/contrib/django/test_tracing_disabled.py
new file mode 100644
--- /dev/null
+++ b/tests/contrib/django/test_tracing_disabled.py
@@ -0,0 +1,40 @@
+# 3rd party
+from django.apps import apps
+from django.test import TestCase, override_settings
+
+# project
+from ddtrace.tracer import Tracer
+from ddtrace.contrib.django.conf import settings
+
+# testing
+from ...test_tracer import DummyWriter
+
+
+class DjangoTracingDisabledTest(TestCase):
+    def setUp(self):
+        tracer = Tracer()
+        tracer.writer = DummyWriter()
+        self.tracer = tracer
+        # Backup the old conf
+        self.backupTracer = settings.TRACER
+        self.backupEnabled = settings.ENABLED
+        # Disable tracing
+        settings.ENABLED = False
+        settings.TRACER = tracer
+        # Restart the app
+        app = apps.get_app_config('datadog_django')
+        app.ready()
+
+    def tearDown(self):
+        # Reset the original settings
+        settings.ENABLED = self.backupEnabled
+        settings.TRACER = self.backupTracer
+
+    def test_no_service_info_is_written(self):
+        services = self.tracer.writer.pop_services()
+        assert len(services) == 0
+
+    def test_no_trace_is_written(self):
+        settings.TRACER.trace("client.testing").finish()
+        traces = self.tracer.writer.pop_traces()
+        assert len(traces) == 0