Skip to content

Commit

Permalink
Merge pull request #615 from kongfei605/ip_refresh
Browse files Browse the repository at this point in the history
refresh ip info 1 min and make cloudwatch more debug log
  • Loading branch information
kongfei605 committed Aug 21, 2023
2 parents bb1eace + 391937b commit 3a63db5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
34 changes: 15 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var envVarEscaper = strings.NewReplacer(
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 @@ -157,11 +156,7 @@ func InitConfig(configDir string, debugMode, testMode bool, interval int64, inpu

Config.Global.Hostname = strings.TrimSpace(Config.Global.Hostname)

if err := Config.fillIP(); err != nil {
return err
}

if err := InitHostname(); err != nil {
if err := InitHostInfo(); err != nil {
return err
}

Expand All @@ -187,30 +182,28 @@ func InitConfig(configDir string, debugMode, testMode bool, interval int64, inpu
return nil
}

func (c *ConfigType) fillIP() error {
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()
name := HostInfo.GetHostname()
if ret == "" {
return name
}

ret = strings.Replace(ret, "$hostname", name, -1)
ret = strings.Replace(ret, "$ip", c.Global.IP, -1)
ret = strings.Replace(ret, "$ip", c.GetHostIP(), -1)
ret = os.Expand(ret, GetEnv)

return ret
}
func (c *ConfigType) GetHostIP() string {
ret := HostInfo.GetIP()
if ret == "" {
return c.GetHostname()
}

return ret
}

func GetEnv(key string) string {
v := os.Getenv(key)
Expand Down Expand Up @@ -275,6 +268,9 @@ func GetOutboundIP() (net.IP, error) {
log.Printf("W! parse writers url %s error %s", v.Url, err)
continue
} else {
if strings.Contains(u.Host, "localhost") || strings.Contains(u.Host, "127.0.0.1") {
continue
}
if len(u.Port()) == 0 {
if u.Scheme == "http" {
u.Host = u.Host + ":80"
Expand Down Expand Up @@ -314,7 +310,7 @@ func GlobalLabels() map[string]string {

func Expand(nv string) string {
nv = strings.Replace(nv, "$hostname", Config.GetHostname(), -1)
nv = strings.Replace(nv, "$ip", Config.Global.IP, -1)
nv = strings.Replace(nv, "$ip", Config.GetHostIP(), -1)
nv = os.Expand(nv, GetEnv)
return nv
}
53 changes: 42 additions & 11 deletions config/hostname.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package config

import (
"fmt"
"log"
"os"
"sync"
"time"
)

type HostnameCache struct {
type HostInfoCache struct {
name string
ip string
sync.RWMutex
}

var Hostname *HostnameCache
var HostInfo *HostInfoCache

func (c *HostnameCache) Get() string {
func (c *HostInfoCache) GetHostname() string {
c.RLock()
n := c.name
c.RUnlock()
return n
}

func (c *HostnameCache) Set(name string) {
if name == c.Get() {
func (c *HostInfoCache) GetIP() string {
c.RLock()
defer c.RUnlock()
ip := c.ip
return ip
}

func (c *HostInfoCache) SetHostname(name string) {
if name == c.GetHostname() {
return
}

Expand All @@ -31,29 +40,51 @@ func (c *HostnameCache) Set(name string) {
c.Unlock()
}

func InitHostname() error {
func (c *HostInfoCache) SetIP(ip string) {
if ip == c.GetIP() {
return
}

c.Lock()
c.ip = ip
c.Unlock()
}

func InitHostInfo() error {
hostname, err := os.Hostname()
if err != nil {
return err
}

Hostname = &HostnameCache{
ip, err := GetOutboundIP()
if err != nil {
return err
}

HostInfo = &HostInfoCache{
name: hostname,
ip: fmt.Sprint(ip),
}

go Hostname.update()
go HostInfo.update()

return nil
}

func (c *HostnameCache) update() {
func (c *HostInfoCache) update() {
for {
time.Sleep(time.Second)
time.Sleep(time.Minute)
name, err := os.Hostname()
if err != nil {
log.Println("E! failed to get hostname:", err)
} else {
Hostname.Set(name)
HostInfo.SetHostname(name)
}
ip, err := GetOutboundIP()
if err != nil {
log.Println("E! failed to get ip:", err)
} else {
HostInfo.SetIP(fmt.Sprint(ip))
}
}
}
9 changes: 6 additions & 3 deletions inputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (ins *Instance) Gather(slist *internalTypes.SampleList) {
// Get all of the possible queries so we can send groups of 100.
queries := ins.getDataQueries(filteredMetrics)
if len(queries) == 0 {
log.Println("E! data queries length is 0")
log.Printf("E! data queries length is 0, namespaces:%+v", ins.Namespaces)
return
}

Expand Down Expand Up @@ -310,6 +310,9 @@ type filteredMetric struct {
// getFilteredMetrics returns metrics specified in the config file or metrics listed from Cloudwatch.
func getFilteredMetrics(c *Instance) ([]filteredMetric, error) {
if c.metricCache != nil && c.metricCache.isValid() {
if config.Config.DebugMode {
log.Printf("D! use filtered metrics cache for namespace %+v", c.Namespaces)
}
return c.metricCache.metrics, nil
}

Expand Down Expand Up @@ -511,12 +514,12 @@ func (ins *Instance) getDataQueries(filteredMetrics []filteredMetric) map[string

if len(dataQueries) == 0 {
if config.Config.DebugMode {
log.Println("D! no metrics found to collect")
log.Printf("D! no metrics found to collect for namespace:%+v", ins.Namespaces)
}
return nil
}

if ins.metricCache == nil {
if ins.metricCache == nil || !ins.metricCache.isValid() {
ins.metricCache = &metricCache{
queries: dataQueries,
built: time.Now(),
Expand Down

0 comments on commit 3a63db5

Please sign in to comment.