-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from caas-team/feat/check-routine
Feat/check routine
- Loading branch information
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/caas-team/sparrow/pkg/sparrow" | ||
) | ||
|
||
func main() { | ||
|
||
config := sparrow.NewConfig() | ||
sparrow := sparrow.New(config) | ||
|
||
log.Println("running sparrow") | ||
if err := sparrow.Run(context.Background()); err != nil { | ||
panic(err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"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 | ||
// 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 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package checks | ||
|
||
import "errors" | ||
|
||
|
||
var ErrInvalidConfig = errors.New("invalid config") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// ensure that RoundTrip implements the Check interface | ||
var _ Check = &RoundTrip{} | ||
|
||
type roundTripConfig struct{} | ||
|
||
// RoundTrip is a check that measures the round trip time of a request | ||
type RoundTrip struct { | ||
name string | ||
c chan<- Result | ||
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 | ||
} | ||
|
||
func (rt *RoundTrip) Startup(ctx context.Context, cResult chan<- Result) error { | ||
// TODO register http handler for this check | ||
http.HandleFunc("/rtt", func(w http.ResponseWriter, r *http.Request) { | ||
// TODO handle | ||
}) | ||
|
||
rt.c = cResult | ||
return nil | ||
} | ||
|
||
// Shutdown is called once when the check is unregistered or sparrow shuts down | ||
|
||
func (rt *RoundTrip) Shutdown(ctx context.Context) error { | ||
http.Handle("/rtt", http.NotFoundHandler()) | ||
|
||
return nil | ||
} | ||
|
||
// Name returns the name of the check | ||
func (rt *RoundTrip) Name() string { | ||
return rt.name | ||
} | ||
|
||
func (rt *RoundTrip) SetConfig(ctx context.Context, config any) error { | ||
checkConfig, ok := config.(roundTripConfig) | ||
if !ok { | ||
return ErrInvalidConfig | ||
} | ||
rt.config = checkConfig | ||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package sparrow | ||
|
||
type Config struct { | ||
Checks map[string]any | ||
} | ||
|
||
func NewConfig() *Config { | ||
// TODO read this from config file | ||
return &Config{ | ||
Checks: map[string]any{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sparrow | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/caas-team/sparrow/pkg/checks" | ||
) | ||
|
||
type Sparrow struct { | ||
checks []checks.Check | ||
config *Config | ||
c chan checks.Result | ||
} | ||
|
||
// New creates a new sparrow from a given configfile | ||
func New(config *Config) *Sparrow { | ||
// TODO read this from config file | ||
return &Sparrow{ | ||
config: config, | ||
c: make(chan checks.Result), | ||
} | ||
} | ||
|
||
// Run starts the sparrow | ||
func (s *Sparrow) Run(ctx context.Context) error { | ||
// TODO Setup before checks run | ||
// setup database | ||
// setup http server | ||
|
||
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) | ||
} | ||
err = check.Startup(ctx, s.c) | ||
if err != nil { | ||
return fmt.Errorf("failed to startup check %s: %w", check.Name(), err) | ||
} | ||
go check.Run(ctx) | ||
} | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case result := <-s.c: | ||
// TODO write result to database | ||
fmt.Println(result) | ||
} | ||
} | ||
} |