@@ -40,8 +40,9 @@ type Inhibitor struct {
4040 logger * slog.Logger
4141 metrics * InhibitorMetrics
4242
43- mtx sync.RWMutex
44- cancel func ()
43+ mtx sync.RWMutex
44+ loadingFinished sync.WaitGroup
45+ cancel func ()
4546}
4647
4748// NewInhibitor returns a new Inhibitor.
@@ -53,6 +54,7 @@ func NewInhibitor(ap provider.Alerts, rs []config.InhibitRule, mk types.AlertMar
5354 metrics : metrics ,
5455 }
5556
57+ ih .loadingFinished .Add (1 )
5658 ruleNames := make (map [string ]struct {})
5759 for i , cr := range rs {
5860 if _ , ok := ruleNames [cr .Name ]; ok {
@@ -66,14 +68,19 @@ func NewInhibitor(ap provider.Alerts, rs []config.InhibitRule, mk types.AlertMar
6668 ruleNames [cr .Name ] = struct {}{}
6769 }
6870 }
69-
7071 return ih
7172}
7273
7374func (ih * Inhibitor ) run (ctx context.Context ) {
74- it := ih .alerts .Subscribe ("inhibitor" )
75+ initalAlerts , it := ih .alerts .SlurpAndSubscribe ("inhibitor" )
7576 defer it .Close ()
7677
78+ for _ , a := range initalAlerts {
79+ ih .processAlert (a )
80+ }
81+
82+ ih .loadingFinished .Done ()
83+
7784 for {
7885 select {
7986 case <- ctx .Done ():
@@ -83,33 +90,42 @@ func (ih *Inhibitor) run(ctx context.Context) {
8390 ih .logger .Error ("Error iterating alerts" , "err" , err )
8491 continue
8592 }
86- // Update the inhibition rules' cache.
87- cachedSum := 0
88- indexedSum := 0
89- for _ , r := range ih .rules {
90- if r .SourceMatchers .Matches (a .Labels ) {
91- if err := r .scache .Set (a ); err != nil {
92- ih .logger .Error ("error on set alert" , "err" , err )
93- continue
94- }
95- r .updateIndex (a )
96-
97- }
98- cached := r .scache .Len ()
99- indexed := r .sindex .Len ()
100-
101- if r .Name != "" {
102- r .metrics .sourceAlertsCacheItems .With (prometheus.Labels {"rule" : r .Name }).Set (float64 (cached ))
103- r .metrics .sourceAlertsIndexItems .With (prometheus.Labels {"rule" : r .Name }).Set (float64 (indexed ))
104- }
105-
106- cachedSum += cached
107- indexedSum += indexed
93+ ih .processAlert (a )
94+ }
95+ }
96+ }
97+
98+ func (ih * Inhibitor ) processAlert (a * types.Alert ) {
99+ // Update the inhibition rules' cache.
100+ cachedSum := 0
101+ indexedSum := 0
102+ for _ , r := range ih .rules {
103+ if r .SourceMatchers .Matches (a .Labels ) {
104+ if err := r .scache .Set (a ); err != nil {
105+ ih .logger .Error ("error on set alert" , "err" , err )
106+ continue
108107 }
109- ih .metrics .sourceAlertsCacheItems .Set (float64 (cachedSum ))
110- ih .metrics .sourceAlertsIndexItems .Set (float64 (indexedSum ))
108+ r .updateIndex (a )
109+
110+ }
111+ cached := r .scache .Len ()
112+ indexed := r .sindex .Len ()
113+
114+ if r .Name != "" {
115+ r .metrics .sourceAlertsCacheItems .With (prometheus.Labels {"rule" : r .Name }).Set (float64 (cached ))
116+ r .metrics .sourceAlertsIndexItems .With (prometheus.Labels {"rule" : r .Name }).Set (float64 (indexed ))
111117 }
118+
119+ cachedSum += cached
120+ indexedSum += indexed
112121 }
122+ ih .metrics .sourceAlertsCacheItems .Set (float64 (cachedSum ))
123+ ih .metrics .sourceAlertsIndexItems .Set (float64 (indexedSum ))
124+
125+ }
126+
127+ func (ih * Inhibitor ) WaitForLoading () {
128+ ih .loadingFinished .Wait ()
113129}
114130
115131// Run the Inhibitor's background processing.
0 commit comments