Skip to content

Commit

Permalink
fix(ddtrace/tracer): ignore agent-provided TCP port when using a UDS …
Browse files Browse the repository at this point in the history
…w/ WithDogstatsdAddress (#2985)

Co-authored-by: Mikayla Toffler <[email protected]>
  • Loading branch information
darccio and mtoffl01 authored Nov 25, 2024
1 parent 58e1e63 commit 003e486
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ func newConfig(opts ...StartOption) *config {
if agentport := c.agent.StatsdPort; agentport > 0 && !c.agent.ignore {
// the agent reported a non-standard port
host, _, err := net.SplitHostPort(addr)
if err == nil {
// Use agent-reported address if it differs from the user-defined TCP-based protocol URI
if err == nil && host != "unix" {
// we have a valid host:port address; replace the port because
// the agent knows better
if host == "" {
Expand Down
70 changes: 66 additions & 4 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ func TestTracerOptionsDefaults(t *testing.T) {

t.Run("dogstatsd", func(t *testing.T) {
t.Run("default", func(t *testing.T) {
tracer := newTracer(WithAgentTimeout(2))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, c.dogstatsdAddr, "localhost:8125")
assert.Equal(t, globalconfig.DogstatsdAddr(), "localhost:8125")
})

t.Run("default:ignore", func(t *testing.T) {
tracer := newTracer(WithAgentTimeout(2), withIgnoreAgent(true))
defer tracer.Stop()
c := tracer.config
Expand All @@ -459,6 +467,15 @@ func TestTracerOptionsDefaults(t *testing.T) {
})

t.Run("env-host", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
tracer := newTracer(WithAgentTimeout(2))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, c.dogstatsdAddr, "my-host:8125")
assert.Equal(t, globalconfig.DogstatsdAddr(), "my-host:8125")
})

t.Run("env-host:ignore", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
tracer := newTracer(WithAgentTimeout(2), withIgnoreAgent(true))
defer tracer.Stop()
Expand All @@ -468,6 +485,15 @@ func TestTracerOptionsDefaults(t *testing.T) {
})

t.Run("env-port", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_PORT", "123")
tracer := newTracer(WithAgentTimeout(2))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, "localhost:8125", c.dogstatsdAddr)
assert.Equal(t, "localhost:8125", globalconfig.DogstatsdAddr())
})

t.Run("env-port:ignore", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_PORT", "123")
tracer := newTracer(WithAgentTimeout(2), withIgnoreAgent(true))
defer tracer.Stop()
Expand All @@ -479,29 +505,64 @@ func TestTracerOptionsDefaults(t *testing.T) {
t.Run("env-both", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
t.Setenv("DD_DOGSTATSD_PORT", "123")
tracer := newTracer(WithAgentTimeout(2), withIgnoreAgent(true))
tracer := newTracer(WithAgentTimeout(2))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, c.dogstatsdAddr, "my-host:123")
assert.Equal(t, globalconfig.DogstatsdAddr(), "my-host:123")
})

t.Run("env-env", func(t *testing.T) {
t.Setenv("DD_ENV", "testEnv")
t.Run("env-both:ignore", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
t.Setenv("DD_DOGSTATSD_PORT", "123")
tracer := newTracer(WithAgentTimeout(2), withIgnoreAgent(true))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, "testEnv", c.env)
assert.Equal(t, c.dogstatsdAddr, "my-host:123")
assert.Equal(t, globalconfig.DogstatsdAddr(), "my-host:123")
})

t.Run("option", func(t *testing.T) {
tracer := newTracer(WithDogstatsdAddress("10.1.0.12:4002"))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, c.dogstatsdAddr, "10.1.0.12:8125")
assert.Equal(t, globalconfig.DogstatsdAddr(), "10.1.0.12:8125")
})

t.Run("option:ignore", func(t *testing.T) {
tracer := newTracer(WithDogstatsdAddress("10.1.0.12:4002"), withIgnoreAgent(true))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, c.dogstatsdAddr, "10.1.0.12:4002")
assert.Equal(t, globalconfig.DogstatsdAddr(), "10.1.0.12:4002")
})

t.Run("env-env", func(t *testing.T) {
t.Setenv("DD_ENV", "testEnv")
tracer := newTracer(WithAgentTimeout(2))
defer tracer.Stop()
c := tracer.config
assert.Equal(t, "testEnv", c.env)
})

t.Run("uds", func(t *testing.T) {
assert := assert.New(t)
dir, err := os.MkdirTemp("", "socket")
if err != nil {
t.Fatal("Failed to create socket")
}
addr := filepath.Join(dir, "dsd.socket")
defer os.RemoveAll(addr)
tracer := newTracer(WithDogstatsdAddress("unix://" + addr))
defer tracer.Stop()
c := tracer.config
assert.NotNil(c)
assert.Equal("unix://"+addr, c.dogstatsdAddr)
assert.Equal("unix://"+addr, globalconfig.DogstatsdAddr())
})

t.Run("uds:ignore", func(t *testing.T) {
assert := assert.New(t)
dir, err := os.MkdirTemp("", "socket")
if err != nil {
Expand All @@ -512,6 +573,7 @@ func TestTracerOptionsDefaults(t *testing.T) {
tracer := newTracer(WithDogstatsdAddress("unix://"+addr), withIgnoreAgent(true))
defer tracer.Stop()
c := tracer.config
assert.NotNil(c)
assert.Equal("unix://"+addr, c.dogstatsdAddr)
assert.Equal("unix://"+addr, globalconfig.DogstatsdAddr())
})
Expand Down

0 comments on commit 003e486

Please sign in to comment.