Skip to content

Commit c3970ab

Browse files
Added first logic for variables in custom annotations and labels #7
1 parent 43bd1df commit c3970ab

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package de.gdata.mobilelab.alertmanagercallback;
22

3+
import com.floreysoft.jmte.Engine;
4+
import org.graylog2.plugin.Message;
5+
import org.graylog2.plugin.MessageSummary;
36
import org.graylog2.plugin.alarms.AlertCondition;
47
import org.graylog2.plugin.configuration.Configuration;
58
import org.graylog2.plugin.streams.Stream;
69
import org.joda.time.DateTime;
710

811
import java.io.IOException;
12+
import java.util.Collections;
913
import java.util.HashMap;
14+
import java.util.List;
1015
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
18+
import static com.google.common.base.MoreObjects.firstNonNull;
1119

1220
class AlertManagerPayloadBuilder {
1321

@@ -17,10 +25,12 @@ class AlertManagerPayloadBuilder {
1725
private AlertCondition.CheckResult checkResult;
1826
private Configuration configuration;
1927
private CustomPropertiesTextFieldParser customPropertiesTextFieldParser;
28+
private Engine templateEngine;
2029

2130
private AlertManagerPayloadBuilder() {
2231
// Private constructor to hide the implicit one
2332
customPropertiesTextFieldParser = new CustomPropertiesTextFieldParser();
33+
templateEngine = new Engine();
2434
}
2535

2636
static AlertManagerPayloadBuilder newInstance() {
@@ -45,7 +55,24 @@ AlertManagerPayloadBuilder withConfiguration(Configuration configuration) {
4555
return this;
4656
}
4757

58+
// parts copied from org.graylog2.alerts.FormattedEmailAlertSender#getModel()
59+
private Map<String, Object> createModel(Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) {
60+
Map<String, Object> model = new HashMap<>();
61+
model.put("stream", stream);
62+
model.put("check_result", checkResult);
63+
model.put("stream_url", extractStreamUrl());
64+
model.put("alertCondition", checkResult.getTriggeredCondition());
65+
66+
final List<Message> messages = firstNonNull(backlog, Collections.emptyList());
67+
model.put("backlog", messages);
68+
model.put("backlog_size", messages.size());
69+
70+
return model;
71+
}
72+
4873
AlertManagerPayload build() {
74+
75+
4976
AlertManagerPayload alertManagerPayload = new AlertManagerPayload();
5077
alertManagerPayload.setAnnotations(extractAnnotations());
5178
alertManagerPayload.setLabels(extractLabels());
@@ -72,9 +99,35 @@ private Map<String, Object> extractLabels() {
7299
// damaged configuration, so we'll not put any additional label into the map
73100
}
74101

102+
transformTemplateValues(labels);
103+
75104
return labels;
76105
}
77106

107+
private void transformTemplateValues(Map<String, Object> customValueMap) {
108+
customValueMap.entrySet().forEach(
109+
entry -> {
110+
try {
111+
if (entry.getValue() != null) {
112+
String valueAsString = (String) entry.getValue();
113+
entry.setValue(templateEngine.transform(
114+
valueAsString,
115+
createModel(
116+
stream,
117+
checkResult,
118+
checkResult.getMatchingMessages().stream()
119+
.map(MessageSummary::getRawMessage)
120+
.collect(Collectors.toList())
121+
)
122+
));
123+
}
124+
} catch (Exception ex) {
125+
// Just catch exceptions for bad formatting or direct values which should not be formatted
126+
}
127+
}
128+
);
129+
}
130+
78131
private Map<String, Object> extractAnnotations() {
79132
Map<String, Object> annotations = new HashMap<>();
80133

@@ -98,6 +151,8 @@ private Map<String, Object> extractAnnotations() {
98151
// damaged configuration, so we'll not put any additional annotation into the map
99152
}
100153

154+
transformTemplateValues(annotations);
155+
101156
return annotations;
102157
}
103158

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

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
import java.util.HashMap;
1111
import java.util.Map;
1212

13-
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.assertFalse;
15-
import static org.junit.Assert.assertNotEquals;
16-
import static org.junit.Assert.assertNotNull;
17-
import static org.junit.Assert.assertNull;
18-
import static org.junit.Assert.assertTrue;
13+
import static org.junit.Assert.*;
1914
import static org.mockito.Mockito.mock;
2015
import static org.mockito.Mockito.when;
2116

@@ -472,4 +467,59 @@ public void buildWithStrangeNotatedCustomAnnotations() {
472467
assertEquals("production=staging", alertManagerPayload.getAnnotations().get("system"));
473468
assertEquals(2, alertManagerPayload.getAnnotations().size());
474469
}
470+
471+
@Test
472+
public void buildWithCustomTemplateAnnotations() {
473+
// given: configuration mock
474+
Configuration configuration = mock(Configuration.class);
475+
when(configuration.getString("alertmanager_custom_annotations"))
476+
.thenReturn(
477+
"mystreamtitle=${stream.title};"
478+
+ "myresultdesc=${check_result.resultDescription};"
479+
+ "mytriggeredat=${check_result.triggeredAt};"
480+
+ "mystreamid=${stream.id};"
481+
+ "mystreamdescription=${stream.description};"
482+
+ "myalertconditiontitle=${alertCondition.title};"
483+
+ "myalertconditiondesc=${alertCondition.description};"
484+
+ "mytriggeredcondition=${check_result.triggeredCondition};"
485+
);
486+
487+
// and: stream mock
488+
Stream stream = mock(Stream.class);
489+
when(stream.getTitle()).thenReturn("StreamTitle");
490+
when(stream.getId()).thenReturn("StreamId");
491+
when(stream.getDescription()).thenReturn("StreamDescription");
492+
493+
// and: checkResult mock
494+
AlertCondition.CheckResult checkResult = mock(AlertCondition.CheckResult.class);
495+
DateTime triggeredAt = new DateTime();
496+
when(checkResult.getTriggeredAt()).thenReturn(triggeredAt);
497+
when(checkResult.getResultDescription()).thenReturn("CheckResultResultDescription");
498+
AlertCondition alertCondition = mock(AlertCondition.class);
499+
when(alertCondition.toString()).thenReturn("TriggeredConditionString");
500+
when(alertCondition.getDescription()).thenReturn("AlertDescription");
501+
when(alertCondition.getTitle()).thenReturn("AlertTitle");
502+
when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);
503+
504+
505+
// and: instance with set mocks as values
506+
AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder.newInstance()
507+
.withConfiguration(configuration)
508+
.withStream(stream)
509+
.withCheckResult(checkResult)
510+
.build();
511+
512+
// expect: correct values set
513+
// - annotations
514+
assertEquals("StreamTitle", alertManagerPayload.getAnnotations().get("mystreamtitle"));
515+
assertEquals("CheckResultResultDescription", alertManagerPayload.getAnnotations().get("myresultdesc"));
516+
assertNotNull(alertManagerPayload.getAnnotations().get("mytriggeredat"));
517+
assertNotEquals("", alertManagerPayload.getAnnotations().get("mytriggeredat"));
518+
assertEquals("StreamId", alertManagerPayload.getAnnotations().get("mystreamid"));
519+
assertEquals("StreamDescription", alertManagerPayload.getAnnotations().get("mystreamdescription"));
520+
assertEquals("AlertTitle", alertManagerPayload.getAnnotations().get("myalertconditiontitle"));
521+
assertEquals("AlertDescription", alertManagerPayload.getAnnotations().get("myalertconditiondesc"));
522+
assertEquals("TriggeredConditionString", alertManagerPayload.getAnnotations().get("mytriggeredcondition"));
523+
assertEquals(12, alertManagerPayload.getAnnotations().size());
524+
}
475525
}

0 commit comments

Comments
 (0)