Skip to content

Commit

Permalink
feat: generic run method on base struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed May 18, 2024
1 parent 94c3919 commit 6d6346c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 77 deletions.
27 changes: 27 additions & 0 deletions pkg/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/caas-team/sparrow/internal/helper"
"github.com/caas-team/sparrow/internal/logger"
"github.com/getkin/kin-openapi/openapi3"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -115,6 +116,32 @@ func (b *Base[T]) SendResult(channel chan ResultDTO, data any) {
}
}

// CheckFunc is a function that performs a check and returns the result
type CheckFunc func(ctx context.Context) any

// StartCheck runs the check indefinitely, sending results to the provided channel at the specified interval
func (b *Base[T]) StartCheck(ctx context.Context, cResult chan ResultDTO, interval time.Duration, check CheckFunc) error {
ctx, cancel := logger.NewContextWithLogger(ctx)
defer cancel()
log := logger.FromContext(ctx).With("check", b.Name())

log.InfoContext(ctx, "Starting check", "interval", interval.String())
for {
select {
case <-ctx.Done():
log.ErrorContext(ctx, "Context canceled", "error", ctx.Err())
return ctx.Err()
case <-b.DoneChan:
log.InfoContext(ctx, "Shutdown signal received")
return nil
case <-time.After(interval):
res := check(ctx)
b.SendResult(cResult, res)
log.DebugContext(ctx, "Check run completed")
}
}
}

// Shutdown shuts down the check
func (b *Base[T]) Shutdown() {
b.DoneChan <- struct{}{}
Expand Down
21 changes: 3 additions & 18 deletions pkg/checks/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,9 @@ type result struct {

// Run starts the dns check
func (d *DNS) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
ctx, cancel := logger.NewContextWithLogger(ctx)
defer cancel()
log := logger.FromContext(ctx)

log.Info("Starting dns check", "interval", d.Config.Interval.String())
for {
select {
case <-ctx.Done():
log.Error("Context canceled", "err", ctx.Err())
return ctx.Err()
case <-d.DoneChan:
return nil
case <-time.After(d.Config.Interval):
res := d.check(ctx)
d.SendResult(cResult, res)
log.Debug("Successfully finished dns check run")
}
}
return d.StartCheck(ctx, cResult, d.Config.Interval, func(ctx context.Context) any {
return d.check(ctx)
})
}

// Schema provides the schema of the data that will be provided
Expand Down
25 changes: 4 additions & 21 deletions pkg/checks/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"
"net/http"
"sync"
"time"

"github.com/caas-team/sparrow/internal/helper"
"github.com/caas-team/sparrow/internal/logger"
Expand Down Expand Up @@ -68,31 +67,15 @@ type metrics struct {

// Run starts the health check
func (h *Health) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
ctx, cancel := logger.NewContextWithLogger(ctx)
defer cancel()
log := logger.FromContext(ctx)

log.Info("Starting healthcheck", "interval", h.Config.Interval.String())
for {
select {
case <-ctx.Done():
log.Error("Context canceled", "err", ctx.Err())
return ctx.Err()
case <-h.DoneChan:
log.Debug("Soft shut down")
return nil
case <-time.After(h.Config.Interval):
res := h.check(ctx)
h.SendResult(cResult, res)
log.Debug("Successfully finished health check run")
}
}
return h.StartCheck(ctx, cResult, h.Config.Interval, func(ctx context.Context) any {
return h.check(ctx)
})
}

// Schema provides the schema of the data that will be provided
// by the health check
func (h *Health) Schema() (*openapi3.SchemaRef, error) {
return checks.OpenapiFromPerfData[map[string]string](map[string]string{})
return checks.OpenapiFromPerfData(map[string]string{})
}

// newMetrics initializes metric collectors of the health check
Expand Down
23 changes: 4 additions & 19 deletions pkg/checks/latency/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,15 @@ type metrics struct {

// Run starts the latency check
func (l *Latency) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
ctx, cancel := logger.NewContextWithLogger(ctx)
defer cancel()
log := logger.FromContext(ctx)

log.Info("Starting latency check", "interval", l.Config.Interval.String())
for {
select {
case <-ctx.Done():
log.Error("Context canceled", "err", ctx.Err())
return ctx.Err()
case <-l.DoneChan:
return nil
case <-time.After(l.Config.Interval):
res := l.check(ctx)
l.SendResult(cResult, res)
log.Debug("Successfully finished latency check run")
}
}
return l.StartCheck(ctx, cResult, l.Config.Interval, func(ctx context.Context) any {
return l.check(ctx)
})
}

// Schema provides the schema of the data that will be provided
// by the latency check
func (l *Latency) Schema() (*openapi3.SchemaRef, error) {
return checks.OpenapiFromPerfData[map[string]result](make(map[string]result))
return checks.OpenapiFromPerfData(make(map[string]result))
}

// newMetrics initializes metric collectors of the latency check
Expand Down
23 changes: 4 additions & 19 deletions pkg/checks/traceroute/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,9 @@ type hop struct {

// Run runs the check in a loop sending results to the provided channel
func (tr *Traceroute) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
ctx, cancel := logger.NewContextWithLogger(ctx)
defer cancel()
log := logger.FromContext(ctx)

log.Info("Starting traceroute check", "interval", tr.Config.Interval.String())
for {
select {
case <-ctx.Done():
log.Error("Context canceled", "error", ctx.Err())
return ctx.Err()
case <-tr.DoneChan:
return nil
case <-time.After(tr.Config.Interval):
res := tr.check(ctx)
tr.SendResult(cResult, res)
log.Debug("Successfully finished traceroute check run")
}
}
return tr.StartCheck(ctx, cResult, tr.Config.Interval, func(ctx context.Context) any {
return tr.check(ctx)
})
}

func (tr *Traceroute) check(ctx context.Context) map[string]result {
Expand Down Expand Up @@ -145,7 +130,7 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {

// Schema returns an openapi3.SchemaRef of the result type returned by the check
func (tr *Traceroute) Schema() (*openapi3.SchemaRef, error) {
return checks.OpenapiFromPerfData[map[string]result](map[string]result{})
return checks.OpenapiFromPerfData(map[string]result{})
}

// GetMetricCollectors allows the check to provide prometheus metric collectors
Expand Down

0 comments on commit 6d6346c

Please sign in to comment.