Skip to content

Commit

Permalink
add support for context variables in subject & message templates (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
  • Loading branch information
sbcd90 committed Oct 28, 2022
1 parent dc26cac commit e477a09
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.opensearch.common.xcontent.XContentParserUtils;
import org.opensearch.commons.alerting.model.action.Action;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -305,6 +307,26 @@ public String getSeverity() {
}

public List<Action> getActions() {
return actions;
List<Action> transformedActions = new ArrayList<>();

if (actions != null) {
for (Action action: actions) {
String subjectTemplate = action.getSubjectTemplate() != null ? action.getSubjectTemplate().getIdOrCode(): "";
subjectTemplate = subjectTemplate.replace("{{ctx.detector", "{{ctx.monitor");

action.getMessageTemplate();
String messageTemplate = action.getMessageTemplate().getIdOrCode();
messageTemplate = messageTemplate.replace("{{ctx.detector", "{{ctx.monitor");

Action transformedAction = new Action(action.getName(), action.getDestinationId(),
new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, subjectTemplate, Collections.emptyMap()),
new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, messageTemplate, Collections.emptyMap()),
action.getThrottleEnabled(), action.getThrottle(),
action.getId(), action.getActionExecutionPolicy());

transformedActions.add(transformedAction);
}
}
return transformedActions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ public static DetectorRule randomDetectorRule() {

public static Action randomAction(String destinationId) {
String name = OpenSearchRestTestCase.randomUnicodeOfLength(10);
Script template = randomTemplateScript("Hello World", null);
Script template = randomTemplateScript("Detector {{ctx.detector.name}} just entered alert status. Please investigate the issue.\n" +
" - Trigger: {{ctx.trigger.name}}\n" +
" - Severity: {{ctx.trigger.severity}}", null);
Boolean throttleEnabled = false;
Throttle throttle = randomThrottle(null, null);
return new Action(name, destinationId, template, template, throttleEnabled, throttle, OpenSearchRestTestCase.randomAlphaOfLength(10), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -115,9 +114,11 @@ public void testGetAlerts_success() throws IOException {

for (Map.Entry<String, Object> actionResult: actionResults.entrySet()) {
Map<String, String> actionOutput = ((Map<String, Map<String, String>>) actionResult.getValue()).get("output");
String expectedMessage = triggerAction.getSubjectTemplate().getIdOrCode().replace("{{ctx.detector.name}}", detector.getName())
.replace("{{ctx.trigger.name}}", "test-trigger").replace("{{ctx.trigger.severity}}", "1");

Assert.assertEquals("Hello World", actionOutput.get("subject"));
Assert.assertEquals("Hello World", actionOutput.get("message"));
Assert.assertEquals(expectedMessage, actionOutput.get("subject"));
Assert.assertEquals(expectedMessage, actionOutput.get("message"));
}
}
}
Expand Down

0 comments on commit e477a09

Please sign in to comment.