Skip to content

Commit

Permalink
revert: healthz endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Mar 12, 2024
1 parent 673c079 commit acd769a
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 165 deletions.
4 changes: 0 additions & 4 deletions pkg/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ type Check interface {
GetConfig() Runtime
// Name returns the name of the check
Name() string
// IsRunning returns true if the check is currently running
IsRunning() bool
// Schema returns an openapi3.SchemaRef of the result type returned by the check
Schema() (*openapi3.SchemaRef, error)
// GetMetricCollectors allows the check to provide prometheus metric collectors
Expand All @@ -65,8 +63,6 @@ type Check interface {
type CheckBase struct {
// Mutex for thread-safe access to shared resources within the check implementation
Mu sync.Mutex
// Running is a flag indicating if the check is currently running
Running bool
// Signal channel used to notify about shutdown of a check
DoneChan chan struct{}
}
Expand Down
37 changes: 0 additions & 37 deletions pkg/checks/base_moq.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions pkg/checks/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ func (d *DNS) Name() string {
return CheckName
}

func (d *DNS) IsRunning() bool {
return d.Running
}

// NewCheck creates a new instance of the dns check
func NewCheck() checks.Check {
return &DNS{
CheckBase: checks.CheckBase{
Mu: sync.Mutex{},
Running: false,
DoneChan: make(chan struct{}, 1),
},
config: Config{
Expand All @@ -90,7 +85,6 @@ func (d *DNS) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
log := logger.FromContext(ctx)

log.Info("Starting dns check", "interval", d.config.Interval.String())
d.Running = true
for {
select {
case <-ctx.Done():
Expand Down
6 changes: 0 additions & 6 deletions pkg/checks/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func NewCheck() checks.Check {
return &Health{
CheckBase: checks.CheckBase{
Mu: sync.Mutex{},
Running: false,
DoneChan: make(chan struct{}, 1),
},
config: Config{
Expand All @@ -78,7 +77,6 @@ func (h *Health) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
log := logger.FromContext(ctx)

log.Info("Starting healthcheck", "interval", h.config.Interval.String())
h.Running = true
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -137,10 +135,6 @@ func (h *Health) Name() string {
return CheckName
}

func (h *Health) IsRunning() bool {
return h.Running
}

// Schema provides the schema of the data that will be provided
// by the health check
func (h *Health) Schema() (*openapi3.SchemaRef, error) {
Expand Down
6 changes: 0 additions & 6 deletions pkg/checks/latency/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func NewCheck() checks.Check {
return &Latency{
CheckBase: checks.CheckBase{
Mu: sync.Mutex{},
Running: false,
DoneChan: make(chan struct{}, 1),
},
config: Config{
Expand Down Expand Up @@ -84,7 +83,6 @@ func (l *Latency) Run(ctx context.Context, cResult chan checks.ResultDTO) error
log := logger.FromContext(ctx)

log.Info("Starting latency check", "interval", l.config.Interval.String())
l.Running = true
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -141,10 +139,6 @@ func (l *Latency) Name() string {
return CheckName
}

func (l *Latency) IsRunning() bool {
return l.Running
}

// Schema provides the schema of the data that will be provided
// by the latency check
func (l *Latency) Schema() (*openapi3.SchemaRef, error) {
Expand Down
6 changes: 0 additions & 6 deletions pkg/checks/traceroute/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func NewCheck() checks.Check {
return &Traceroute{
CheckBase: checks.CheckBase{
Mu: sync.Mutex{},
Running: false,
DoneChan: make(chan struct{}),
},
config: Config{},
Expand Down Expand Up @@ -72,7 +71,6 @@ func (tr *Traceroute) Run(ctx context.Context, cResult chan checks.ResultDTO) er
log := logger.FromContext(ctx)

log.Info("Starting traceroute check", "interval", tr.config.Interval.String())
tr.Running = true
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -199,7 +197,3 @@ func (tr *Traceroute) GetMetricCollectors() []prometheus.Collector {
func (tr *Traceroute) Name() string {
return CheckName
}

func (tr *Traceroute) IsRunning() bool {
return tr.Running
}
7 changes: 0 additions & 7 deletions pkg/sparrow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ func (cc *ChecksController) UnregisterCheck(ctx context.Context, check checks.Ch
return nil
}

// GetChecks returns a copy of all checks.
func (cc *ChecksController) GetChecks() []checks.Check {
cks := make([]checks.Check, len(cc.checks.Iter()))
_ = copy(cks, cc.checks.Iter())
return cks
}

var oapiBoilerplate = openapi3.T{
// this object should probably be user defined
OpenAPI: "3.0.0",
Expand Down
25 changes: 0 additions & 25 deletions pkg/sparrow/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ func (s *Sparrow) startupAPI(ctx context.Context) error {
promhttp.HandlerOpts{Registry: s.metrics.GetRegistry()},
).ServeHTTP,
},
{
Path: "/healthz", Method: http.MethodGet,
Handler: s.handleHealthz,
},
}

err := s.api.RegisterRoutes(ctx, routes...)
Expand Down Expand Up @@ -140,24 +136,3 @@ func (s *Sparrow) handleCheckMetrics(w http.ResponseWriter, r *http.Request) {
}
w.Header().Add("Content-Type", "application/json")
}

// handleHealthz returns a 200 if all checks are running and healthy
func (s *Sparrow) handleHealthz(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
healthy := true
for _, c := range s.controller.GetChecks() {
if !c.IsRunning() {
healthy = false
break
}
}
if !healthy {
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte(http.StatusText(http.StatusServiceUnavailable)))
if err != nil {
logger.FromContext(ctx).Error("Failed to write response", "error", err)
}
}

api.OkHandler(ctx).ServeHTTP(w, r)
}
68 changes: 0 additions & 68 deletions pkg/sparrow/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,74 +162,6 @@ func TestSparrow_handleCheckMetrics(t *testing.T) {
}
}

func TestSparrow_handleHealthz(t *testing.T) {
tests := []struct {
name string
checks []checks.Check
wantCode int
}{
{
name: "all checks healthy",
checks: []checks.Check{
&checks.CheckMock{
IsRunningFunc: func() bool {
return true
},
},
&checks.CheckMock{
IsRunningFunc: func() bool {
return true
},
},
},
wantCode: http.StatusOK,
},
{
name: "one check unhealthy",
checks: []checks.Check{
&checks.CheckMock{
IsRunningFunc: func() bool {
return false
},
},
&checks.CheckMock{
IsRunningFunc: func() bool {
return true
},
},
},
wantCode: http.StatusServiceUnavailable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Sparrow{
controller: &ChecksController{
checks: runtime.Checks{},
},
}
for _, c := range tt.checks {
s.controller.checks.Add(c)
}

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/healthz", bytes.NewBuffer([]byte{}))

s.handleHealthz(w, r)
resp := w.Result() //nolint:bodyclose // close is defer below
defer func(b io.ReadCloser) {
err := b.Close()
if err != nil {
t.Fatalf("Failed to close response body: %v", err)
}
}(resp.Body)
if tt.wantCode != resp.StatusCode {
t.Errorf("Sparrow.handleHealthz() Status = %v, want %v", resp.StatusCode, tt.wantCode)
}
})
}
}

func chiRequest(r *http.Request, value string) *http.Request {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("checkName", value)
Expand Down

0 comments on commit acd769a

Please sign in to comment.