11package de .gdata .mobilelab .alertmanagercallback ;
22
3+ import com .floreysoft .jmte .Engine ;
4+ import org .graylog2 .plugin .Message ;
5+ import org .graylog2 .plugin .MessageSummary ;
36import org .graylog2 .plugin .alarms .AlertCondition ;
47import org .graylog2 .plugin .configuration .Configuration ;
58import org .graylog2 .plugin .streams .Stream ;
69import org .joda .time .DateTime ;
710
811import java .io .IOException ;
12+ import java .util .Collections ;
913import java .util .HashMap ;
14+ import java .util .List ;
1015import java .util .Map ;
16+ import java .util .stream .Collectors ;
17+
18+ import static com .google .common .base .MoreObjects .firstNonNull ;
1119
1220class 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
0 commit comments