feat: add last option to alert condition match type (#5929)

This commit is contained in:
Srikanth Chekuri 2024-09-19 23:21:31 +05:30 committed by GitHub
parent e203276678
commit 4edc6dbeae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 132 additions and 0 deletions

View File

@ -53,6 +53,7 @@
"option_atleastonce": "at least once",
"option_onaverage": "on average",
"option_intotal": "in total",
"option_last": "last",
"option_above": "above",
"option_below": "below",
"option_equal": "is equal to",

View File

@ -40,6 +40,7 @@
"option_atleastonce": "at least once",
"option_onaverage": "on average",
"option_intotal": "in total",
"option_last": "last",
"option_above": "above",
"option_below": "below",
"option_equal": "is equal to",

View File

@ -53,6 +53,7 @@
"option_atleastonce": "at least once",
"option_onaverage": "on average",
"option_intotal": "in total",
"option_last": "last",
"option_above": "above",
"option_below": "below",
"option_equal": "is equal to",

View File

@ -40,6 +40,7 @@
"option_atleastonce": "at least once",
"option_onaverage": "on average",
"option_intotal": "in total",
"option_last": "last",
"option_above": "above",
"option_below": "below",
"option_equal": "is equal to",

View File

@ -103,6 +103,7 @@ function RuleOptions({
<Select.Option value="2">{t('option_allthetimes')}</Select.Option>
<Select.Option value="3">{t('option_onaverage')}</Select.Option>
<Select.Option value="4">{t('option_intotal')}</Select.Option>
<Select.Option value="5">{t('option_last')}</Select.Option>
</InlineSelect>
);

View File

@ -112,6 +112,7 @@ const (
AllTheTimes MatchType = "2"
OnAverage MatchType = "3"
InTotal MatchType = "4"
Last MatchType = "5"
)
type RuleCondition struct {

View File

@ -483,6 +483,27 @@ func (r *BaseRule) shouldAlert(series v3.Series) (Sample, bool) {
shouldAlert = true
}
}
case Last:
// If the last sample matches the condition, the rule is firing.
shouldAlert = false
alertSmpl = Sample{Point: Point{V: series.Points[len(series.Points)-1].Value}, Metric: lblsNormalized, MetricOrig: lbls}
if r.compareOp() == ValueIsAbove {
if series.Points[len(series.Points)-1].Value > r.targetVal() {
shouldAlert = true
}
} else if r.compareOp() == ValueIsBelow {
if series.Points[len(series.Points)-1].Value < r.targetVal() {
shouldAlert = true
}
} else if r.compareOp() == ValueIsEq {
if series.Points[len(series.Points)-1].Value == r.targetVal() {
shouldAlert = true
}
} else if r.compareOp() == ValueIsNotEq {
if series.Points[len(series.Points)-1].Value != r.targetVal() {
shouldAlert = true
}
}
}
return alertSmpl, shouldAlert
}

View File

@ -677,6 +677,111 @@ func TestThresholdRuleShouldAlert(t *testing.T) {
matchType: "4", // InTotal
target: 20.0,
},
// Test cases for Last
// greater than last
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: true,
compareOp: "1", // Greater Than
matchType: "5", // Last
target: 5.0,
expectedAlertSample: v3.Point{Value: 10.0},
},
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: false,
compareOp: "1", // Greater Than
matchType: "5", // Last
target: 20.0,
},
// less than last
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: true,
compareOp: "2", // Less Than
matchType: "5", // Last
target: 15.0,
expectedAlertSample: v3.Point{Value: 10.0},
},
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: false,
compareOp: "2", // Less Than
matchType: "5", // Last
target: 5.0,
},
// equals last
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: true,
compareOp: "3", // Equals
matchType: "5", // Last
target: 10.0,
expectedAlertSample: v3.Point{Value: 10.0},
},
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: false,
compareOp: "3", // Equals
matchType: "5", // Last
target: 5.0,
},
// not equals last
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: true,
compareOp: "4", // Not Equals
matchType: "5", // Last
target: 5.0,
expectedAlertSample: v3.Point{Value: 10.0},
},
{
values: v3.Series{
Points: []v3.Point{
{Value: 10.0},
{Value: 10.0},
},
},
expectAlert: false,
compareOp: "4", // Not Equals
matchType: "5", // Last
target: 10.0,
},
}
fm := featureManager.StartManager()