Skip to content

Commit dfa20d6

Browse files
committed
Drop expiration in the alert cache
The cache in `AlertGetter` might not be that useful because it expires mostly before the next call happens and it could lead to stale data. The cache is kept to preserve the presence of an alert to avoid breaking if an attempt to retrieve alerts hiccuped. We rely on the existing `minimumUpdateCheckInterval` to avoid bombarding the OpenShift monitoring. We may come back to figure out a more efficient method to get the alerts instead of polling periodically if performance becomes an issue.
1 parent dd17fbf commit dfa20d6

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

pkg/clusterconditions/promql/alerts.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"sync"
7-
"time"
87

98
"github.com/prometheus/client_golang/api"
109
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -26,23 +25,19 @@ func NewAlertGetter(promQLTarget clusterconditions.PromQLTarget) Getter {
2625
if !ok {
2726
panic("invalid condition type")
2827
}
29-
return &ocAlertGetter{promQL: v, expiration: 1 * time.Minute}
28+
return &ocAlertGetter{promQL: v}
3029
}
3130

3231
type ocAlertGetter struct {
3332
promQL *PromQL
3433

35-
mutex sync.Mutex
36-
cached prometheusv1.AlertsResult
37-
expiration time.Duration
38-
lastRefresh time.Time
34+
mutex sync.Mutex
35+
cached prometheusv1.AlertsResult
3936
}
4037

4138
func (o *ocAlertGetter) Get(ctx context.Context) prometheusv1.AlertsResult {
42-
if time.Now().After(o.lastRefresh.Add(o.expiration)) {
43-
if err := o.refresh(ctx); err != nil {
44-
klog.Errorf("Failed to refresh alerts, using stale cache instead: %v", err)
45-
}
39+
if err := o.refresh(ctx); err != nil {
40+
klog.Errorf("Failed to refresh alerts, using stale cache instead: %v", err)
4641
}
4742
return o.cached
4843
}
@@ -89,7 +84,6 @@ func (o *ocAlertGetter) refresh(ctx context.Context) error {
8984
return fmt.Errorf("failed to get alerts: %w", err)
9085
}
9186
o.cached = r
92-
o.lastRefresh = time.Now()
9387
klog.Infof("refreshed: %d alerts", len(o.cached.Alerts))
9488
return nil
9589
}

0 commit comments

Comments
 (0)