Skip to content

Commit bd48357

Browse files
arturdobojoshiste
authored andcommitted
Add slack notifications
1 parent de4c510 commit bd48357

File tree

5 files changed

+326
-1
lines changed

5 files changed

+326
-1
lines changed

spring-boot-admin-docs/src/main/asciidoc/index.adoc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,45 @@ To enable Hipchat notifications you need to create an API token from you Hipchat
466466
|
467467
|===
468468

469+
[slack-notifications]
470+
==== Slack notifications ====
471+
To enable Slack notifications you need to add a incoming Webhook under custom integrations on your Slack
472+
account and configure it appropriately.
473+
474+
.Slack notifications configuration options
475+
|===
476+
| Property name |Description |Default value
477+
478+
| spring.boot.admin.notify.slack.enabled
479+
| Enable Slack notifications
480+
| `true`
481+
482+
| spring.boot.admin.notify.slack.ignore-changes
483+
| Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.
484+
| `"UNKNOWN:UP"`
485+
486+
| spring.boot.admin.notify.slack.webhook-url
487+
| The Slack Webhook URL to send notifications
488+
|
489+
490+
| spring.boot.admin.notify.slack.channel
491+
| Optional channel name (without # at the beginning). If different than channel in Slack Webhooks settings
492+
|
493+
494+
| spring.boot.admin.notify.slack.icon
495+
| Optional icon name (without surrounding colons). If different than icon in Slack Webhooks settings
496+
|
497+
498+
| spring.boot.admin.notify.slack.username
499+
| Optional username to send notification if different than in Slack Webhooks settings
500+
| `Spring Boot Admin`
501+
502+
| spring.boot.admin.notify.slack.message
503+
| Message to use in the event. SpEL-expressions and Slack markups are supported
504+
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
505+
|
506+
|===
507+
469508
[reminder-notifactaions]
470509
==== Reminder notifications ====
471510
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 {
496535
<1> The reminders will be sent every 5 minutes.
497536
<2> Schedules sending of due reminders every 60 seconds.
498537

499-
500538
[[faqs]]
501539
== FAQs ==
502540
[qanda]

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/config/NotifierConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import de.codecentric.boot.admin.notify.Notifier;
4040
import de.codecentric.boot.admin.notify.NotifierListener;
4141
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
42+
import de.codecentric.boot.admin.notify.SlackNotifier;
4243

4344
@Configuration
4445
public class NotifierConfiguration {
@@ -123,4 +124,19 @@ public HipchatNotifier hipchatNotifier() {
123124
return new HipchatNotifier();
124125
}
125126
}
127+
128+
@Configuration
129+
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.slack", name = "webhook-url")
130+
@AutoConfigureBefore({ NotifierListenerConfiguration.class,
131+
CompositeNotifierConfiguration.class })
132+
public static class SlackNotifierConfiguration {
133+
@Bean
134+
@ConditionalOnMissingBean
135+
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.slack", name = "enabled", matchIfMissing = true)
136+
@ConfigurationProperties("spring.boot.admin.notify.slack")
137+
public SlackNotifier slackNotifier() {
138+
return new SlackNotifier();
139+
}
140+
}
141+
126142
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package de.codecentric.boot.admin.notify;
2+
3+
import java.net.URI;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import org.springframework.expression.Expression;
9+
import org.springframework.expression.ParserContext;
10+
import org.springframework.expression.spel.standard.SpelExpressionParser;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
14+
15+
/**
16+
* Notifier submitting events to Slack.
17+
*
18+
* @author Artur Dobosiewicz
19+
*/
20+
public class SlackNotifier extends AbstractStatusChangeNotifier {
21+
private static final String DEFAULT_MESSAGE = "*#{application.name}* (#{application.id}) is *#{to.status}*";
22+
23+
private final SpelExpressionParser parser = new SpelExpressionParser();
24+
private RestTemplate restTemplate = new RestTemplate();
25+
26+
/**
27+
* Webhook url for Slack API (i.e. https://hooks.slack.com/services/xxx)
28+
*/
29+
private URI webhookUrl;
30+
31+
/**
32+
* Optional channel name without # sign (i.e. somechannel)
33+
*/
34+
private String channel;
35+
36+
/**
37+
* Optional emoji icon without colons (i.e. my-emoji)
38+
*/
39+
private String icon;
40+
41+
/**
42+
* Optional username which sends notification
43+
*/
44+
private String username = "Spring Boot Admin";
45+
46+
/**
47+
* Message formatted using Slack markups. SpEL template using event as root
48+
*/
49+
private Expression message;
50+
51+
public SlackNotifier() {
52+
this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
53+
}
54+
55+
@Override
56+
protected void doNotify(ClientApplicationStatusChangedEvent event) throws Exception {
57+
restTemplate.postForEntity(webhookUrl, createMessage(event), Void.class);
58+
}
59+
60+
public void setRestTemplate(RestTemplate restTemplate) {
61+
this.restTemplate = restTemplate;
62+
}
63+
64+
public void setWebhookUrl(URI webhookUrl) {
65+
this.webhookUrl = webhookUrl;
66+
}
67+
68+
public void setChannel(String channel) {
69+
this.channel = channel;
70+
}
71+
72+
public void setIcon(String icon) {
73+
this.icon = icon;
74+
}
75+
76+
public void setUsername(String username) {
77+
this.username = username;
78+
}
79+
80+
public void setMessage(Expression message) {
81+
this.message = message;
82+
}
83+
84+
private Object createMessage(ClientApplicationStatusChangedEvent event) {
85+
Map<String, Object> messageJson = new HashMap<>();
86+
messageJson.put("username", username);
87+
if (icon != null) {
88+
messageJson.put("icon_emoji", ":" + icon + ":");
89+
}
90+
if (channel != null) {
91+
messageJson.put("channel", channel);
92+
}
93+
94+
Map<String, Object> attachments = new HashMap<>();
95+
attachments.put("text", getText(event));
96+
attachments.put("color", getColor(event));
97+
attachments.put("mrkdwn_in", Collections.singletonList("text"));
98+
99+
messageJson.put("attachments", Collections.singletonList(attachments));
100+
101+
return messageJson;
102+
}
103+
104+
private String getText(ClientApplicationStatusChangedEvent event) {
105+
return message.getValue(event, String.class);
106+
}
107+
108+
private String getColor(ClientApplicationStatusChangedEvent event) {
109+
return "UP".equals(event.getTo().getStatus()) ? "good" : "danger";
110+
}
111+
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/config/NotifierConfigurationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import de.codecentric.boot.admin.notify.Notifier;
4444
import de.codecentric.boot.admin.notify.NotifierListener;
4545
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
46+
import de.codecentric.boot.admin.notify.SlackNotifier;
4647

4748
public class NotifierConfigurationTest {
4849
private static final ClientApplicationEvent APP_DOWN = new ClientApplicationStatusChangedEvent(
@@ -91,6 +92,12 @@ public void test_hipchat() {
9192
assertThat(context.getBean(HipchatNotifier.class), is(instanceOf(HipchatNotifier.class)));
9293
}
9394

95+
@Test
96+
public void test_slack() {
97+
load(null, "spring.boot.admin.notify.slack.webhook-url:http://example.com");
98+
assertThat(context.getBean(SlackNotifier.class), is(instanceOf(SlackNotifier.class)));
99+
}
100+
94101
@Test
95102
public void test_multipleNotifiers() {
96103
load(TestMultipleNotifierConfig.class);
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package de.codecentric.boot.admin.notify;
2+
3+
import static org.mockito.Matchers.any;
4+
import static org.mockito.Matchers.eq;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
8+
import java.net.URI;
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import javax.annotation.Nullable;
14+
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.springframework.expression.Expression;
18+
import org.springframework.expression.ParserContext;
19+
import org.springframework.expression.spel.standard.SpelExpressionParser;
20+
import org.springframework.web.client.RestTemplate;
21+
22+
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
23+
import de.codecentric.boot.admin.model.Application;
24+
import de.codecentric.boot.admin.model.StatusInfo;
25+
26+
public class SlackNotifierTest {
27+
private static final String channel = "channel";
28+
private static final String icon = "icon";
29+
private static final String user = "user";
30+
private static final String appName = "App";
31+
private static final String id = "-id-";
32+
private static final String message = "test";
33+
34+
private SlackNotifier notifier;
35+
private RestTemplate restTemplate;
36+
37+
@Before
38+
public void setUp() {
39+
restTemplate = mock(RestTemplate.class);
40+
notifier = new SlackNotifier();
41+
notifier.setUsername(user);
42+
notifier.setWebhookUrl(URI.create("http://localhost/"));
43+
notifier.setRestTemplate(restTemplate);
44+
}
45+
46+
@Test
47+
public void test_onApplicationEvent_resolve() {
48+
StatusInfo infoDown = StatusInfo.ofDown();
49+
StatusInfo infoUp = StatusInfo.ofUp();
50+
51+
notifier.setChannel(channel);
52+
notifier.setIcon(icon);
53+
notifier.notify(getEvent(infoDown, infoUp));
54+
55+
Object expected = expectedMessage("good", user, icon, channel,
56+
standardMessage(infoUp.getStatus(), appName, id));
57+
58+
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
59+
}
60+
61+
@Test
62+
public void test_onApplicationEvent_resolve_without_channel_and_icon() {
63+
StatusInfo infoDown = StatusInfo.ofDown();
64+
StatusInfo infoUp = StatusInfo.ofUp();
65+
66+
notifier.notify(getEvent(infoDown, infoUp));
67+
68+
Object expected = expectedMessage("good", user, null, null,
69+
standardMessage(infoUp.getStatus(), appName, id));
70+
71+
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
72+
}
73+
74+
@Test
75+
public void test_onApplicationEvent_resolve_with_given_user() {
76+
StatusInfo infoDown = StatusInfo.ofDown();
77+
StatusInfo infoUp = StatusInfo.ofUp();
78+
String anotherUser = "another user";
79+
80+
notifier.setUsername(anotherUser);
81+
notifier.setChannel(channel);
82+
notifier.setIcon(icon);
83+
notifier.notify(getEvent(infoDown, infoUp));
84+
85+
Object expected = expectedMessage("good", anotherUser, icon, channel,
86+
standardMessage(infoUp.getStatus(), appName, id));
87+
88+
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
89+
}
90+
91+
@Test
92+
public void test_onApplicationEvent_resolve_with_given_message() {
93+
StatusInfo infoDown = StatusInfo.ofDown();
94+
StatusInfo infoUp = StatusInfo.ofUp();
95+
96+
Expression expression = new SpelExpressionParser().parseExpression(message,
97+
ParserContext.TEMPLATE_EXPRESSION);
98+
notifier.setMessage(expression);
99+
notifier.setChannel(channel);
100+
notifier.setIcon(icon);
101+
notifier.notify(getEvent(infoDown, infoUp));
102+
103+
Object expected = expectedMessage("good", user, icon, channel, message);
104+
105+
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
106+
}
107+
108+
@Test
109+
public void test_onApplicationEvent_trigger() {
110+
StatusInfo infoDown = StatusInfo.ofDown();
111+
StatusInfo infoUp = StatusInfo.ofUp();
112+
113+
notifier.setChannel(channel);
114+
notifier.setIcon(icon);
115+
notifier.notify(getEvent(infoUp, infoDown));
116+
117+
Object expected = expectedMessage("danger", user, icon, channel,
118+
standardMessage(infoDown.getStatus(), appName, id));
119+
120+
verify(restTemplate).postForEntity(any(URI.class), eq(expected), eq(Void.class));
121+
}
122+
123+
private ClientApplicationStatusChangedEvent getEvent(StatusInfo infoDown, StatusInfo infoUp) {
124+
return new ClientApplicationStatusChangedEvent(
125+
Application.create(appName).withId(id).withHealthUrl("http://health").build(),
126+
infoDown, infoUp);
127+
}
128+
129+
private Object expectedMessage(String color, String user, @Nullable String icon,
130+
@Nullable String channel, String message) {
131+
Map<String, Object> messageJson = new HashMap<>();
132+
messageJson.put("username", user);
133+
if (icon != null) {
134+
messageJson.put("icon_emoji", ":" + icon + ":");
135+
}
136+
if (channel != null) {
137+
messageJson.put("channel", channel);
138+
}
139+
140+
Map<String, Object> attachments = new HashMap<>();
141+
attachments.put("text", message);
142+
attachments.put("color", color);
143+
attachments.put("mrkdwn_in", Collections.singletonList("text"));
144+
145+
messageJson.put("attachments", Collections.singletonList(attachments));
146+
147+
return messageJson;
148+
}
149+
150+
private String standardMessage(String status, String appName, String id) {
151+
return "*" + appName + "* (" + id + ") is *" + status + "*";
152+
}
153+
}

0 commit comments

Comments
 (0)