Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #870 from studanshu/updatetriggername
Browse files Browse the repository at this point in the history
bug fix in getTriggerDetails() function in AuditNotifier class
  • Loading branch information
studanshu committed Aug 8, 2018
2 parents 2b1a6f6 + c26340c commit 02ab0ba
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
Expand Up @@ -160,7 +160,7 @@ protected String getAuditBody(NotificationContext context, NotificationStatus no
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("<b>Triggered on Metric: </b> {0}<br/>", context.getTriggeredMetric().getIdentifier()));
}
sb.append(MessageFormat.format("<b>Trigger details: </b> {0}<br/>", getTriggerDetails(trigger)));
sb.append(MessageFormat.format("<b>Trigger details: </b> {0}<br/>", getTriggerDetails(trigger, context)));
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("<b>Triggering event value: </b> {0}<br/>", context.getTriggerEventValue()));
}
Expand All @@ -180,9 +180,10 @@ protected String getAuditBody(NotificationContext context, NotificationStatus no
*
* @return The trigger detail information.
*/
protected String getTriggerDetails(Trigger trigger) {
protected String getTriggerDetails(Trigger trigger, NotificationContext context) {
if (trigger != null) {
String triggerString = trigger.toString();
triggerString = replaceTemplatesInTriggerName(triggerString, context.getTriggeredMetric().getScope(), context.getTriggeredMetric().getMetric(), context.getTriggeredMetric().getTags());

return triggerString.substring(triggerString.indexOf("{") + 1, triggerString.indexOf("}"));
} else {
Expand Down
Expand Up @@ -112,24 +112,30 @@ private Map<String, String> getLowerCaseTagMap(final Map<String, String> tags)
return lowerCaseTagMap;
}

/*
* Finds all the templates like ${scope}, ${metric} and replaces it with the required fields.
* If no matches are found, nothing is done. Should be a protected function, making public for unit testing.
* */
public String getDisplayTriggerName(NotificationContext context) {
String newTriggerName = context.getTrigger().getName();
Metric triggeredMetric = context.getTriggeredMetric();
newTriggerName = newTriggerName.replaceAll("(?i)\\$\\{scope\\}", triggeredMetric.getScope());
newTriggerName = newTriggerName.replaceAll("(?i)\\$\\{metric\\}", triggeredMetric.getMetric());
Map<String, String> tags = triggeredMetric.getTags();
public String replaceTemplatesInTriggerName(String triggerName, String scope, String metric, Map<String, String> tags) {
triggerName = triggerName.replaceAll("(?i)\\$\\{scope\\}", scope);
triggerName = triggerName.replaceAll("(?i)\\$\\{metric\\}", metric);
Map<String, String> lowerCaseTagMap = getLowerCaseTagMap(tags);
Matcher m = Pattern.compile("(?i)\\$\\{.*?\\}").matcher(newTriggerName);
Matcher m = Pattern.compile("(?i)\\$\\{.*?\\}").matcher(triggerName);
while (m.find()) {
String currentRegex = m.group(), currentTagKey = currentRegex.substring(2, currentRegex.length()-1).toLowerCase();
if (lowerCaseTagMap.containsKey(currentTagKey))
newTriggerName = newTriggerName.replace(currentRegex, lowerCaseTagMap.get(currentTagKey));
triggerName = triggerName.replace(currentRegex, lowerCaseTagMap.get(currentTagKey));
}
return newTriggerName;
return triggerName;

}

/*
* Finds all the templates like ${scope}, ${metric} and replaces it with the required fields.
* If no matches are found, nothing is done. Should be a protected function, making public for unit testing.
* */
protected String getDisplayTriggerName(NotificationContext context) {
String triggerName = context.getTrigger().getName();
Metric triggeredMetric = context.getTriggeredMetric();
String scope = triggeredMetric.getScope(), metric = triggeredMetric.getMetric();
Map<String, String> tags = triggeredMetric.getTags();
return replaceTemplatesInTriggerName(triggerName, scope, metric, tags);
}

private void _createAnnotation(NotificationContext notificationContext, Map<String, String> additionalFields) {
Expand Down
Expand Up @@ -176,7 +176,7 @@ protected String getEmailBody(NotificationContext context, NotificationStatus no
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("<b>Triggered on Metric: </b> {0}<br/>", context.getTriggeredMetric().getIdentifier()));
}
sb.append(MessageFormat.format("<b>Trigger details: </b> {0}<br/>", getTriggerDetails(trigger)));
sb.append(MessageFormat.format("<b>Trigger details: </b> {0}<br/>", getTriggerDetails(trigger, context)));
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("<b>Triggering event value: </b> {0}<br/>", context.getTriggerEventValue()));
}
Expand Down
Expand Up @@ -279,7 +279,7 @@ protected String getGOCMessageBody(Notification notification, Trigger trigger, N
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("Triggered on Metric: {0}\n", context.getTriggeredMetric().getIdentifier()));
}
sb.append(MessageFormat.format("Trigger details: {0}\n", getTriggerDetails(trigger)));
sb.append(MessageFormat.format("Trigger details: {0}\n", getTriggerDetails(trigger, context)));
if(!trigger.getType().equals(TriggerType.NO_DATA)){
sb.append(MessageFormat.format("Triggering event value: {0}\n", context.getTriggerEventValue()));
}
Expand Down
Expand Up @@ -156,7 +156,7 @@ private String generateGusFeed(Notification notification, Trigger trigger, Notif
String triggerName = getDisplayTriggerName(context);
String notificationCooldownExpiraton = DATE_FORMATTER.get().format(new Date(context.getCoolDownExpiration()));
String metricExpression = context.getAlert().getExpression();
String triggerDetails = getTriggerDetails(trigger);
String triggerDetails = getTriggerDetails(trigger, context);
double triggerEventValue = context.getTriggerEventValue();
if(context.getNotification().getCustomText() != null && context.getNotification().getCustomText().length()>0){
sb.append(context.getNotification().getCustomText()).append("\n>");
Expand Down
Expand Up @@ -103,7 +103,7 @@ public void testUpdatingTriggerName() {
Notifier notifier = system.getServiceFactory().getAlertService().getNotifier(SupportedNotifier.GOC);
notifier.sendNotification(context);
assertEquals("${sCopE}-trigger_name-${MEtriC}-trigger_metric-${tag1}-trigger_tag1-${tag2}-trigger_tag2-${tag3}-${tAg2}", context.getTrigger().getName());
assertEquals("scope-trigger_name-metric-trigger_metric-val1-trigger_tag1-val2-trigger_tag2-${tag3}-val2", system.getNotifierFactory().getGOCNotifier().getDisplayTriggerName(context));
assertEquals("scope-trigger_name-metric-trigger_metric-val1-trigger_tag1-val2-trigger_tag2-${tag3}-val2", system.getNotifierFactory().getGOCNotifier().replaceTemplatesInTriggerName(context.getTrigger().getName(), "scope", "metric", tags));


}
Expand Down

0 comments on commit 02ab0ba

Please sign in to comment.