Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
[hostname] collect directly from agent6 if possible
Browse files Browse the repository at this point in the history
[hostname] later values take precedence for env vars
  • Loading branch information
truthbk authored and olivielpeau committed Feb 26, 2018
1 parent 5d83dcd commit 3336476
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
30 changes: 22 additions & 8 deletions config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type AgentConfig struct {

// transaction analytics
AnalyzedRateByService map[string]float64

// infrastructure agent binary
DDAgentBin string // DDAgentBin will be "" for Agent5 scenarios
}

// NewDefaultAgentConfig returns a configuration with the default values
Expand Down Expand Up @@ -117,16 +120,27 @@ func NewDefaultAgentConfig() *AgentConfig {

// getHostname shells out to obtain the hostname used by the infra agent
// falling back to os.Hostname() if it is unavailable
func getHostname() (string, error) {
ddAgentPy := "/opt/datadog-agent/embedded/bin/python"
getHostnameCmd := "from utils.hostname import get_hostname; print get_hostname()"

cmd := exec.Command(ddAgentPy, "-c", getHostnameCmd)
cmd.Env = []string{"PYTHONPATH=/opt/datadog-agent/agent"}
func getHostname(ddAgentBin string) (string, error) {
var cmd *exec.Cmd

// In Agent 6 we will have an Agent binary defined.
if ddAgentBin != "" {
cmd = exec.Command(ddAgentBin, "hostname")
cmd.Env = []string{}
} else {
getHostnameCmd := "from utils.hostname import get_hostname; print get_hostname()"
cmd = exec.Command(defaultDDAgentPy, "-c", getHostnameCmd)
cmd.Env = []string{defaultDDAgentPyEnv}
}

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Copying all environment variables to child process
// Windows: Required, so the child process can load DLLs, etc.
// Linux: Optional, but will make use of DD_HOSTNAME and DOCKER_DD_AGENT if they exist
osEnv := os.Environ()
cmd.Env = append(osEnv, cmd.Env...)

err := cmd.Run()
if err != nil {
Expand All @@ -140,6 +154,7 @@ func getHostname() (string, error) {
}

return hostname, nil

}

// NewAgentConfig creates the AgentConfig from the standard config
Expand Down Expand Up @@ -179,9 +194,8 @@ func NewAgentConfig(conf *File, legacyConf *File, agentYaml *YamlAgentConfig) (*
}

// If hostname isn't provided in the configuration, try to guess it.
// TODO: rely on Agent 6 code instead.
if c.HostName == "" {
hostname, err := getHostname()
hostname, err := getHostname(c.DDAgentBin)
if err != nil {
return c, errors.New("failed to automatically set the hostname, you must specify it via configuration for or the DD_HOSTNAME env var")
}
Expand Down
13 changes: 11 additions & 2 deletions config/agent_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@

package config

// DefaultLogFilePath is where the agent will write logs if not overriden in the conf
const DefaultLogFilePath = "/var/log/datadog/trace-agent.log"
const (
// DefaultLogFilePath is where the agent will write logs if not overriden in the conf
DefaultLogFilePath = "/var/log/datadog/trace-agent.log"

// Agent 5
defaultDDAgentPy = "/opt/datadog-agent/embedded/bin/python"
defaultDDAgentPyEnv = "PYTHONPATH=/opt/datadog-agent/agent"

// Agent 6
defaultDDAgentBin = "/opt/datadog-agent/bin/agent/agent"
)
6 changes: 4 additions & 2 deletions config/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ func TestConfigNewIfExists(t *testing.T) {
}

func TestGetHostname(t *testing.T) {
h, err := getHostname()
h, err := getHostname("")
assert.Nil(t, err)
assert.NotEqual(t, "", h)

host, _ := os.Hostname()
assert.Equal(t, host, h)
}

func TestUndocumentedIni(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions config/agent_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package config

// DefaultLogFilePath is where the agent will write logs if not overriden in the conf
const DefaultLogFilePath = "c:\\programdata\\datadog\\logs\\trace-agent.log"
const (
// DefaultLogFilePath is where the agent will write logs if not overriden in the conf
DefaultLogFilePath = "c:\\programdata\\datadog\\logs\\trace-agent.log"

// Agent 5
defaultDDAgentPy = "c:\\Program Files\\Datadog\\Datadog Agent\\embedded\\python.exe"
defaultDDAgentPyEnv = "PYTHONPATH=c:\\Program Files\\Datadog\\Datadog Agent\\agent"

// Agent 6
defaultDDAgentBin = "c:\\Program Files\\Datadog\\Datadog Agent\\embedded\\agent.exe"
)
8 changes: 8 additions & 0 deletions config/merge_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type traceAgent struct {
StatsWriter statsWriter `yaml:"stats_writer"`

AnalyzedRateByService map[string]float64 `yaml:"analyzed_rate_by_service"`

DDAgentBin string `yaml:"dd_agent_bin"`
}

type traceWriter struct {
Expand Down Expand Up @@ -191,6 +193,12 @@ func mergeYamlConfig(agentConf *AgentConfig, yc *YamlAgentConfig) error {
// undocumented
agentConf.AnalyzedRateByService = yc.TraceAgent.AnalyzedRateByService

// undocumented
agentConf.DDAgentBin = defaultDDAgentBin
if yc.TraceAgent.DDAgentBin != "" {
agentConf.DDAgentBin = yc.TraceAgent.DDAgentBin
}

return nil
}

Expand Down

0 comments on commit 3336476

Please sign in to comment.