Repository: DataDog/dd-trace-py

Issue:
Datadog k8s Admission Controller makes using Unix Domain Socket impossible
### Which version of dd-trace-py are you using?

1.0.2

### Which version of pip are you using?

21.3.1

### Which version of the libraries are you using?

datadog==0.44.0
ddtrace==1.0.2

### How can we reproduce your problem?

[Enable the Admission Controller](https://github.com/DataDog/helm-charts/blob/main/charts/datadog/values.yaml#L694) in the Datadog helm chart: 
```
clusterAgent.admissionController.enabled=true
```

Start up a container with `ddtrace` installed:
```
# kubectl run ... -- bash
```

The Datadog Admission Controller automagically sets various Datadog `DD_`* environment variables on the container.
```
# env | grep DD_
DD_AGENT_HOST=10.103.1.28
DD_ENTITY_ID=3aabef08-143d-4396-9b51-9811354f6adf
```

Try and start python with ddtrace:
```
# DD_TRACE_STARTUP_LOGS=true ddtrace-run python
- DATADOG TRACER DIAGNOSTIC - Agent not reachable at http://10.103.1.28:8126. Exception raised: [Errno 111] Connection refused
Python 3.8.3 (default, May 19 2020, 18:47:26)
...
```

Set `DD_TRACE_AGENT_URL` to explicitly use UDS:
```
# DD_TRACE_STARTUP_LOGS=true DD_TRACE_AGENT_URL=unix:///run/datadog/apm.socket  ddtrace-run python
- DATADOG TRACER DIAGNOSTIC - Agent not reachable at http://10.103.1.28:None. Exception raised: Port could not be cast to integer value as 'None'
Python 3.8.3 (default, May 19 2020, 18:47:26)
...
```

Note how the error has changed, so setting `DD_TRACE_AGENT_URL` has an effect but does not switch ddtrace to using UDS. 

Remove the `DD_AGENT_HOST` env var and try again:
```
# unset DD_AGENT_HOST
# DD_TRACE_STARTUP_LOGS=true DD_TRACE_AGENT_URL=unix:///run/datadog/apm.socket  ddtrace-run python
Python 3.8.3 (default, May 19 2020, 18:47:26)
...
```

Now it works. And setting `DD_TRACE_AGENT_URL` is not required:
```
# DD_TRACE_STARTUP_LOGS=true  ddtrace-run python
Python 3.8.3 (default, May 19 2020, 18:47:26)
...
```

I want the other environment variables the Admission Controller sets up, but not `DD_AGENT_HOST`. I thought I could achieve this and work around the issue by setting the `DD_AGENT_HOST` to empty string in the pod manifest. But surprisingly that still did not work. This still produces `Agent not reachable at http://10.103.1.28:8126. Exception raised: [Errno 111] Connection refused`. 

If I disable the Datadog Admission Controller then it works. But obviously I have to manually reproduce all the other env vars it would add for me.

### What is the result that you expected?

That there is a way to use the Datadog Admission Controller with ddtrace. 

I thought I found the appropriate place in the [code where ddtrace figures out how to connect](https://github.com/DataDog/dd-trace-py/blob/fb8dfa2f33fff37d21df9728d8386c0260df9744/ddtrace/internal/agent.py#L65), and it has the correct logic. But I assume this is not actually the code doing this because it does not match the results I get above.

One further point. I was hoping that `DD_TRACE_DEBUG` would help debug how it was trying to connect but it doesn't report anything useful in this area. 




Discussion:
hey @damonmaria sorry for the long delay getting back to you here, I was able to reproduce this issue.

It seems when we initially create an instance of our tracer we get the correct value for the agent URL, but then our `sitecustomize.py` that is added by `ddtrace-run` will reconfigure the tracer, but it will explicitly pass in the hostname which will cause us to re-create the agent_url used by the tracer.

Resolving patch:
diff --git a/ddtrace/bootstrap/sitecustomize.py b/ddtrace/bootstrap/sitecustomize.py
--- a/ddtrace/bootstrap/sitecustomize.py
+++ b/ddtrace/bootstrap/sitecustomize.py
@@ -78,9 +78,6 @@ def update_patched_modules():
 try:
     from ddtrace import tracer
 
-    dd_hostname = os.getenv("DD_TRACE_AGENT_HOSTNAME")
-    hostname = os.getenv("DD_AGENT_HOST", dd_hostname)
-    port = os.getenv("DD_TRACE_AGENT_PORT")
     priority_sampling = os.getenv("DD_PRIORITY_SAMPLING")
     profiling = asbool(os.getenv("DD_PROFILING_ENABLED", False))
 
@@ -100,10 +97,6 @@ def update_patched_modules():
         trace_enabled = False
         opts["enabled"] = False
 
-    if hostname:
-        opts["hostname"] = hostname
-    if port:
-        opts["port"] = int(port)
     if priority_sampling:
         opts["priority_sampling"] = asbool(priority_sampling)
 
diff --git a/ddtrace/internal/agent.py b/ddtrace/internal/agent.py
--- a/ddtrace/internal/agent.py
+++ b/ddtrace/internal/agent.py
@@ -24,7 +24,7 @@
 
 def get_trace_hostname(default=DEFAULT_HOSTNAME):
     # type: (Union[T, str]) -> Union[T, str]
-    return os.environ.get("DD_AGENT_HOST", os.environ.get("DATADOG_TRACE_AGENT_HOSTNAME", default))
+    return os.environ.get("DD_AGENT_HOST", os.environ.get("DD_TRACE_AGENT_HOSTNAME", default))
 
 
 def get_stats_hostname(default=DEFAULT_HOSTNAME):


Test patch: