Skip to content

Commit

Permalink
feat: improve registration of checks by config
Browse files Browse the repository at this point in the history
  • Loading branch information
y-eight committed Oct 20, 2023
1 parent 5d2ef20 commit a7d27c6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 54 deletions.
7 changes: 7 additions & 0 deletions pkg/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
"time"
)

// Available Checks will be registered in this map
// The key is the name of the Check
// The name needs to map the configuration item key
var RegisteredChecks = map[string]func(string) Check{
"rtt": GetRoundtripCheck,
}

type Check interface {
// Run is called once per check interval
// this should error if there is a problem running the check
Expand Down
45 changes: 0 additions & 45 deletions pkg/checks/interface.go

This file was deleted.

7 changes: 7 additions & 0 deletions pkg/checks/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type RoundTrip struct {
config roundTripConfig
}

// Constructor for the RoundtripCheck
func GetRoundtripCheck(name string) Check {
return &RoundTrip{
name: name,
}
}

func (rt *RoundTrip) Run(ctx context.Context) (Result, error) {
return Result{}, nil
}
Expand Down
12 changes: 3 additions & 9 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Sparrow struct {
func New(config *Config) *Sparrow {
// TODO read this from config file
return &Sparrow{

config: config,
c: make(chan checks.Result),
}
Expand All @@ -41,15 +40,10 @@ func (s *Sparrow) Run(ctx context.Context) error {
}
}()

for k, checkConfig := range s.config.Checks {
var check checks.Check
switch k {
case "rtt":
check = &checks.RoundTrip{}
default:
continue
}
for checkName, checkConfig := range s.config.Checks {
check := checks.RegisteredChecks[checkName](checkName)
s.checks = append(s.checks, check)

err := check.SetConfig(ctx, checkConfig)
if err != nil {
return fmt.Errorf("failed to set config for check %s: %w", check.Name(), err)
Expand Down

0 comments on commit a7d27c6

Please sign in to comment.