Add Prewarming Pool Alert

that checks for every environment if the filled share of the prewarmin pool is at least the specified threshold.
This commit is contained in:
Maximilian Paß
2023-10-31 13:06:53 +01:00
committed by Sebastian Serth
parent 1be3ce5ae1
commit c46a09eeae
9 changed files with 101 additions and 22 deletions

View File

@ -1,12 +1,42 @@
package api
import (
"errors"
"fmt"
"github.com/openHPI/poseidon/internal/config"
"github.com/openHPI/poseidon/internal/environment"
"github.com/openHPI/poseidon/pkg/dto"
"net/http"
"strings"
)
var ErrorPrewarmingPoolDepleting = errors.New("the prewarming pool is depleting")
// Health handles the health route.
// It responds that the server is alive.
// If it is not, the response won't reach the client.
func Health(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusNoContent)
func Health(manager environment.Manager) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
if err := checkPrewarmingPool(manager); err != nil {
sendJSON(writer, &dto.InternalServerError{Message: err.Error(), ErrorCode: dto.PrewarmingPoolDepleting},
http.StatusServiceUnavailable, request.Context())
return
}
writer.WriteHeader(http.StatusNoContent)
}
}
func checkPrewarmingPool(manager environment.Manager) error {
var depletingEnvironments []int
for _, data := range manager.Statistics() {
if float64(data.IdleRunners)/float64(data.PrewarmingPoolSize) < config.Config.Server.PrewarmingPoolAlertThreshold {
depletingEnvironments = append(depletingEnvironments, data.ID)
}
}
if len(depletingEnvironments) > 0 {
arrayToString := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(depletingEnvironments)), ","), "[]")
return fmt.Errorf("%w: environments %s", ErrorPrewarmingPoolDepleting, arrayToString)
}
return nil
}