Skip to content

Commit

Permalink
implement dd_dogstatsd_host
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoffl01 committed Jan 14, 2025
1 parent 471d723 commit c866acb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
4 changes: 2 additions & 2 deletions contrib/net/http/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ func TestTraceAndServe(t *testing.T) {
t.Setenv("DD_TRACE_HTTP_SERVER_ERROR_STATUSES", "500")

cfg := &ServeConfig{
Service: "service",
Resource: "resource",
Service: "service",
Resource: "resource",
}

handler := func(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 4 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,10 @@ func udsClient(socketPath string, timeout time.Duration) *http.Client {

// defaultDogstatsdAddr returns the default connection address for Dogstatsd.
func defaultDogstatsdAddr() string {
envHost, envPort := os.Getenv("DD_AGENT_HOST"), os.Getenv("DD_DOGSTATSD_PORT")
envHost, envPort := os.Getenv("DD_DOGSTATSD_HOST"), os.Getenv("DD_DOGSTATSD_PORT")
if envHost == "" {
envHost = os.Getenv("DD_AGENT_HOST")
}
if _, err := os.Stat(defaultSocketDSD); err == nil && envHost == "" && envPort == "" {
// socket exists and user didn't specify otherwise via env vars
return "unix://" + defaultSocketDSD
Expand Down
56 changes: 49 additions & 7 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func TestTracerOptionsDefaults(t *testing.T) {
assert.Equal(t, "localhost:8125", globalconfig.DogstatsdAddr())
})

t.Run("env-host", func(t *testing.T) {
t.Run("env-agent_host", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
tracer := newTracer(opts...)
defer tracer.Stop()
Expand All @@ -495,6 +495,15 @@ func TestTracerOptionsDefaults(t *testing.T) {
assert.Equal(t, "my-host:8125", globalconfig.DogstatsdAddr())
})

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

t.Run("env-port", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_PORT", "123")
tracer := newTracer(opts...)
Expand All @@ -515,19 +524,21 @@ func TestTracerOptionsDefaults(t *testing.T) {
fail = false
})

t.Run("env-both", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
t.Run("env-all", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_HOST", "my-host")
t.Setenv("DD_DOGSTATSD_PORT", "123")
t.Setenv("DD_AGENT_HOST", "other-host")
tracer := newTracer(opts...)
defer tracer.Stop()
c := tracer.config
assert.Equal(t, "my-host:8125", c.dogstatsdAddr)
assert.Equal(t, "my-host:8125", globalconfig.DogstatsdAddr())
})

t.Run("env-both: agent not available", func(t *testing.T) {
t.Setenv("DD_AGENT_HOST", "my-host")
t.Run("env-all: agent not available", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_HOST", "my-host")
t.Setenv("DD_DOGSTATSD_PORT", "123")
t.Setenv("DD_AGENT_HOST", "other-host")
fail = true
tracer := newTracer(opts...)
defer tracer.Stop()
Expand Down Expand Up @@ -917,12 +928,43 @@ func TestDefaultDogstatsdAddr(t *testing.T) {
assert.Equal(t, defaultDogstatsdAddr(), "localhost:8125")
})

t.Run("env", func(t *testing.T) {
t.Run("host-env", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_HOST", "111.111.1.1")
t.Setenv("DD_AGENT_HOST", "222.222.2.2")
assert.Equal(t, "111.111.1.1:8125", defaultDogstatsdAddr())
})

t.Run("port-env", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_PORT", "8111")
assert.Equal(t, defaultDogstatsdAddr(), "localhost:8111")
t.Setenv("DD_AGENT_HOST", "222.222.2.2")
assert.Equal(t, defaultDogstatsdAddr(), "222.222.2.2:8111")
})

t.Run("host-env+port-env", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_HOST", "111.111.1.1")
t.Setenv("DD_DOGSTATSD_PORT", "8888")
t.Setenv("DD_AGENT_HOST", "222.222.2.2")
assert.Equal(t, "111.111.1.1:8888", defaultDogstatsdAddr())
})

t.Run("host-env+socket", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_HOST", "111.111.1.1")
assert.Equal(t, "111.111.1.1:8125", defaultDogstatsdAddr())
f, err := os.CreateTemp("", "dsd.socket")
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(f.Name())
defer func(old string) { defaultSocketDSD = old }(defaultSocketDSD)
defaultSocketDSD = f.Name()
assert.Equal(t, "111.111.1.1:8125", defaultDogstatsdAddr())
})

t.Run("env+socket", func(t *testing.T) {
t.Run("port-env+socket", func(t *testing.T) {
t.Setenv("DD_DOGSTATSD_PORT", "8111")
assert.Equal(t, defaultDogstatsdAddr(), "localhost:8111")
f, err := ioutil.TempFile("", "dsd.socket")
Expand Down

0 comments on commit c866acb

Please sign in to comment.