From a7d27c6177b33e211e89c4fcfb926ec8f189cf35 Mon Sep 17 00:00:00 2001 From: "maximilian.schubert@telekom.de" Date: Fri, 20 Oct 2023 16:09:28 +0200 Subject: [PATCH] feat: improve registration of checks by config --- pkg/checks/checks.go | 7 +++++++ pkg/checks/interface.go | 45 ----------------------------------------- pkg/checks/roundtrip.go | 7 +++++++ pkg/sparrow/run.go | 12 +++-------- 4 files changed, 17 insertions(+), 54 deletions(-) delete mode 100644 pkg/checks/interface.go diff --git a/pkg/checks/checks.go b/pkg/checks/checks.go index cdb242f6..0ca0d666 100644 --- a/pkg/checks/checks.go +++ b/pkg/checks/checks.go @@ -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 diff --git a/pkg/checks/interface.go b/pkg/checks/interface.go deleted file mode 100644 index cdb242f6..00000000 --- a/pkg/checks/interface.go +++ /dev/null @@ -1,45 +0,0 @@ -package checks - -import ( - "context" - "errors" - "time" -) - -type Check interface { - // Run is called once per check interval - // this should error if there is a problem running the check - // Returns an error and a result. Returning a non nil error will cause a shutdown of the system - Run(ctx context.Context) (Result, error) - // Startup is called once when the check is registered - // In the Run() method, the check should send results to the cResult channel - // this will cause sparrow to update its data store with the results - Startup(ctx context.Context, cResult chan<- Result) error - // Shutdown is called once when the check is unregistered or sparrow shuts down - Shutdown(ctx context.Context) error - // SetConfig is called once when the check is registered - // This is also called while the check is running, if the remote config is updated - // This should return an error if the config is invalid - SetConfig(ctx context.Context, config any) error - // Name returns the name of the check - Name() string -} - -type CheckConfig struct { - Enabled bool - IntervalSec int - MaxRetryCount int - RetryDelaySec int -} - -var ErrRetry = errors.New("retry") - -type Result struct { - // data contains performance metrics about the check run - Data any - // Timestamp is the UTC time the check was run - Timestamp time.Time - // Err should be nil if the check ran successfully indicating the check is "healthy" - // if the check failed, this should be an error message that will be logged and returned to an API user - Err error -} diff --git a/pkg/checks/roundtrip.go b/pkg/checks/roundtrip.go index ed8c10eb..ae5bed2b 100644 --- a/pkg/checks/roundtrip.go +++ b/pkg/checks/roundtrip.go @@ -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 } diff --git a/pkg/sparrow/run.go b/pkg/sparrow/run.go index e981e114..760ea4ab 100644 --- a/pkg/sparrow/run.go +++ b/pkg/sparrow/run.go @@ -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), } @@ -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)