From c866acb20171d027dd2be4d93b2b15dcc34ef93b Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Tue, 14 Jan 2025 13:59:30 -0500 Subject: [PATCH] implement dd_dogstatsd_host --- contrib/net/http/trace_test.go | 4 +-- ddtrace/tracer/option.go | 5 ++- ddtrace/tracer/option_test.go | 56 +++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/contrib/net/http/trace_test.go b/contrib/net/http/trace_test.go index f85a592861..0489f8c3ef 100644 --- a/contrib/net/http/trace_test.go +++ b/contrib/net/http/trace_test.go @@ -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) { diff --git a/ddtrace/tracer/option.go b/ddtrace/tracer/option.go index c1ad01edf9..43240b93e3 100644 --- a/ddtrace/tracer/option.go +++ b/ddtrace/tracer/option.go @@ -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 diff --git a/ddtrace/tracer/option_test.go b/ddtrace/tracer/option_test.go index 02146593e1..7c842c9102 100644 --- a/ddtrace/tracer/option_test.go +++ b/ddtrace/tracer/option_test.go @@ -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() @@ -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...) @@ -515,9 +524,10 @@ 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 @@ -525,9 +535,10 @@ func TestTracerOptionsDefaults(t *testing.T) { 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() @@ -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")