Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: resolve agent addresses more accurately. #258

Merged
merged 2 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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