Repository: DataDog/dd-trace-py

Issue:
OpenTracing Tracer does not honor environment variable configuration
### Which version of dd-trace-py are you using?

0.47.0

### Which version of pip are you using?

20.1.1

### Which version of the libraries are you using?

```
celery==4.3.0
Celery-OpenTracing==0.0.1
opentracing==2.4.0
```

### How can we reproduce your problem?

1. Configure ddtrace using environment variables:

   ```
      - DD_AGENT_SERVICE_HOST: datadog-agent-traceport.infra.svc.cluster.local
      - DD_TRACE_ENABLED=false
      - DD_ENV=local
      - DD_LOGS_INJECTION=false
      - DD_SERVICE=foo
      - DD_TRACE_DEBUG=false
    ```

2. Follow the instructions in the documentation for enabling the `celery_opentracing` module:

   ```
   from celery_opentracing import CeleryTracing
   from ddtrace.opentracer import set_global_tracer, Tracer

   ddtracer = Tracer()
   set_global_tracer(ddtracer)

   app = CeleryTracing(app, tracer=ddtracer)
   ```

### What is the result that you get?

The tracers are configured to be sent to localhost and `DD_TRACE_ENABLED` is not honored. This also affects the non-opentracing tracer.

### What is the result that you expected?

The tracers should honor the `DD_` environment variables.

### Workaround

I've managed to workaround the issue by configuring the OpenTracing Tracer as follows:

```
from ddtrace import config as ddconfig
from ddtrace.opentracer import Tracer, set_global_tracer


def init_tracer(service_name=None):
    if service_name is None:
        service_name = ddconfig.service
    # Trick DataDog's open tracing shim layer to use the normal tracer's defaults 🤦🏻‍
    config = dict(agent_hostname=None, agent_port=None, enabled=None)
    tracer = Tracer(service_name, config=config)
    set_global_tracer(tracer)
    return tracer
``` 

The issue seems to be that the [`DEFAULT_CONFIG` in `opentracer/tracer.py`](https://github.com/DataDog/dd-trace-py/blob/c9abe9578b2f7b9da612ea8ba42e3a9e685c2509/ddtrace/opentracer/tracer.py#L22-L35) will [override  the configuration of the underlying `_dd_tracer`](https://github.com/DataDog/dd-trace-py/blob/c9abe9578b2f7b9da612ea8ba42e3a9e685c2509/ddtrace/opentracer/tracer.py#L86-L98). This is particularly problematic as the OpenTracing Tracer shim will use the global `ddtrace.tracer` by default, resulting in the unexpected "deconfiguration" of that tracer.



Discussion:

Resolving patch:
diff --git a/ddtrace/opentracer/tracer.py b/ddtrace/opentracer/tracer.py
--- a/ddtrace/opentracer/tracer.py
+++ b/ddtrace/opentracer/tracer.py
@@ -21,11 +21,11 @@
 log = get_logger(__name__)
 
 DEFAULT_CONFIG = {
-    keys.AGENT_HOSTNAME: "localhost",
-    keys.AGENT_HTTPS: False,
-    keys.AGENT_PORT: 8126,
+    keys.AGENT_HOSTNAME: None,
+    keys.AGENT_HTTPS: None,
+    keys.AGENT_PORT: None,
     keys.DEBUG: False,
-    keys.ENABLED: True,
+    keys.ENABLED: None,
     keys.GLOBAL_TAGS: {},
     keys.SAMPLER: None,
     keys.PRIORITY_SAMPLING: None,
@@ -63,7 +63,6 @@ def __init__(self, service_name=None, config=None, scope_manager=None, dd_tracer
             self._config.update(config)
         # Pull out commonly used properties for performance
         self._service_name = service_name or get_application_name()
-        self._enabled = self._config.get(keys.ENABLED)
         self._debug = self._config.get(keys.DEBUG)
 
         if self._debug:
@@ -87,7 +86,7 @@ def __init__(self, service_name=None, config=None, scope_manager=None, dd_tracer
         self._dd_tracer = dd_tracer or ddtrace.tracer or DatadogTracer()
         self._dd_tracer.set_tags(self._config.get(keys.GLOBAL_TAGS))
         self._dd_tracer.configure(
-            enabled=self._enabled,
+            enabled=self._config.get(keys.ENABLED),
             hostname=self._config.get(keys.AGENT_HOSTNAME),
             https=self._config.get(keys.AGENT_HTTPS),
             port=self._config.get(keys.AGENT_PORT),


Test patch:
diff --git a/tests/opentracer/core/test_tracer.py b/tests/opentracer/core/test_tracer.py
--- a/tests/opentracer/core/test_tracer.py
+++ b/tests/opentracer/core/test_tracer.py
@@ -10,6 +10,7 @@
 import pytest
 
 import ddtrace
+from ddtrace import Tracer as DDTracer
 from ddtrace.ext.priority import AUTO_KEEP
 from ddtrace.opentracer import Tracer
 from ddtrace.opentracer import set_global_tracer
@@ -25,7 +26,7 @@ def test_config(self):
         tracer = Tracer(service_name="myservice", config=config)
 
         assert tracer._service_name == "myservice"
-        assert tracer._enabled is True
+        assert tracer._dd_tracer.enabled is True
 
     def test_no_service_name(self):
         """A service_name should be generated if one is not provided."""
@@ -44,10 +45,7 @@ def test_multiple_tracer_configs(self):
 
         # Ensure tracer1's config was not mutated
         assert tracer1._service_name == "serv1"
-        assert tracer1._enabled is True
-
         assert tracer2._service_name == "serv2"
-        assert tracer2._enabled is False
 
     def test_invalid_config_key(self):
         """A config with an invalid key should raise a ConfigException."""
@@ -71,6 +69,12 @@ def test_invalid_config_key(self):
             assert ["enabeld", "setttings"] in str(ce_info)
             assert tracer is not None
 
+    def test_ddtrace_fallback_config(self, monkeypatch):
+        """Ensure datadog configuration is used by default."""
+        monkeypatch.setenv("DD_TRACE_ENABLED", "false")
+        tracer = Tracer(dd_tracer=DDTracer())
+        assert tracer._dd_tracer.enabled is False
+
     def test_global_tags(self):
         """Global tags should be passed from the opentracer to the tracer."""
         config = {