From 23873a2be16fdbf0c68dba2ea11e11b94a0302cf Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Tue, 31 May 2022 16:53:21 +0800 Subject: [PATCH] refactor code: get hostname --- agent/reader.go | 2 +- config/config.go | 52 +++++++++++++++++++--------------------- config/hostname.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 config/hostname.go diff --git a/agent/reader.go b/agent/reader.go index 2cf9ec66..32d989ed 100644 --- a/agent/reader.go +++ b/agent/reader.go @@ -186,7 +186,7 @@ func convert(item *types.Sample) *prompb.TimeSeries { // add label: agent_hostname if _, has := item.Labels[agentHostnameLabelKey]; !has { if !config.Config.Global.OmitHostname { - item.Labels[agentHostnameLabelKey] = config.Config.Global.Hostname + item.Labels[agentHostnameLabelKey] = config.Config.GetHostname() } } diff --git a/config/config.go b/config/config.go index be10ebd2..cbc649b6 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net" - "os" "path" "strings" "time" @@ -16,6 +15,7 @@ import ( type Global struct { PrintConfigs bool `toml:"print_configs"` Hostname string `toml:"hostname"` + IP string `toml:"-"` OmitHostname bool `toml:"omit_hostname"` Labels map[string]string `toml:"labels"` Precision string `toml:"precision"` @@ -67,7 +67,11 @@ func InitConfig(configDir string, debugMode bool, testMode bool) error { return fmt.Errorf("failed to load configs of dir: %s", configDir) } - if err := Config.fillHostname(); err != nil { + if err := Config.fillIP(); err != nil { + return err + } + + if err := InitHostname(); err != nil { return err } @@ -79,36 +83,32 @@ func InitConfig(configDir string, debugMode bool, testMode bool) error { return nil } -func (c *ConfigType) fillHostname() error { - if c.Global.Hostname == "" { - name, err := GetHostname() - if err != nil { - return err - } - - c.Global.Hostname = name +func (c *ConfigType) fillIP() error { + if !strings.Contains(c.Global.Hostname, "$ip") { return nil } - if strings.Contains(c.Global.Hostname, "$hostname") { - name, err := GetHostname() - if err != nil { - return err - } - - c.Global.Hostname = strings.Replace(c.Global.Hostname, "$hostname", name, -1) + ip, err := GetOutboundIP() + if err != nil { + return err } - if strings.Contains(c.Global.Hostname, "$ip") { - ip, err := GetOutboundIP() - if err != nil { - return err - } + c.Global.IP = fmt.Sprint(ip) + return nil +} - c.Global.Hostname = strings.Replace(c.Global.Hostname, "$ip", fmt.Sprint(ip), -1) +func (c *ConfigType) GetHostname() string { + ret := c.Global.Hostname + + name := Hostname.Get() + if ret == "" { + return name } - return nil + ret = strings.Replace(ret, "$hostname", name, -1) + ret = strings.Replace(ret, "$ip", c.Global.IP, -1) + + return ret } func GetInterval() time.Duration { @@ -119,10 +119,6 @@ func GetInterval() time.Duration { return time.Duration(Config.Global.Interval) } -func GetHostname() (string, error) { - return os.Hostname() -} - // Get preferred outbound ip of this machine func GetOutboundIP() (net.IP, error) { conn, err := net.Dial("udp", "8.8.8.8:80") diff --git a/config/hostname.go b/config/hostname.go new file mode 100644 index 00000000..b5215414 --- /dev/null +++ b/config/hostname.go @@ -0,0 +1,59 @@ +package config + +import ( + "log" + "os" + "sync" + "time" +) + +type HostnameCache struct { + name string + sync.RWMutex +} + +var Hostname *HostnameCache + +func (c *HostnameCache) Get() string { + c.RLock() + n := c.name + c.RUnlock() + return n +} + +func (c *HostnameCache) Set(name string) { + if name == c.Get() { + return + } + + c.Lock() + c.name = name + c.Unlock() +} + +func InitHostname() error { + hostname, err := os.Hostname() + if err != nil { + return err + } + + Hostname = &HostnameCache{ + name: hostname, + } + + go loopUpdateHostname() + + return nil +} + +func loopUpdateHostname() { + for { + time.Sleep(time.Second) + name, err := os.Hostname() + if err != nil { + log.Println("E! failed to get hostname:", err) + } else { + Hostname.Set(name) + } + } +}