Skip to content

Commit

Permalink
ddtrace/tracer: resolve agent addresses more accurately. (DataDog#258)
Browse files Browse the repository at this point in the history
This change ensures that a larger flexibility is permitted when
specifying host[:port] address for the agent. Previously, unexpected
behaviour happened when specifying only the hostname, as an example.
  • Loading branch information
gbbr authored and mingrammer committed Dec 22, 2020
1 parent faf3154 commit 88e56c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
27 changes: 18 additions & 9 deletions ddtrace/tracer/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,8 @@ func newHTTPTransport(addr string) *httpTransport {
"Datadog-Meta-Tracer-Version": tracerVersion,
"Content-Type": "application/msgpack",
}
host, port, _ := net.SplitHostPort(addr)
if host == "" {
host = defaultHostname
}
if port == "" {
port = defaultPort
}
addr = fmt.Sprintf("%s:%s", host, port)
return &httpTransport{
traceURL: fmt.Sprintf("http://%s/v0.3/traces", addr),
traceURL: fmt.Sprintf("http://%s/v0.3/traces", resolveAddr(addr)),
client: &http.Client{
// We copy the transport to avoid using the default one, as it might be
// augmented with tracing and we don't want these calls to be recorded.
Expand Down Expand Up @@ -118,3 +110,20 @@ func (t *httpTransport) send(p *payload) error {
}
return nil
}

// resolveAddr resolves the given agent address and fills in any missing host
// and port using the defaults.
func resolveAddr(addr string) string {
host, port, err := net.SplitHostPort(addr)
if err != nil {
// no port in addr
host = addr
}
if host == "" {
host = defaultHostname
}
if port == "" {
port = defaultPort
}
return fmt.Sprintf("%s:%s", host, port)
}
17 changes: 17 additions & 0 deletions ddtrace/tracer/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ func TestTracesAgentIntegration(t *testing.T) {
}
}

func TestResolveAddr(t *testing.T) {
for _, tt := range []struct {
in, out string
}{
{"host", fmt.Sprintf("host:%s", defaultPort)},
{"www.my-address.com", fmt.Sprintf("www.my-address.com:%s", defaultPort)},
{"localhost", fmt.Sprintf("localhost:%s", defaultPort)},
{":1111", fmt.Sprintf("%s:1111", defaultHostname)},
{"", defaultAddress},
{"custom:1234", "custom:1234"},
} {
t.Run(tt.in, func(t *testing.T) {
assert.Equal(t, resolveAddr(tt.in), tt.out)
})
}
}

func TestTransportResponseError(t *testing.T) {
assert := assert.New(t)
ln, err := net.Listen("tcp4", ":0")
Expand Down

0 comments on commit 88e56c5

Please sign in to comment.