Alerting: Add multiple threshold operators (#99516)

The following operators are being added:
- Equal
- Not Equal
- Greater or Equal
- Less or Equal
- Within Range Inclusive
- Outside Range Inclusive
This commit is contained in:
Paulo Dias
2025-02-21 19:11:16 +02:00
committed by GitHub
parent 6ebde0481e
commit 2d2e595555
18 changed files with 781 additions and 128 deletions
+96 -4
View File
@@ -32,18 +32,30 @@ type ThresholdCommand struct {
type ThresholdType string
const (
ThresholdIsAbove ThresholdType = "gt"
ThresholdIsBelow ThresholdType = "lt"
ThresholdIsWithinRange ThresholdType = "within_range"
ThresholdIsOutsideRange ThresholdType = "outside_range"
ThresholdIsAbove ThresholdType = "gt"
ThresholdIsBelow ThresholdType = "lt"
ThresholdIsEqual ThresholdType = "eq"
ThresholdIsNotEqual ThresholdType = "ne"
ThresholdIsGreaterThanEqual ThresholdType = "gte"
ThresholdIsLessThanEqual ThresholdType = "lte"
ThresholdIsWithinRange ThresholdType = "within_range"
ThresholdIsOutsideRange ThresholdType = "outside_range"
ThresholdIsWithinRangeIncluded ThresholdType = "within_range_included"
ThresholdIsOutsideRangeIncluded ThresholdType = "outside_range_included"
)
var (
supportedThresholdFuncs = []string{
string(ThresholdIsAbove),
string(ThresholdIsBelow),
string(ThresholdIsEqual),
string(ThresholdIsNotEqual),
string(ThresholdIsGreaterThanEqual),
string(ThresholdIsLessThanEqual),
string(ThresholdIsWithinRange),
string(ThresholdIsOutsideRange),
string(ThresholdIsWithinRangeIncluded),
string(ThresholdIsOutsideRangeIncluded),
}
)
@@ -60,6 +72,16 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
}
predicate = withinRangePredicate{left: conditions[0], right: conditions[1]}
case ThresholdIsWithinRangeIncluded:
if len(conditions) < 2 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
}
predicate = withinRangeIncludedPredicate{left: conditions[0], right: conditions[1]}
case ThresholdIsOutsideRangeIncluded:
if len(conditions) < 2 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
}
predicate = outsideRangeIncludedPredicate{left: conditions[0], right: conditions[1]}
case ThresholdIsAbove:
if len(conditions) < 1 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
@@ -70,6 +92,26 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
}
predicate = lessThanPredicate{value: conditions[0]}
case ThresholdIsEqual:
if len(conditions) < 1 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
}
predicate = equalPredicate{value: conditions[0]}
case ThresholdIsNotEqual:
if len(conditions) < 1 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
}
predicate = notEqualPredicate{value: conditions[0]}
case ThresholdIsGreaterThanEqual:
if len(conditions) < 1 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
}
predicate = greaterThanEqualPredicate{value: conditions[0]}
case ThresholdIsLessThanEqual:
if len(conditions) < 1 {
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
}
predicate = lessThanEqualPredicate{value: conditions[0]}
default:
return nil, fmt.Errorf("expected threshold function to be one of [%s], got %s", strings.Join(supportedThresholdFuncs, ", "), thresholdFunc)
}
@@ -279,6 +321,24 @@ func (r outsideRangePredicate) Eval(f float64) bool {
return f < r.left || f > r.right
}
type withinRangeIncludedPredicate struct {
left float64
right float64
}
func (r withinRangeIncludedPredicate) Eval(f float64) bool {
return f >= r.left && f <= r.right
}
type outsideRangeIncludedPredicate struct {
left float64
right float64
}
func (r outsideRangeIncludedPredicate) Eval(f float64) bool {
return f <= r.left || f >= r.right
}
type lessThanPredicate struct {
value float64
}
@@ -294,3 +354,35 @@ type greaterThanPredicate struct {
func (r greaterThanPredicate) Eval(f float64) bool {
return f > r.value
}
type equalPredicate struct {
value float64
}
func (r equalPredicate) Eval(f float64) bool {
return f == r.value
}
type notEqualPredicate struct {
value float64
}
func (r notEqualPredicate) Eval(f float64) bool {
return f != r.value
}
type greaterThanEqualPredicate struct {
value float64
}
func (r greaterThanEqualPredicate) Eval(f float64) bool {
return f >= r.value
}
type lessThanEqualPredicate struct {
value float64
}
func (r lessThanEqualPredicate) Eval(f float64) bool {
return f <= r.value
}