Skip to content

Commit

Permalink
Show state information for targets when requesting a list of register…
Browse files Browse the repository at this point in the history
…ed targets
  • Loading branch information
rb3ckers committed Dec 22, 2019
1 parent 38327ba commit 6cfaf5b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions datatypes/MirrorTargets.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type MirrorSettings struct {
RetryAfter time.Duration
}

type Target struct {
Name string
FailingSince time.Time
State string
}

type targetState struct {
sync.Mutex
firstFailure time.Time
Expand Down Expand Up @@ -66,6 +72,31 @@ func (mt *MirrorTargets) ForEach(f func(string, *gobreaker.CircuitBreaker)) {
}
}

func (mt *MirrorTargets) ListTargets() []Target {
targets := make([]Target, len(mt.targets)-1)

for url, target := range mt.targets {
var state string
switch target.circuitBreaker.State() {
case gobreaker.StateOpen:
state = "failing"
case gobreaker.StateHalfOpen:
state = "retrying"
case gobreaker.StateClosed:
state = "alive"
default:
state = "unknown"
}

targets = append(targets, Target{
Name: url,
FailingSince: target.firstFailure,
State: state,
})
}
return targets
}

func (ts *targetState) onBreakerChange(name string, from gobreaker.State, to gobreaker.State) {
if from == gobreaker.StateClosed && to == gobreaker.StateOpen {
ts.Lock()
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ func mirrorTo(targetURL string, req *http.Request, body []byte, breaker *gobreak

func mirrorsHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodGet {
targets.ForEach(func(target string, breaker *gobreaker.CircuitBreaker) {
fmt.Fprintln(res, target)
})
for _, target := range targets.ListTargets() {
if target.State == "alive" {
fmt.Fprintf(res, "%s: %s\n", target.Name, target.State)
} else {
fmt.Fprintf(res, "%s: %s (since: %s)\n", target.Name, target.State, target.FailingSince.UTC().Format(time.RFC3339))
}
}
return
}

Expand Down

0 comments on commit 6cfaf5b

Please sign in to comment.