From 466bf66d8bbe3c193784f7512f34b6e81027c8fa Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Thu, 7 Nov 2024 17:58:31 +0100 Subject: [PATCH] fix(options): Don't override c.httpClient if it is set via the options --- ddtrace/tracer/option.go | 20 +++++++++++--------- ddtrace/tracer/option_test.go | 13 +++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ddtrace/tracer/option.go b/ddtrace/tracer/option.go index 2e9b08c7cf..4f85dbc71f 100644 --- a/ddtrace/tracer/option.go +++ b/ddtrace/tracer/option.go @@ -454,16 +454,18 @@ func newConfig(opts ...StartOption) *config { if c.agentURL == nil { c.agentURL = internal.AgentURLFromEnv() } - if c.agentURL.Scheme == "unix" { - // If we're connecting over UDS we can just rely on the agent to provide the hostname - log.Debug("connecting to agent over unix, do not set hostname on any traces") - c.httpClient = udsClient(c.agentURL.Path, c.httpClientTimeout) - c.agentURL = &url.URL{ - Scheme: "http", - Host: fmt.Sprintf("UDS_%s", strings.NewReplacer(":", "_", "/", "_", `\`, "_").Replace(c.agentURL.Path)), + if c.httpClient == nil { + if c.agentURL.Scheme == "unix" { + // If we're connecting over UDS we can just rely on the agent to provide the hostname + log.Debug("connecting to agent over unix, do not set hostname on any traces") + c.httpClient = udsClient(c.agentURL.Path, c.httpClientTimeout) + c.agentURL = &url.URL{ + Scheme: "http", + Host: fmt.Sprintf("UDS_%s", strings.NewReplacer(":", "_", "/", "_", `\`, "_").Replace(c.agentURL.Path)), + } + } else { + c.httpClient = defaultHTTPClient(c.httpClientTimeout) } - } else if c.httpClient == nil { - c.httpClient = defaultHTTPClient(c.httpClientTimeout) } WithGlobalTag(ext.RuntimeID, globalconfig.RuntimeID())(c) globalTags := c.globalTags.get() diff --git a/ddtrace/tracer/option_test.go b/ddtrace/tracer/option_test.go index 34ebc0a846..078c3ce243 100644 --- a/ddtrace/tracer/option_test.go +++ b/ddtrace/tracer/option_test.go @@ -1510,3 +1510,16 @@ func TestWithStatsComputation(t *testing.T) { assert.True(c.statsComputationEnabled) }) } + +func TestNoHTTPClientOverride(t *testing.T) { + t.Run("default", func(t *testing.T) { + assert := assert.New(t) + client := http.DefaultClient + client.Timeout = 30 * time.Second // Default is 10s + c := newConfig( + WithHTTPClient(client), + WithUDS("/tmp/agent.sock"), + ) + assert.Equal(30*time.Second, c.httpClient.Timeout) + }) +}