Skip to content

Commit

Permalink
Refactor list of supported services and filter funcs. (prometheus-com…
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiangreco authored Jan 18, 2023
1 parent 7e59491 commit a5db8d7
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 309 deletions.
7 changes: 3 additions & 4 deletions cmd/yace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/logger"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/services"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/session"
)

Expand Down Expand Up @@ -75,7 +74,7 @@ func main() {
},
Action: func(c *cli.Context) error {
log.Println("Parse config..")
if err := cfg.Load(&configFile, services.CheckServiceName); err != nil {
if err := cfg.Load(&configFile); err != nil {
log.Fatal("Couldn't read ", configFile, ": ", err)
os.Exit(1)
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func startScraper(_ *cli.Context) error {
}

log.Println("Parse config..")
if err := cfg.Load(&configFile, services.CheckServiceName); err != nil {
if err := cfg.Load(&configFile); err != nil {
return fmt.Errorf("Couldn't read %s: %w", configFile, err)
}

Expand Down Expand Up @@ -142,7 +141,7 @@ func startScraper(_ *cli.Context) error {
return
}
log.Println("Parse config..")
if err := cfg.Load(&configFile, services.CheckServiceName); err != nil {
if err := cfg.Load(&configFile); err != nil {
log.Fatal("Couldn't read ", &configFile, ": ", err)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *Role) ValidateRole(roleIdx int, parent string) error {
return nil
}

func (c *ScrapeConf) Load(file *string, validSvc func(string) bool) error {
func (c *ScrapeConf) Load(file *string) error {
yamlFile, err := os.ReadFile(*file)
if err != nil {
return err
Expand Down Expand Up @@ -126,21 +126,21 @@ func (c *ScrapeConf) Load(file *string, validSvc func(string) bool) error {
}
}

err = c.Validate(validSvc)
err = c.Validate()
if err != nil {
return err
}
return nil
}

func (c *ScrapeConf) Validate(validSvc func(string) bool) error {
func (c *ScrapeConf) Validate() error {
if c.Discovery.Jobs == nil && c.Static == nil && c.CustomNamespace == nil {
return fmt.Errorf("At least 1 Discovery job, 1 Static or one CustomNamespace must be defined")
}

if c.Discovery.Jobs != nil {
for idx, job := range c.Discovery.Jobs {
err := job.validateDiscoveryJob(idx, validSvc)
err := job.validateDiscoveryJob(idx)
if err != nil {
return err
}
Expand Down Expand Up @@ -171,9 +171,9 @@ func (c *ScrapeConf) Validate(validSvc func(string) bool) error {
return nil
}

func (j *Job) validateDiscoveryJob(jobIdx int, validSvc func(string) bool) error {
func (j *Job) validateDiscoveryJob(jobIdx int) error {
if j.Type != "" {
if !validSvc(j.Type) {
if SupportedServices.GetService(j.Type) == nil {
return fmt.Errorf("Discovery job [%d]: Service is not in known list!: %s", jobIdx, j.Type)
}
} else {
Expand Down
22 changes: 2 additions & 20 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestConfLoad(t *testing.T) {
for _, tc := range testCases {
config := ScrapeConf{}
configFile := fmt.Sprintf("testdata/%s", tc.configFile)
if err := config.Load(&configFile, testServices); err != nil {
if err := config.Load(&configFile); err != nil {
t.Error(err)
t.FailNow()
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestBadConfigs(t *testing.T) {
for _, tc := range testCases {
config := ScrapeConf{}
configFile := fmt.Sprintf("testdata/%s", tc.configFile)
if err := config.Load(&configFile, testServices); err != nil {
if err := config.Load(&configFile); err != nil {
if !strings.Contains(err.Error(), tc.errorMsg) {
t.Errorf("expecter error for config file %q to contain %q but got: %s", tc.configFile, tc.errorMsg, err)
t.FailNow()
Expand All @@ -71,21 +71,3 @@ func TestBadConfigs(t *testing.T) {
}
}
}

func testServices(s string) bool {
switch s {
case
"alb",
"billing",
"ebs",
"elb",
"es",
"kafka",
"kinesis",
"s3",
"vpn":
return true
default:
return false
}
}
Loading

0 comments on commit a5db8d7

Please sign in to comment.