You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eagle.apache.org by ra...@apache.org on 2016/09/27 15:58:36 UTC

incubator-eagle git commit: EAGLE-567: Enhance Slack publish format

Repository: incubator-eagle
Updated Branches:
  refs/heads/master 16c17067a -> c897f74b5


EAGLE-567: Enhance Slack publish format

Author: Zeng, Bryant
Reviwer: ralphsu

This closes #456


Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/c897f74b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/c897f74b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/c897f74b

Branch: refs/heads/master
Commit: c897f74b544e037cfea72968aacbeca97e07fd37
Parents: 16c1706
Author: mizeng <mi...@ebaysf.com>
Authored: Tue Sep 27 15:15:16 2016 +0800
Committer: Ralph, Su <su...@gmail.com>
Committed: Tue Sep 27 08:54:49 2016 -0700

----------------------------------------------------------------------
 .../publisher/impl/AlertSlackPublisher.java     | 65 +++++++++++++-------
 1 file changed, 43 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c897f74b/eagle-core/eagle-alert-parent/eagle-alert/alert-engine/src/main/java/org/apache/eagle/alert/engine/publisher/impl/AlertSlackPublisher.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-engine/src/main/java/org/apache/eagle/alert/engine/publisher/impl/AlertSlackPublisher.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-engine/src/main/java/org/apache/eagle/alert/engine/publisher/impl/AlertSlackPublisher.java
index 3e3bd67..a52d558 100644
--- a/eagle-core/eagle-alert-parent/eagle-alert/alert-engine/src/main/java/org/apache/eagle/alert/engine/publisher/impl/AlertSlackPublisher.java
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-engine/src/main/java/org/apache/eagle/alert/engine/publisher/impl/AlertSlackPublisher.java
@@ -19,6 +19,7 @@
 package org.apache.eagle.alert.engine.publisher.impl;
 
 import com.typesafe.config.Config;
+import com.ullink.slack.simpleslackapi.SlackAttachment;
 import com.ullink.slack.simpleslackapi.SlackChannel;
 import com.ullink.slack.simpleslackapi.SlackSession;
 import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory;
@@ -85,6 +86,8 @@ public class AlertSlackPublisher extends AbstractPublishPlugin {
             String device = "";
             String deviceType = "";
             String colo = "";
+            String component = "";
+            String color = "";
             // only user defined severity level alert will send to Slack;
             boolean publishToSlack = false;
 
@@ -107,39 +110,57 @@ public class AlertSlackPublisher extends AbstractPublishPlugin {
                 if (colName.equalsIgnoreCase("docId")) {
                     docId = outputEvent.getData()[i].toString();
                 }
-                if (colName.equalsIgnoreCase("df_device")) {
+                if (colName.equalsIgnoreCase("df_device") || colName.equalsIgnoreCase("entity") || colName.equalsIgnoreCase("hostname")) {
                     device = outputEvent.getData()[i].toString();
                 }
-                if (colName.equalsIgnoreCase("df_type")) {
+                if (colName.equalsIgnoreCase("df_type") || colName.equalsIgnoreCase("entityType")) {
                     deviceType = outputEvent.getData()[i].toString();
                 }
                 if (colName.equalsIgnoreCase("dc")) {
                     colo = outputEvent.getData()[i].toString();
                 }
-            }
-
-            StringBuilder messageToSlack = new StringBuilder();
-            messageToSlack.append("Message: " + message + "\n");
-            messageToSlack.append("Severity: " + severity + "\n");
-            messageToSlack.append("Device: " + device + "\n");
-            messageToSlack.append("Device Type: " + deviceType + "\n");
-            messageToSlack.append("Colo: " + colo + "\n");
-            messageToSlack.append("Alert Policy: " + event.getPolicyId() + "\n");
-            messageToSlack.append("Doc Id: " + docId + "\n");
-
-            if (StringUtils.isNotEmpty(urlTemplate)) {
-                try {
-                    messageToSlack.append("Link to Alert Console: " + String.format(urlTemplate, docId) + "\n");
-                } catch (Exception e) {
-                    LOG.warn("There's an error when processing Alert Console Link!");
+                if (colName.equalsIgnoreCase("component")) {
+                    component = outputEvent.getData()[i].toString();
                 }
-
             }
 
             if (publishToSlack) {
                 try {
+                    // get hex color code from severity
+                    switch (severity) {
+                        case "CRITICAL":
+                            color = "#dd3333"; //red
+                            break;
+                        case "WARNING":
+                            color = "#ffc04c"; //yellow
+                            break;
+                        default:
+                            color = "#439FE0"; //blue
+                            break;
+                    }
+
+                    String messageToSlack = String.format("*New Alert from Device [%s] and Component [%s]!*", device, component);
+                    SlackAttachment attachment = new SlackAttachment();
+                    attachment.setColor(color);
+                    attachment.setFooter("Alert Engine");
+                    attachment.addField("Severity", severity, false);
+                    attachment.addField("Message", message, false);
+                    attachment.addField("Device", device, false);
+                    attachment.addField("Device Type", deviceType, false);
+                    attachment.addField("Colo", colo, false);
+                    attachment.addField("Alert Policy", event.getPolicyId(), false);
+                    attachment.addField("Doc Id", docId, false);
+
+                    if (StringUtils.isNotEmpty(urlTemplate)) {
+                        try {
+                            attachment.addField("Link to Alert Console", String.format(urlTemplate, docId), false);
+                        } catch (Exception e) {
+                            LOG.warn("There's an error when processing Alert Console Link!");
+                        }
+                    }
+
                     for (String slackChannel: slackChannels.split(",")) {
-                        sendMessageToAChannel(session, slackChannel, messageToSlack.toString());
+                        sendMessageToAChannel(session, slackChannel, messageToSlack, attachment);
                     }
                 } catch (Exception e) {
                     status.successful = false;
@@ -165,9 +186,9 @@ public class AlertSlackPublisher extends AbstractPublishPlugin {
         return LOG;
     }
 
-    private void sendMessageToAChannel(SlackSession session, String channelName, String message) {
+    private void sendMessageToAChannel(SlackSession session, String channelName, String message, SlackAttachment attachment) {
         //get a channel
         SlackChannel channel = session.findChannelByName(channelName);
-        session.sendMessage(channel, message);
+        session.sendMessage(channel, message, attachment);
     }
 }