Skip to content

Commit

Permalink
Allow the result map for the series set before hand with a hint.
Browse files Browse the repository at this point in the history
Signed-off-by: gotjosh <josue.abreu@gmail.com>
  • Loading branch information
gotjosh committed Apr 24, 2024
1 parent 6cfc584 commit 2de2fee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions rules/alerting.go
Expand Up @@ -542,6 +542,13 @@ func (r *AlertingRule) ForEachActiveAlert(f func(*Alert)) {
}
}

func (r *AlertingRule) ActiveAlertsCount() int {
r.activeMtx.Lock()
defer r.activeMtx.Unlock()

return len(r.active)
}

func (r *AlertingRule) sendAlerts(ctx context.Context, ts time.Time, resendDelay, interval time.Duration, notifyFunc NotifyFunc) {
alerts := []*Alert{}
r.ForEachActiveAlert(func(alert *Alert) {
Expand Down
19 changes: 19 additions & 0 deletions rules/alerting_test.go
Expand Up @@ -1027,3 +1027,22 @@ func TestAlertingRule_SetNoDependencyRules(t *testing.T) {
rule.SetNoDependencyRules(true)
require.True(t, rule.NoDependencyRules())
}

func TestAlertingRule_ActiveAlertsCount(t *testing.T) {
rule := NewAlertingRule(
"TestRule",
nil,
time.Minute,
0,
labels.FromStrings("severity", "critical"),
labels.EmptyLabels(), labels.EmptyLabels(), "", true, nil,
)

// Set an active alert.
lbls := labels.FromStrings("a1", "1")
h := lbls.Hash()
al := &Alert{State: StateFiring, Labels: lbls, ActiveAt: time.Now()}
rule.active[h] = al

require.Equal(t, 1, t rule.ActiveAlertsCount())

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / Go tests on Windows

missing ',' in argument list

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / Go tests on Windows

missing ',' in argument list

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected rule in argument list; possibly missing comma or ) (typecheck)

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' in argument list (typecheck)

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected rule in argument list; possibly missing comma or ) (typecheck)

Check failure on line 1047 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' in argument list (typecheck)
}

Check failure on line 1048 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' in argument list (typecheck)

Check failure on line 1048 in rules/alerting_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' in argument list (typecheck)
3 changes: 2 additions & 1 deletion rules/group.go
Expand Up @@ -681,7 +681,8 @@ func (g *Group) RestoreForState(ts time.Time) {
continue
}

seriesByLabels := map[string]storage.Series{}
// While not technically the same number of series we expect, it's as good of an approximation as any.
seriesByLabels := make(map[string]storage.Series, alertRule.ActiveAlertsCount())
for sset.Next() {
seriesByLabels[sset.At().Labels().DropMetricName().String()] = sset.At()
}
Expand Down

0 comments on commit 2de2fee

Please sign in to comment.