Skip to content

Commit 6c4925f

Browse files
committed
fix flapping alerts in Alertmanager
- If startsAt and endsAt are equal, Alertmanager will mark the alert as resolved. This is the case when the grace period is set to 0 for an alert condition. - Alertmanager will prune alerts when it hits the time specified in the endsAt field. Graylog is good at sending notifications on time, but a small window is added in case there is a delay will prevent alerts going stale.
1 parent 19c2aa4 commit 6c4925f

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/main/java/de/gdata/mobilelab/alertmanagercallback/AlertManagerPayloadBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,16 @@ private String extractEndsAt() {
118118
return new DateTime().plusMinutes(1).toString();
119119
}
120120

121-
return checkResult.getTriggeredAt().plusMinutes(checkResult.getTriggeredCondition().getGrace()).toString();
121+
int delay = checkResult.getTriggeredCondition().getGrace();
122+
123+
// when grace is 0, the next alert isn't for another minute
124+
if(delay == 0) {
125+
delay += 1;
126+
}
127+
128+
// give a small window to avoid alerts expiring due to the notification
129+
// not being sent exactly on time
130+
return checkResult.getTriggeredAt().plusMinutes(delay).plusSeconds(10).toString();
122131
}
123132

124133
private String extractStartsAt() {

src/test/java/de/gdata/mobilelab/alertmanagercallback/AlertManagerPayloadBuilderTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void buildWithFullInformation() {
157157
assertEquals(triggeredAt.toString(), alertManagerPayload.getStartsAt());
158158

159159
// - endsAt
160-
assertEquals(triggeredAt.plusMinutes(1337).toString(), alertManagerPayload.getEndsAt());
160+
assertEquals(triggeredAt.plusMinutes(1337).plusSeconds(10).toString(), alertManagerPayload.getEndsAt());
161161

162162
// - generatorUrl
163163
assertEquals("aStreamUrl", alertManagerPayload.getGeneratorURL());
@@ -216,6 +216,8 @@ public void buildWithNoInformationForEndsAt() {
216216
AlertCondition alertCondition = mock(AlertCondition.class);
217217
when(alertCondition.getGrace()).thenReturn(0);
218218
when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);
219+
DateTime triggeredAt = new DateTime();
220+
when(checkResult.getTriggeredAt()).thenReturn(triggeredAt);
219221

220222
// and: instance with set mocks as values
221223
AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder.newInstance()
@@ -226,6 +228,7 @@ public void buildWithNoInformationForEndsAt() {
226228
// - endsAt
227229
assertNotNull(alertManagerPayload.getEndsAt());
228230
assertNotEquals("", alertManagerPayload.getEndsAt());
231+
assertEquals(triggeredAt.plusMinutes(1).plusSeconds(10).toString(), alertManagerPayload.getEndsAt());
229232
}
230233

231234
@Test
@@ -602,4 +605,4 @@ public void buildWithCustomTemplateAnnotationsWithoutMessageBacklog() {
602605
assertEquals("<No Backlog>", alertManagerPayload.getAnnotations().get("myMessageBacklog"));
603606
assertEquals(13, alertManagerPayload.getAnnotations().size());
604607
}
605-
}
608+
}

0 commit comments

Comments
 (0)