Skip to content

Commit

Permalink
refactor code: get hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed May 31, 2022
1 parent 56f98af commit 23873a2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 29 deletions.
2 changes: 1 addition & 1 deletion agent/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
52 changes: 24 additions & 28 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net"
"os"
"path"
"strings"
"time"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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")
Expand Down
59 changes: 59 additions & 0 deletions config/hostname.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 23873a2

Please sign in to comment.