@@ -155,22 +155,32 @@ func TestAlertsLimiterWithNoLimits(t *testing.T) {
155
155
}
156
156
157
157
func TestAlertsLimiterWithCountLimit (t * testing.T ) {
158
+ alert2WithMoreAnnotations := alert2
159
+ alert2WithMoreAnnotations .Annotations = model.LabelSet {"job" : "test" , "cluster" : "prod" , "new" : "super-long-annotation" }
160
+ alert2WithMoreAnnotationsSize := alertSize (alert2WithMoreAnnotations )
161
+
158
162
ops := []callbackOp {
159
163
{alert : & types.Alert {Alert : alert1 }, existing : false , expectedCount : 1 , expectedTotalSize : alert1Size },
160
164
{alert : & types.Alert {Alert : alert2 }, existing : false , expectedInsertError : fmt .Errorf (errTooManyAlerts , 1 ), expectedCount : 1 , expectedTotalSize : alert1Size },
161
165
{alert : & types.Alert {Alert : alert1 }, delete : true , expectedCount : 0 , expectedTotalSize : 0 },
162
166
163
167
{alert : & types.Alert {Alert : alert2 }, existing : false , expectedCount : 1 , expectedTotalSize : alert2Size },
168
+ // Update of existing alert works -- doesn't change count.
169
+ {alert : & types.Alert {Alert : alert2WithMoreAnnotations }, existing : true , expectedCount : 1 , expectedTotalSize : alert2WithMoreAnnotationsSize },
164
170
{alert : & types.Alert {Alert : alert2 }, delete : true , expectedCount : 0 , expectedTotalSize : 0 },
165
171
}
166
172
167
173
testLimiter (t , & mockAlertManagerLimits {maxAlertsCount : 1 }, ops )
168
174
}
169
175
170
176
func TestAlertsLimiterWithSizeLimit (t * testing.T ) {
177
+ alert2WithMoreAnnotations := alert2
178
+ alert2WithMoreAnnotations .Annotations = model.LabelSet {"job" : "test" , "cluster" : "prod" , "new" : "super-long-annotation" }
179
+
171
180
ops := []callbackOp {
172
181
{alert : & types.Alert {Alert : alert1 }, existing : false , expectedCount : 1 , expectedTotalSize : alert1Size },
173
182
{alert : & types.Alert {Alert : alert2 }, existing : false , expectedInsertError : fmt .Errorf (errAlertsTooBig , alert2Size ), expectedCount : 1 , expectedTotalSize : alert1Size },
183
+ {alert : & types.Alert {Alert : alert2WithMoreAnnotations }, existing : false , expectedInsertError : fmt .Errorf (errAlertsTooBig , alert2Size ), expectedCount : 1 , expectedTotalSize : alert1Size },
174
184
{alert : & types.Alert {Alert : alert1 }, delete : true , expectedCount : 0 , expectedTotalSize : 0 },
175
185
176
186
{alert : & types.Alert {Alert : alert2 }, existing : false , expectedCount : 1 , expectedTotalSize : alert2Size },
@@ -183,6 +193,25 @@ func TestAlertsLimiterWithSizeLimit(t *testing.T) {
183
193
testLimiter (t , & mockAlertManagerLimits {maxAlertsSizeBytes : alert2Size }, ops )
184
194
}
185
195
196
+ func TestAlertsLimiterWithSizeLimitAndAnnotationUpdate (t * testing.T ) {
197
+ alert2WithMoreAnnotations := alert2
198
+ alert2WithMoreAnnotations .Annotations = model.LabelSet {"job" : "test" , "cluster" : "prod" , "new" : "super-long-annotation" }
199
+ alert2WithMoreAnnotationsSize := alertSize (alert2WithMoreAnnotations )
200
+
201
+ // Updating alert with larger annotation that goes over the size limit fails.
202
+ testLimiter (t , & mockAlertManagerLimits {maxAlertsSizeBytes : alert2Size }, []callbackOp {
203
+ {alert : & types.Alert {Alert : alert2 }, existing : false , expectedCount : 1 , expectedTotalSize : alert2Size },
204
+ {alert : & types.Alert {Alert : alert2WithMoreAnnotations }, existing : true , expectedInsertError : fmt .Errorf (errAlertsTooBig , alert2Size ), expectedCount : 1 , expectedTotalSize : alert2Size },
205
+ })
206
+
207
+ // Updating alert with larger annotations in the limit works fine.
208
+ testLimiter (t , & mockAlertManagerLimits {maxAlertsSizeBytes : alert2WithMoreAnnotationsSize }, []callbackOp {
209
+ {alert : & types.Alert {Alert : alert2 }, existing : false , expectedCount : 1 , expectedTotalSize : alert2Size },
210
+ {alert : & types.Alert {Alert : alert2WithMoreAnnotations }, existing : true , expectedCount : 1 , expectedTotalSize : alert2WithMoreAnnotationsSize },
211
+ {alert : & types.Alert {Alert : alert2 }, existing : true , expectedCount : 1 , expectedTotalSize : alert2Size },
212
+ })
213
+ }
214
+
186
215
// testLimiter sends sequence of alerts to limiter, and checks if limiter updated reacted correctly.
187
216
func testLimiter (t * testing.T , limits Limits , ops []callbackOp ) {
188
217
reg := prometheus .NewPedanticRegistry ()
0 commit comments