Skip to content

Commit

Permalink
fix - yaml config for new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Oct 24, 2023
1 parent ca9ae90 commit 657fbc8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
8 changes: 5 additions & 3 deletions agent/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import (

func Load() {
cnf := config.GetInstance()
cnf.Load()
if err := cnf.Load(); err != nil {
return
}

for {
// Flush old metrics:
Expand All @@ -62,7 +64,7 @@ func Load() {
}

// Wait loop:
log.DebugWithFields("Wait until next collect metrics", log.Fields{"interval": cnf.General.Interval * time.Second})
time.Sleep(config.GetInstance().General.Interval * time.Second)
log.DebugWithFields("Wait until next collect metrics", log.Fields{"interval": time.Duration(cnf.General.Interval) * time.Second})
time.Sleep(time.Duration(cnf.General.Interval) * time.Second)
}
}
12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@ func GetInstance() *Config {
return this
}

func (c *Config) Load() {
func (c *Config) Load() error {
source := file.ReadExpandEnvAsString(c.Path)

if err := yaml.Unmarshal([]byte(source), &c); err != nil {
log.ErrorWithFields("Config", log.Fields{"message": "Imposible to parse config file", "file": c.Path})
return
log.ErrorWithFields("Config", log.Fields{"error": err, "file": c.Path})
return err
}

c.SanityCheck()

return nil
}

// SanityCheck verify the minimum config settings and set default values to start.
func (c *Config) SanityCheck() {
if c.General.Interval < 3 {
c.General.Interval = 10

log.Error("Use positive value, and minimun start from 3 seconds, using default 10 seconds.")
log.Warning("Use positive value, and minimun start from 3 seconds, using default 10 seconds.")
}

if len(c.General.Hostname) == 0 {
c.General.Hostname = net.Hostname()

log.Error("general.hostname: Custom value is not set, using current.")
log.Warning("general.hostname: Custom value is not set, using current.")
}
}
10 changes: 3 additions & 7 deletions config/structure.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package config

import (
"time"
)

// All is a struct to contain all configuration imported or loaded from config file.
type Config struct {
Path string
IPAddress string
General struct {
Hostname string `yaml:"hostname"`
Interval time.Duration `yaml:"interval"`
AWSRegion string `yaml:"aws_region"`
Hostname string `yaml:"hostname"`
Interval int `yaml:"interval"`
AWSRegion string `yaml:"aws_region"`
}
Inputs struct {
AWS struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/aws/aws-sdk-go v1.46.2
github.com/debeando/go-common v0.4.2
github.com/debeando/go-common v0.4.3
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/kardianos/service v1.2.2
github.com/shirou/gopsutil v3.21.11+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/debeando/go-common v0.4.2 h1:sn6Vuuhl+3D3mUwxwLWa9KdQTN93UquJUyNSOmWicvk=
github.com/debeando/go-common v0.4.2/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/debeando/go-common v0.4.3 h1:JUDvmSZONsNS8DvyJ/rrJBbRvTuFBqqjtBVUkEp4uvI=
github.com/debeando/go-common v0.4.3/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
Expand Down

0 comments on commit 657fbc8

Please sign in to comment.