Skip to content

Commit

Permalink
[alert_generator] Test a whole bunch of template variables/functions (p…
Browse files Browse the repository at this point in the history
…rometheus#68)

- $labels.something .Labels.something
- $value .Value
- Numbers:
  - humanize
  - humanize1024
  - humanizeDuration
  - humanizePercentage
  - humanizeTimestamp
- Strings:
  - title
  - toUpper
  - toLower
  - match
  - reReplaceAll

Signed-off-by: Ganesh Vernekar <[email protected]>
  • Loading branch information
codesome authored Jan 25, 2022
1 parent 3723f95 commit 27e822f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 98 deletions.
29 changes: 15 additions & 14 deletions alert_generator/testsuite/cases/case_new_alerts_and_order_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (tc *newAlertsAndOrderCheck) RuleGroup() (rulefmt.RuleGroup, error) {
Expr: r2Expr,
Labels: map[string]string{"foo": "baz", "ba_dum": "tss", "rulegroup": tc.groupName},
// The expression also has alertname. So this template variable should result in r1AlertName.
Annotations: map[string]string{"description": "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{$labels.foo}}."},
Annotations: map[string]string{"description": "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{.Labels.foo}}."},
},
},
}, nil
Expand Down Expand Up @@ -297,7 +297,7 @@ func (tc *newAlertsAndOrderCheck) expRuleGroups(ts int64) (expRgs []v1.RuleGroup
Name: tc.r2AlertName,
Query: tc.r2Query,
Labels: labels.FromStrings("foo", "baz", "ba_dum", "tss", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{$labels.foo}}."),
Annotations: labels.FromStrings("description", "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{.Labels.foo}}."),
Alerts: a2,
Health: "ok",
Type: "alerting",
Expand Down Expand Up @@ -490,13 +490,19 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {

resendDelayMs := int64(ResendDelay / time.Millisecond)

orderingID := 0
addAlert := func(ea ExpectedAlert) {
orderingID++
ea.OrderingID = orderingID
exp = append(exp, ea)
}

// r11.
_20th := 20 * int64(tc.rwInterval/time.Millisecond) // Firing.
_73rd := 73 * int64(tc.rwInterval/time.Millisecond) // Resolved.
_73rdPlus15m := _73rd + int64(15*time.Minute/time.Millisecond)
for ts := _20th; ts < _73rd; ts += resendDelayMs {
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -521,8 +527,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {
// based on this first resolved alert.
tolerance = 2 * tc.groupInterval
}
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tolerance,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: true,
Expand All @@ -543,8 +548,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {
_65thPlus15m := _65th + int64(15*time.Minute/time.Millisecond)
//_8th_plus_gi := _8th + int64(tc.groupInterval/time.Millisecond) // Small for firing.
for ts := _44th; ts < _65th; ts += resendDelayMs {
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -560,8 +564,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {
})

// r2.
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -586,8 +589,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {
// based on this first resolved alert.
tolerance = 2 * tc.groupInterval
}
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tolerance,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: true,
Expand All @@ -601,8 +603,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert {
},
})

exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tolerance,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: true,
Expand Down
52 changes: 27 additions & 25 deletions alert_generator/testsuite/cases/case_pending_firing_resolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (tc *pendingAndFiringAndResolved) RuleGroup() (rulefmt.RuleGroup, error) {
Labels: map[string]string{"foo": "bar", "rulegroup": tc.groupName},
Annotations: map[string]string{
"description": "SimpleAlert is firing",
"summary": "The value is {{$value}}",
"summary": "The value is {{$value}} {{.Value}}",
},
},
},
Expand Down Expand Up @@ -162,7 +162,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e
expAlerts = append(expAlerts, []v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11 11"),
State: "pending",
Value: "11",
ActiveAt: &activeAt,
Expand All @@ -178,7 +178,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e
expAlerts = append(expAlerts, []v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
State: "pending",
Value: "15",
ActiveAt: &aa,
Expand All @@ -194,7 +194,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e
expAlerts = append(expAlerts, []v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
State: "firing",
Value: "15",
ActiveAt: &aa,
Expand All @@ -206,7 +206,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e
expAlerts = append(expAlerts, []v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"),
State: "firing",
Value: "19",
ActiveAt: &activeAt,
Expand Down Expand Up @@ -238,7 +238,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule
Query: tc.query,
Duration: float64(time.Duration(tc.forDuration) / time.Second),
Labels: labels.FromStrings("foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is {{$value}}"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is {{$value}} {{.Value}}"),
Alerts: alerts,
Health: "ok",
Type: "alerting",
Expand All @@ -254,7 +254,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule
expRgs = append(expRgs, getRg("pending", []*v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11 11"),
State: "pending",
Value: "11",
ActiveAt: &activeAt,
Expand All @@ -269,7 +269,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule
expRgs = append(expRgs, getRg("pending", []*v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
State: "pending",
Value: "15",
ActiveAt: &aa,
Expand All @@ -284,7 +284,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule
expRgs = append(expRgs, getRg("firing", []*v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
State: "firing",
Value: "15",
ActiveAt: &aa,
Expand All @@ -295,7 +295,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule
expRgs = append(expRgs, getRg("firing", []*v1.Alert{
{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"),
State: "firing",
Value: "19",
ActiveAt: &activeAt,
Expand Down Expand Up @@ -379,10 +379,16 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
endsAtDelta = 4 * tc.groupInterval
}

orderingID := 0
addAlert := func(ea ExpectedAlert) {
orderingID++
ea.OrderingID = orderingID
exp = append(exp, ea)
}

resendDelayMs := int64(ResendDelay / time.Millisecond)
for ts := _32nd; ts < _53rd; ts += resendDelayMs {
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -392,15 +398,14 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
EndsAtDelta: endsAtDelta,
Alert: &notifier.Alert{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
StartsAt: timestamp.Time(tc.zeroTime + _32nd),
},
})
}
// Value change.
for ts := _53rd; ts < _69th; ts += resendDelayMs {
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -410,7 +415,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
EndsAtDelta: endsAtDelta,
Alert: &notifier.Alert{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"),
StartsAt: timestamp.Time(tc.zeroTime + _32nd),
},
})
Expand All @@ -426,8 +431,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
// based on this first resolved alert.
tolerance = 2 * tc.groupInterval
}
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tolerance,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: true,
Expand All @@ -437,16 +441,15 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
EndsAtDelta: endsAtDelta,
Alert: &notifier.Alert{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"),
StartsAt: timestamp.Time(tc.zeroTime + _32nd),
},
})
}

// Firing again.
for ts := _113th; ts < _134th; ts += resendDelayMs {
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tc.groupInterval,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: false,
Expand All @@ -456,7 +459,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
EndsAtDelta: endsAtDelta,
Alert: &notifier.Alert{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
StartsAt: timestamp.Time(tc.zeroTime + _113th),
},
})
Expand All @@ -472,8 +475,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
// based on this first resolved alert.
tolerance = 2 * tc.groupInterval
}
exp = append(exp, ExpectedAlert{
OrderingID: int(ts),
addAlert(ExpectedAlert{
TimeTolerance: tolerance,
Ts: timestamp.Time(tc.zeroTime + ts),
Resolved: true,
Expand All @@ -482,7 +484,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert {
EndsAtDelta: endsAtDelta,
Alert: &notifier.Alert{
Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"),
Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"),
StartsAt: timestamp.Time(tc.zeroTime + _113th),
},
})
Expand Down
Loading

0 comments on commit 27e822f

Please sign in to comment.