52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package health
|
|
|
|
import "context"
|
|
|
|
type Controller interface {
|
|
ProbeAll(ctx context.Context) ([]*ControllerComponentStatus, error)
|
|
}
|
|
|
|
type ControllerComponentStatus struct {
|
|
IsHealthy bool `json:"is_healthy"`
|
|
IsReady bool `json:"is_ready"`
|
|
|
|
Message string `json:"message"`
|
|
URL string `json:"url"`
|
|
Additional map[string]string `json:"additional"`
|
|
Error error
|
|
}
|
|
|
|
type controllerImpl struct {
|
|
components []ProbableComponent
|
|
}
|
|
|
|
func (c *controllerImpl) ProbeAll(ctx context.Context) ([]*ControllerComponentStatus, error) {
|
|
result := make([]*ControllerComponentStatus, len(c.components))
|
|
for i, p := range c.components {
|
|
status, err := p.Probe(ctx)
|
|
if err != nil {
|
|
result[i] = &ControllerComponentStatus{
|
|
IsHealthy: false,
|
|
IsReady: false,
|
|
|
|
Message: err.Error(),
|
|
Error: err,
|
|
}
|
|
} else {
|
|
result[i] = &ControllerComponentStatus{
|
|
IsHealthy: status.IsHealthy,
|
|
IsReady: status.IsReady,
|
|
Message: status.Message,
|
|
URL: status.URL,
|
|
Additional: status.Additional,
|
|
Error: status.Error,
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func NewControllerFor(components ...ProbableComponent) Controller {
|
|
return &controllerImpl{components: components}
|
|
}
|