refactor code: get hostname

This commit is contained in:
Ulric Qin 2022-05-31 16:53:21 +08:00
parent 56f98afc6a
commit 23873a2be1
3 changed files with 86 additions and 31 deletions

View File

@ -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()
}
}

View File

@ -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,38 +83,34 @@ 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)
}
if strings.Contains(c.Global.Hostname, "$ip") {
ip, err := GetOutboundIP()
if err != nil {
return err
}
c.Global.Hostname = strings.Replace(c.Global.Hostname, "$ip", fmt.Sprint(ip), -1)
ip, err := GetOutboundIP()
if err != nil {
return err
}
c.Global.IP = fmt.Sprint(ip)
return nil
}
func (c *ConfigType) GetHostname() string {
ret := c.Global.Hostname
name := Hostname.Get()
if ret == "" {
return name
}
ret = strings.Replace(ret, "$hostname", name, -1)
ret = strings.Replace(ret, "$ip", c.Global.IP, -1)
return ret
}
func GetInterval() time.Duration {
if Config.Global.Interval <= 0 {
return time.Second * 15
@ -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")

59
config/hostname.go Normal file
View File

@ -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)
}
}
}