Skip to content

Commit

Permalink
feat(health): implement health endpoint (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
massix committed Jul 5, 2024
1 parent 25905aa commit 8dbad2a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
48 changes: 48 additions & 0 deletions internal/endpoints/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package endpoints

import (
"net/http"

"github.com/massix/chaos-monkey/internal/watcher"
)

type HealthEndpoint struct {
mainWatcher *watcher.NamespaceWatcher
}

func NewHealthEndpoint(w *watcher.NamespaceWatcher) *HealthEndpoint {
return &HealthEndpoint{
mainWatcher: w,
}
}

func (e *HealthEndpoint) CountWatchers() int {
var allWatchers func(r watcher.Watcher, acc *[]watcher.Watcher) *[]watcher.Watcher
allWatchers = func(w watcher.Watcher, acc *[]watcher.Watcher) *[]watcher.Watcher {
*acc = append(*acc, w)

if w, ok := w.(*watcher.NamespaceWatcher); ok {
for _, child := range w.CrdWatchers {
allWatchers(child, acc)
}
}

if w, ok := w.(*watcher.CrdWatcher); ok {
for _, child := range w.DeploymentWatchers {
allWatchers(child.Watcher, acc)
}
}

return acc
}

return len(*allWatchers(e.mainWatcher, &[]watcher.Watcher{}))
}

// ServeHTTP implements http.Handler.
func (e *HealthEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header()["x-server-type"] = []string{"golang"}
w.WriteHeader(http.StatusOK)
}

var _ = (http.Handler)((*HealthEndpoint)(nil))
58 changes: 58 additions & 0 deletions internal/endpoints/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package endpoints_test

import (
"fmt"
"math/rand"
"testing"

"github.com/massix/chaos-monkey/internal/endpoints"
"github.com/massix/chaos-monkey/internal/watcher"
)

func setup(crdWatchers, deployWatchers int) (*watcher.NamespaceWatcher, int) {
mainWatcher := &watcher.NamespaceWatcher{
CrdWatchers: map[string]watcher.Watcher{},
}
created := 1

for i := 0; i < crdWatchers; i++ {
crdWatcher := &watcher.CrdWatcher{DeploymentWatchers: map[string]*watcher.WatcherConfiguration{}}
mainWatcher.CrdWatchers[fmt.Sprintf("%d", i)] = crdWatcher
created++

for j := 0; j < deployWatchers; j++ {
var target watcher.ConfigurableWatcher
if rand.Intn(2) == 0 {
target = &watcher.PodWatcher{}
} else {
target = &watcher.DeploymentWatcher{}
}
crdWatcher.DeploymentWatchers[fmt.Sprintf("%d", j)] = &watcher.WatcherConfiguration{
Watcher: target,
}
created++
}
}

return mainWatcher, created
}

func TestHealthEndpoint_CountWatchers(t *testing.T) {
mw, created := setup(10, 15)
ep := endpoints.NewHealthEndpoint(mw)
if cnt := ep.CountWatchers(); cnt != (10*15)+10+1 {
t.Fatalf("wrong count: %d (expecting: %d)", cnt, created)
}
}

func BenchmarkHealthEndpoint_CountWatchers(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
mw, _ := setup(rand.Intn(9000)+1, rand.Intn(500)+1)
ep := endpoints.NewHealthEndpoint(mw)
b.StartTimer()
ep.CountWatchers()
b.StopTimer()
}
})
}

0 comments on commit 8dbad2a

Please sign in to comment.