Skip to content

Commit 98ea436

Browse files
Refactored template resolving inside of custom annotations and labels #7
1 parent c3970ab commit 98ea436

File tree

3 files changed

+114
-57
lines changed

3 files changed

+114
-57
lines changed

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

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
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;
63
import org.graylog2.plugin.alarms.AlertCondition;
74
import org.graylog2.plugin.configuration.Configuration;
85
import org.graylog2.plugin.streams.Stream;
96
import org.joda.time.DateTime;
107

118
import java.io.IOException;
12-
import java.util.Collections;
139
import java.util.HashMap;
14-
import java.util.List;
1510
import java.util.Map;
16-
import java.util.stream.Collectors;
17-
18-
import static com.google.common.base.MoreObjects.firstNonNull;
1911

2012
class AlertManagerPayloadBuilder {
2113

@@ -25,12 +17,10 @@ class AlertManagerPayloadBuilder {
2517
private AlertCondition.CheckResult checkResult;
2618
private Configuration configuration;
2719
private CustomPropertiesTextFieldParser customPropertiesTextFieldParser;
28-
private Engine templateEngine;
2920

3021
private AlertManagerPayloadBuilder() {
3122
// Private constructor to hide the implicit one
3223
customPropertiesTextFieldParser = new CustomPropertiesTextFieldParser();
33-
templateEngine = new Engine();
3424
}
3525

3626
static AlertManagerPayloadBuilder newInstance() {
@@ -39,43 +29,38 @@ static AlertManagerPayloadBuilder newInstance() {
3929

4030
AlertManagerPayloadBuilder withStream(Stream stream) {
4131
this.stream = stream;
42-
4332
return this;
4433
}
4534

4635
AlertManagerPayloadBuilder withCheckResult(AlertCondition.CheckResult checkResult) {
4736
this.checkResult = checkResult;
48-
4937
return this;
5038
}
5139

5240
AlertManagerPayloadBuilder withConfiguration(Configuration configuration) {
5341
this.configuration = configuration;
54-
5542
return this;
5643
}
5744

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());
45+
AlertManagerPayload build() {
46+
AlertManagerPayload alertManagerPayload = new AlertManagerPayload();
6547

66-
final List<Message> messages = firstNonNull(backlog, Collections.emptyList());
67-
model.put("backlog", messages);
68-
model.put("backlog_size", messages.size());
48+
Map<String, Object> annotations = extractAnnotations();
49+
Map<String, Object> labels = extractLabels();
6950

70-
return model;
71-
}
51+
// Replace template values such as '${stream.description}'
52+
CustomPropertiesJMTEResolver jmteResolver = CustomPropertiesJMTEResolver.Builder.newBuilder()
53+
.withCheckResult(checkResult)
54+
.withStream(stream)
55+
.withUrl(extractStreamUrl())
56+
.build();
7257

73-
AlertManagerPayload build() {
58+
annotations = jmteResolver.transformTemplateValues(annotations);
59+
labels = jmteResolver.transformTemplateValues(labels);
7460

61+
alertManagerPayload.setAnnotations(annotations);
62+
alertManagerPayload.setLabels(labels);
7563

76-
AlertManagerPayload alertManagerPayload = new AlertManagerPayload();
77-
alertManagerPayload.setAnnotations(extractAnnotations());
78-
alertManagerPayload.setLabels(extractLabels());
7964
alertManagerPayload.setGeneratorURL(extractStreamUrl());
8065
alertManagerPayload.setStartsAt(extractStartsAt());
8166
alertManagerPayload.setEndsAt(extractEndsAt());
@@ -99,35 +84,9 @@ private Map<String, Object> extractLabels() {
9984
// damaged configuration, so we'll not put any additional label into the map
10085
}
10186

102-
transformTemplateValues(labels);
103-
10487
return labels;
10588
}
10689

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-
13190
private Map<String, Object> extractAnnotations() {
13291
Map<String, Object> annotations = new HashMap<>();
13392

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

154-
transformTemplateValues(annotations);
155-
156113
return annotations;
157114
}
158115

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package de.gdata.mobilelab.alertmanagercallback;
2+
3+
import com.floreysoft.jmte.Engine;
4+
import org.graylog2.plugin.Message;
5+
import org.graylog2.plugin.MessageSummary;
6+
import org.graylog2.plugin.alarms.AlertCondition;
7+
import org.graylog2.plugin.streams.Stream;
8+
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
class CustomPropertiesJMTEResolver {
16+
17+
private Engine templateEngine;
18+
private Map<String, Object> templateModel;
19+
20+
private CustomPropertiesJMTEResolver(Map<String, Object> templateModel) {
21+
templateEngine = new Engine();
22+
this.templateModel = templateModel;
23+
}
24+
25+
Map<String, Object> transformTemplateValues(Map<String, Object> customValueMap) {
26+
final Map<String, Object> transformedCustomValueMap = new HashMap<>();
27+
customValueMap.forEach((key, value) -> {
28+
if (value instanceof String) {
29+
transformedCustomValueMap.put(key, templateEngine.transform((String) value, templateModel));
30+
} else {
31+
transformedCustomValueMap.put(key, value);
32+
}
33+
});
34+
return transformedCustomValueMap;
35+
}
36+
37+
static final class Builder {
38+
39+
private String url;
40+
private Stream stream;
41+
private AlertCondition.CheckResult checkResult;
42+
43+
private Builder() {
44+
// private constructor to hide the implicit one
45+
}
46+
47+
static Builder newBuilder() {
48+
return new Builder();
49+
}
50+
51+
Builder withUrl(String url) {
52+
this.url = url;
53+
return this;
54+
}
55+
56+
Builder withStream(Stream stream) {
57+
this.stream = stream;
58+
return this;
59+
}
60+
61+
Builder withCheckResult(AlertCondition.CheckResult checkResult) {
62+
this.checkResult = checkResult;
63+
return this;
64+
}
65+
66+
CustomPropertiesJMTEResolver build() {
67+
return new CustomPropertiesJMTEResolver(createModel());
68+
}
69+
70+
// parts copied from org.graylog2.alerts.FormattedEmailAlertSender#getModel()
71+
private Map<String, Object> createModel() {
72+
73+
final Map<String, Object> model = new HashMap<>();
74+
model.put("stream", stream);
75+
model.put("stream_url", url);
76+
77+
if (checkResult != null) {
78+
model.put("check_result", checkResult);
79+
model.put("alertCondition", checkResult.getTriggeredCondition());
80+
81+
final List<Message> matchingMessages = checkResult.getMatchingMessages() != null ?
82+
checkResult.getMatchingMessages()
83+
.stream()
84+
.map(MessageSummary::getRawMessage)
85+
.collect(Collectors.toList()) :
86+
Collections.emptyList();
87+
model.put("backlog", matchingMessages);
88+
model.put("backlog_size", matchingMessages.size());
89+
}
90+
91+
return model;
92+
}
93+
}
94+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package de.gdata.mobilelab.alertmanagercallback;
2+
3+
public class CustomPropertiesJMTEResolverTest {
4+
5+
// TODO: Add tests
6+
}

0 commit comments

Comments
 (0)