From 1ca34a97f5ce465e953adbe4684bf57fdadcef5e Mon Sep 17 00:00:00 2001 From: Hannah Kim Date: Fri, 11 Oct 2024 13:53:12 -0400 Subject: [PATCH] agent ignore statsdport --- ddtrace/tracer/option.go | 18 +++++++++++++++--- ddtrace/tracer/option_test.go | 16 ++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/ddtrace/tracer/option.go b/ddtrace/tracer/option.go index 9175c20efe..4c649d56f1 100644 --- a/ddtrace/tracer/option.go +++ b/ddtrace/tracer/option.go @@ -522,7 +522,7 @@ func newConfig(opts ...StartOption) *config { // no config defined address; use defaults addr = defaultDogstatsdAddr() } - if agentport := c.agent.StatsdPort; agentport > 0 { + 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 { @@ -625,6 +625,10 @@ type agentFeatures struct { // featureFlags specifies all the feature flags reported by the trace-agent. featureFlags map[string]struct{} + + // ignore indicates that we should ignore the agent in favor of user set values. + // It should only be used during testing. + ignore bool } // HasFlag reports whether the agent has set the feat feature flag. @@ -653,8 +657,10 @@ func loadAgentFeatures(agentDisabled bool, agentURL *url.URL, httpClient *http.C type infoResponse struct { Endpoints []string `json:"endpoints"` ClientDropP0s bool `json:"client_drop_p0s"` - StatsdPort int `json:"statsd_port"` FeatureFlags []string `json:"feature_flags"` + Config struct { + StatsdPort int `json:"statsd_port"` + } `json:"config"` } var info infoResponse if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { @@ -662,7 +668,7 @@ func loadAgentFeatures(agentDisabled bool, agentURL *url.URL, httpClient *http.C return } features.DropP0s = info.ClientDropP0s - features.StatsdPort = info.StatsdPort + features.StatsdPort = info.Config.StatsdPort for _, endpoint := range info.Endpoints { switch endpoint { case "/v0.6/stats": @@ -762,6 +768,12 @@ func WithFeatureFlags(feats ...string) StartOption { } } +func withIgnoreAgent(ignore bool) StartOption { + return func(c *config) { + c.agent.ignore = ignore + } +} + // WithLogger sets logger as the tracer's error printer. // Diagnostic and startup tracer logs are prefixed to simplify the search within logs. // If JSON logging format is required, it's possible to wrap tracer logs using an existing JSON logger with this diff --git a/ddtrace/tracer/option_test.go b/ddtrace/tracer/option_test.go index d4259e9f5c..a71323feb5 100644 --- a/ddtrace/tracer/option_test.go +++ b/ddtrace/tracer/option_test.go @@ -157,19 +157,19 @@ func TestAutoDetectStatsd(t *testing.T) { t.Run("agent", func(t *testing.T) { t.Run("default", func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Write([]byte(`{"statsd_port":0}`)) + w.Write([]byte(`{"endpoints": [], "config": {"statsd_port":0}}`)) })) defer srv.Close() - cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2)) + cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2), withIgnoreAgent(true)) testStatsd(t, cfg, net.JoinHostPort(defaultHostname, "8125")) }) t.Run("port", func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Write([]byte(`{"statsd_port":8999}`)) + w.Write([]byte(`{"endpoints": [], "config": {"statsd_port":8999}}`)) })) defer srv.Close() - cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://"))) + cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), withIgnoreAgent(true)) testStatsd(t, cfg, net.JoinHostPort(defaultHostname, "8999")) }) }) @@ -211,10 +211,10 @@ func TestLoadAgentFeatures(t *testing.T) { t.Run("OK", func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Write([]byte(`{"endpoints":["/v0.6/stats"],"feature_flags":["a","b"],"client_drop_p0s":true,"statsd_port":8999}`)) + w.Write([]byte(`{"endpoints":["/v0.6/stats"],"feature_flags":["a","b"],"client_drop_p0s":true,"config": {"statsd_port":8999}}`)) })) defer srv.Close() - cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2)) + cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2), withIgnoreAgent(true)) assert.True(t, cfg.agent.DropP0s) assert.Equal(t, cfg.agent.StatsdPort, 8999) assert.EqualValues(t, cfg.agent.featureFlags, map[string]struct{}{ @@ -229,10 +229,10 @@ func TestLoadAgentFeatures(t *testing.T) { t.Run("discovery", func(t *testing.T) { t.Setenv("DD_TRACE_FEATURES", "discovery") srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Write([]byte(`{"endpoints":["/v0.6/stats"],"client_drop_p0s":true,"statsd_port":8999}`)) + w.Write([]byte(`{"endpoints":["/v0.6/stats"],"client_drop_p0s":true,"config":{"statsd_port":8999}}`)) })) defer srv.Close() - cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2)) + cfg := newConfig(WithAgentAddr(strings.TrimPrefix(srv.URL, "http://")), WithAgentTimeout(2), withIgnoreAgent(true)) assert.True(t, cfg.agent.DropP0s) assert.True(t, cfg.agent.Stats) assert.Equal(t, 8999, cfg.agent.StatsdPort)