Commit bd48357e by dobo Committed by Johannes Edmeier

Add slack notifications

parent de4c510a
......@@ -466,6 +466,45 @@ To enable Hipchat notifications you need to create an API token from you Hipchat
|
|===
[slack-notifications]
==== Slack notifications ====
To enable Slack notifications you need to add a incoming Webhook under custom integrations on your Slack
account and configure it appropriately.
.Slack notifications configuration options
|===
| Property name |Description |Default value
| spring.boot.admin.notify.slack.enabled
| Enable Slack notifications
| `true`
| spring.boot.admin.notify.slack.ignore-changes
| Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.
| `"UNKNOWN:UP"`
| spring.boot.admin.notify.slack.webhook-url
| The Slack Webhook URL to send notifications
|
| spring.boot.admin.notify.slack.channel
| Optional channel name (without # at the beginning). If different than channel in Slack Webhooks settings
|
| spring.boot.admin.notify.slack.icon
| Optional icon name (without surrounding colons). If different than icon in Slack Webhooks settings
|
| spring.boot.admin.notify.slack.username
| Optional username to send notification if different than in Slack Webhooks settings
| `Spring Boot Admin`
| spring.boot.admin.notify.slack.message
| Message to use in the event. SpEL-expressions and Slack markups are supported
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
|
|===
[reminder-notifactaions]
==== Reminder notifications ====
To get reminders for down/offline applications you can add a `RemindingNotifier` to your `ApplicationContext`. The `RemindingNotifier` uses another `Notifier` as delegate to send the reminders.
......@@ -496,7 +535,6 @@ public class ReminderConfiguration {
<1> The reminders will be sent every 5 minutes.
<2> Schedules sending of due reminders every 60 seconds.
[[faqs]]
== FAQs ==
[qanda]
......
......@@ -39,6 +39,7 @@ import de.codecentric.boot.admin.notify.MailNotifier;
import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.NotifierListener;
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.notify.SlackNotifier;
@Configuration
public class NotifierConfiguration {
......@@ -123,4 +124,19 @@ public class NotifierConfiguration {
return new HipchatNotifier();
}
}
@Configuration
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.slack", name = "webhook-url")
@AutoConfigureBefore({ NotifierListenerConfiguration.class,
CompositeNotifierConfiguration.class })
public static class SlackNotifierConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.slack", name = "enabled", matchIfMissing = true)
@ConfigurationProperties("spring.boot.admin.notify.slack")
public SlackNotifier slackNotifier() {
return new SlackNotifier();
}
}
}
package de.codecentric.boot.admin.notify;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
/**
* Notifier submitting events to Slack.
*
* @author Artur Dobosiewicz
*/
public class SlackNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_MESSAGE = "*#{application.name}* (#{application.id}) is *#{to.status}*";
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate = new RestTemplate();
/**
* Webhook url for Slack API (i.e. https://hooks.slack.com/services/xxx)
*/
private URI webhookUrl;
/**
* Optional channel name without # sign (i.e. somechannel)
*/
private String channel;
/**
* Optional emoji icon without colons (i.e. my-emoji)
*/
private String icon;
/**
* Optional username which sends notification
*/
private String username = "Spring Boot Admin";
/**
* Message formatted using Slack markups. SpEL template using event as root
*/
private Expression message;
public SlackNotifier() {
this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected void doNotify(ClientApplicationStatusChangedEvent event) throws Exception {
restTemplate.postForEntity(webhookUrl, createMessage(event), Void.class);
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void setWebhookUrl(URI webhookUrl) {
this.webhookUrl = webhookUrl;
}
public void setChannel(String channel) {
this.channel = channel;
}
public void setIcon(String icon) {
this.icon = icon;
}
public void setUsername(String username) {
this.username = username;
}
public void setMessage(Expression message) {
this.message = message;
}
private Object createMessage(ClientApplicationStatusChangedEvent event) {
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", username);
if (icon != null) {
messageJson.put("icon_emoji", ":" + icon + ":");
}
if (channel != null) {
messageJson.put("channel", channel);
}
Map<String, Object> attachments = new HashMap<>();
attachments.put("text", getText(event));
attachments.put("color", getColor(event));
attachments.put("mrkdwn_in", Collections.singletonList("text"));
messageJson.put("attachments", Collections.singletonList(attachments));
return messageJson;
}
private String getText(ClientApplicationStatusChangedEvent event) {
return message.getValue(event, String.class);
}
private String getColor(ClientApplicationStatusChangedEvent event) {
return "UP".equals(event.getTo().getStatus()) ? "good" : "danger";
}
}
......@@ -43,6 +43,7 @@ import de.codecentric.boot.admin.notify.MailNotifier;
import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.NotifierListener;
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.notify.SlackNotifier;
public class NotifierConfigurationTest {
private static final ClientApplicationEvent APP_DOWN = new ClientApplicationStatusChangedEvent(
......@@ -92,6 +93,12 @@ public class NotifierConfigurationTest {
}
@Test
public void test_slack() {
load(null, "spring.boot.admin.notify.slack.webhook-url:http://example.com");
assertThat(context.getBean(SlackNotifier.class), is(instanceOf(SlackNotifier.class)));
}
@Test
public void test_multipleNotifiers() {
load(TestMultipleNotifierConfig.class);
assertThat(context.getBean(Notifier.class), is(instanceOf(CompositeNotifier.class)));
......
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
public class SlackNotifierTest {
private static final String channel = "channel";
private static final String icon = "icon";
private static final String user = "user";
private static final String appName = "App";
private static final String id = "-id-";
private static final String message = "test";
private SlackNotifier notifier;
private RestTemplate restTemplate;
@Before
public void setUp() {
restTemplate = mock(RestTemplate.class);
notifier = new SlackNotifier();
notifier.setUsername(user);
notifier.setWebhookUrl(URI.create("http://localhost/"));
notifier.setRestTemplate(restTemplate);
}
@Test
public void test_onApplicationEvent_resolve() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
notifier.setChannel(channel);
notifier.setIcon(icon);
notifier.notify(getEvent(infoDown, infoUp));
Object expected = expectedMessage("good", user, icon, channel,
standardMessage(infoUp.getStatus(), appName, id));
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
}
@Test
public void test_onApplicationEvent_resolve_without_channel_and_icon() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
notifier.notify(getEvent(infoDown, infoUp));
Object expected = expectedMessage("good", user, null, null,
standardMessage(infoUp.getStatus(), appName, id));
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
}
@Test
public void test_onApplicationEvent_resolve_with_given_user() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
String anotherUser = "another user";
notifier.setUsername(anotherUser);
notifier.setChannel(channel);
notifier.setIcon(icon);
notifier.notify(getEvent(infoDown, infoUp));
Object expected = expectedMessage("good", anotherUser, icon, channel,
standardMessage(infoUp.getStatus(), appName, id));
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
}
@Test
public void test_onApplicationEvent_resolve_with_given_message() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
Expression expression = new SpelExpressionParser().parseExpression(message,
ParserContext.TEMPLATE_EXPRESSION);
notifier.setMessage(expression);
notifier.setChannel(channel);
notifier.setIcon(icon);
notifier.notify(getEvent(infoDown, infoUp));
Object expected = expectedMessage("good", user, icon, channel, message);
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
}
@Test
public void test_onApplicationEvent_trigger() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
notifier.setChannel(channel);
notifier.setIcon(icon);
notifier.notify(getEvent(infoUp, infoDown));
Object expected = expectedMessage("danger", user, icon, channel,
standardMessage(infoDown.getStatus(), appName, id));
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
}
private ClientApplicationStatusChangedEvent getEvent(StatusInfo infoDown, StatusInfo infoUp) {
return new ClientApplicationStatusChangedEvent(
Application.create(appName).withId(id).withHealthUrl("http://health").build(),
infoDown, infoUp);
}
private Object expectedMessage(String color, String user, @Nullable String icon,
@Nullable String channel, String message) {
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", user);
if (icon != null) {
messageJson.put("icon_emoji", ":" + icon + ":");
}
if (channel != null) {
messageJson.put("channel", channel);
}
Map<String, Object> attachments = new HashMap<>();
attachments.put("text", message);
attachments.put("color", color);
attachments.put("mrkdwn_in", Collections.singletonList("text"));
messageJson.put("attachments", Collections.singletonList(attachments));
return messageJson;
}
private String standardMessage(String status, String appName, String id) {
return "*" + appName + "* (" + id + ") is *" + status + "*";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment