Skip to content

Commit

Permalink
ddtrace/tracer: make use of DD_AGENT_HOST and DD_TRACE_AGENT_PORT (#362)
Browse files Browse the repository at this point in the history
This change enables the transport to override host and port information
by reading the environment variables DD_AGENT_HOST and
DD_TRACE_AGENT_PORT.
  • Loading branch information
gbbr committed Nov 20, 2018
1 parent 13ea12b commit debe776
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
10 changes: 9 additions & 1 deletion ddtrace/tracer/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -114,7 +115,8 @@ func (t *httpTransport) send(p *payload) error {
}

// resolveAddr resolves the given agent address and fills in any missing host
// and port using the defaults.
// and port using the defaults. Some environment variable settings will
// take precedence over configuration.
func resolveAddr(addr string) string {
host, port, err := net.SplitHostPort(addr)
if err != nil {
Expand All @@ -127,5 +129,11 @@ func resolveAddr(addr string) string {
if port == "" {
port = defaultPort
}
if v := os.Getenv("DD_AGENT_HOST"); v != "" {
host = v
}
if v := os.Getenv("DD_TRACE_AGENT_PORT"); v != "" {
port = v
}
return fmt.Sprintf("%s:%s", host, port)
}
34 changes: 26 additions & 8 deletions ddtrace/tracer/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,34 @@ func TestTracesAgentIntegration(t *testing.T) {

func TestResolveAddr(t *testing.T) {
for _, tt := range []struct {
in, out string
in, envHost, envPort, 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"},
{"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"},
{"", "", "", defaultAddress},
{"", "ip.local", "", fmt.Sprintf("ip.local:%s", defaultPort)},
{"", "", "1234", fmt.Sprintf("%s:1234", defaultHostname)},
{"", "ip.local", "1234", "ip.local:1234"},
{"ip.other", "ip.local", "", fmt.Sprintf("ip.local:%s", defaultPort)},
{"ip.other:1234", "ip.local", "", "ip.local:1234"},
{":8888", "", "1234", fmt.Sprintf("%s:1234", defaultHostname)},
{"ip.other:8888", "", "1234", "ip.other:1234"},
{"ip.other", "ip.local", "1234", "ip.local:1234"},
{"ip.other:8888", "ip.local", "1234", "ip.local:1234"},
} {
t.Run(tt.in, func(t *testing.T) {
t.Run("", func(t *testing.T) {
if tt.envHost != "" {
os.Setenv("DD_AGENT_HOST", tt.envHost)
defer os.Unsetenv("DD_AGENT_HOST")
}
if tt.envPort != "" {
os.Setenv("DD_TRACE_AGENT_PORT", tt.envPort)
defer os.Unsetenv("DD_TRACE_AGENT_PORT")
}
assert.Equal(t, resolveAddr(tt.in), tt.out)
})
}
Expand Down

0 comments on commit debe776

Please sign in to comment.