From bff06e031b82b1d1878ad810d2f4b1ac05980493 Mon Sep 17 00:00:00 2001 From: Lee Avital Date: Tue, 16 Apr 2019 11:28:26 -0400 Subject: [PATCH] Merge pull request #293 from DataDog/lee.avital/dont_hardcode_expvar_port Make process_config.expvar configurable --- cmd/agent/main_common.go | 4 ++-- config/config.go | 2 ++ config/config_test.go | 2 ++ .../testdata/TestDDAgentConfigYamlAndNetworkConfig.yaml | 3 ++- config/yaml_config.go | 9 +++++++++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/agent/main_common.go b/cmd/agent/main_common.go index 253777a14..a756c8431 100644 --- a/cmd/agent/main_common.go +++ b/cmd/agent/main_common.go @@ -146,7 +146,7 @@ func runAgent(exit chan bool) { if opts.info { // using the debug port to get info to work - url := "http://localhost:6062/debug/vars" + url := fmt.Sprintf("http://localhost:%d/debug/vars", cfg.ProcessExpVarPort) if err := Info(os.Stdout, cfg, url); err != nil { os.Exit(1) } @@ -155,7 +155,7 @@ func runAgent(exit chan bool) { // Run a profile server. go func() { - http.ListenAndServe("localhost:6062", nil) + http.ListenAndServe(fmt.Sprintf("localhost:%d", cfg.ProcessExpVarPort), nil) }() cl, err := NewCollector(cfg) diff --git a/config/config.go b/config/config.go index e23b940a7..d29cedd90 100644 --- a/config/config.go +++ b/config/config.go @@ -68,6 +68,7 @@ type AgentConfig struct { DDAgentBin string StatsdHost string StatsdPort int + ProcessExpVarPort int // Network collection configuration EnableNetworkTracing bool @@ -154,6 +155,7 @@ func NewDefaultAgentConfig() *AgentConfig { AllowRealTime: true, HostName: "", Transport: NewDefaultTransport(), + ProcessExpVarPort: 6062, // Statsd for internal instrumentation StatsdHost: "127.0.0.1", diff --git a/config/config_test.go b/config/config_test.go index ba207b841..9f90e0282 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -165,6 +165,7 @@ func TestDefaultConfig(t *testing.T) { assert.Equal(os.Getenv("HOST_SYS"), "") os.Setenv("DOCKER_DD_AGENT", "no") assert.Equal(containerChecks, agentConfig.EnabledChecks) + assert.Equal(6062, agentConfig.ProcessExpVarPort) os.Unsetenv("DOCKER_DD_AGENT") } @@ -263,6 +264,7 @@ func TestAgentConfigYamlAndNetworkConfig(t *testing.T) { assert.Equal(100, agentConfig.Windows.ArgsRefreshInterval) assert.Equal(false, agentConfig.Windows.AddNewArgs) assert.Equal(false, agentConfig.Scrubber.Enabled) + assert.Equal(5065, agentConfig.ProcessExpVarPort) agentConfig, err = NewAgentConfig( "test", diff --git a/config/testdata/TestDDAgentConfigYamlAndNetworkConfig.yaml b/config/testdata/TestDDAgentConfigYamlAndNetworkConfig.yaml index bd34ad345..7392375f9 100644 --- a/config/testdata/TestDDAgentConfigYamlAndNetworkConfig.yaml +++ b/config/testdata/TestDDAgentConfigYamlAndNetworkConfig.yaml @@ -11,4 +11,5 @@ process_config: windows: args_refresh_interval: 100 add_new_args: false - scrub_args: false \ No newline at end of file + scrub_args: false + expvar_port: 5065 \ No newline at end of file diff --git a/config/yaml_config.go b/config/yaml_config.go index 0d0c55d83..c7212c6c3 100644 --- a/config/yaml_config.go +++ b/config/yaml_config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/pkg/errors" "net/url" "regexp" "strings" @@ -137,6 +138,14 @@ func (a *AgentConfig) loadProcessYamlConfig(path string) error { } } + if k := key(ns, "expvar_port"); config.Datadog.IsSet(k) { + port := config.Datadog.GetInt(k) + if port <= 0 { + return errors.Errorf("invalid %s -- %d", k, port) + } + a.ProcessExpVarPort = port + } + // Enable/Disable the DataScrubber to obfuscate process args if scrubArgsKey := key(ns, "scrub_args"); config.Datadog.IsSet(scrubArgsKey) { a.Scrubber.Enabled = config.Datadog.GetBool(scrubArgsKey)