Skip to content

Commit

Permalink
agent ignore statsdport
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahkm committed Oct 11, 2024
1 parent 2a34b15 commit 1ca34a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 15 additions & 3 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -653,16 +657,18 @@ 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 {
log.Error("Decoding features: %v", err)
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":
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
Expand Down Expand Up @@ -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{}{
Expand All @@ -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)
Expand Down

0 comments on commit 1ca34a9

Please sign in to comment.