Monitoring service: notification message prefix; minor improvements
This commit is contained in:
parent
c43a265868
commit
04cab4d5d1
@ -25,7 +25,7 @@ public class TransportInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s transport (%s)", transportType, baseUrl);
|
||||
return String.format("%s (%s)", transportType.getName(), baseUrl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -27,11 +27,12 @@ import org.thingsboard.monitoring.service.transport.impl.MqttTransportHealthChec
|
||||
@Getter
|
||||
public enum TransportType {
|
||||
|
||||
MQTT(MqttTransportHealthChecker.class),
|
||||
COAP(CoapTransportHealthChecker.class),
|
||||
HTTP(HttpTransportHealthChecker.class),
|
||||
LWM2M(Lwm2mTransportHealthChecker.class);
|
||||
MQTT("MQTT", MqttTransportHealthChecker.class),
|
||||
COAP("CoAP",CoapTransportHealthChecker.class),
|
||||
HTTP("HTTP", HttpTransportHealthChecker.class),
|
||||
LWM2M("LwM2M", Lwm2mTransportHealthChecker.class);
|
||||
|
||||
private final String name;
|
||||
private final Class<? extends TransportHealthChecker<?>> serviceClass;
|
||||
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ package org.thingsboard.monitoring.notification;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.thingsboard.monitoring.data.notification.Notification;
|
||||
import org.thingsboard.monitoring.notification.channels.NotificationChannel;
|
||||
@ -24,7 +26,6 @@ import org.thingsboard.monitoring.notification.channels.NotificationChannel;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ -34,15 +35,20 @@ public class NotificationService {
|
||||
private final List<NotificationChannel> notificationChannels;
|
||||
private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
public void sendNotification(Notification notification) {
|
||||
forEachNotificationChannel(notificationChannel -> notificationChannel.sendNotification(notification));
|
||||
}
|
||||
@Value("${monitoring.notifications.message_prefix}")
|
||||
private String messagePrefix;
|
||||
|
||||
private void forEachNotificationChannel(Consumer<NotificationChannel> function) {
|
||||
public void sendNotification(Notification notification) {
|
||||
String message;
|
||||
if (StringUtils.isEmpty(messagePrefix)) {
|
||||
message = notification.getText();
|
||||
} else {
|
||||
message = messagePrefix + System.lineSeparator() + notification.getText();
|
||||
}
|
||||
notificationChannels.forEach(notificationChannel -> {
|
||||
notificationExecutor.submit(() -> {
|
||||
try {
|
||||
function.accept(notificationChannel);
|
||||
notificationChannel.sendNotification(message);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to send notification to {}", notificationChannel.getClass().getSimpleName(), e);
|
||||
}
|
||||
|
||||
@ -15,10 +15,8 @@
|
||||
*/
|
||||
package org.thingsboard.monitoring.notification.channels;
|
||||
|
||||
import org.thingsboard.monitoring.data.notification.Notification;
|
||||
|
||||
public interface NotificationChannel {
|
||||
|
||||
void sendNotification(Notification notification);
|
||||
void sendNotification(String message);
|
||||
|
||||
}
|
||||
|
||||
@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.thingsboard.monitoring.data.notification.Notification;
|
||||
import org.thingsboard.monitoring.notification.channels.NotificationChannel;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@ -29,11 +28,11 @@ import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@ConditionalOnProperty(value = "monitoring.notification_channels.slack.enabled", havingValue = "true")
|
||||
@ConditionalOnProperty(value = "monitoring.notifications.slack.enabled", havingValue = "true")
|
||||
@Slf4j
|
||||
public class SlackNotificationChannel implements NotificationChannel {
|
||||
|
||||
@Value("${monitoring.notification_channels.slack.webhook_url}")
|
||||
@Value("${monitoring.notifications.slack.webhook_url}")
|
||||
private String webhookUrl;
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
@ -47,11 +46,7 @@ public class SlackNotificationChannel implements NotificationChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendNotification(Notification notification) {
|
||||
sendNotification(notification.getText());
|
||||
}
|
||||
|
||||
private void sendNotification(String message) {
|
||||
public void sendNotification(String message) {
|
||||
restTemplate.postForObject(webhookUrl, Map.of("text", message), String.class);
|
||||
}
|
||||
|
||||
|
||||
@ -97,7 +97,8 @@ monitoring:
|
||||
# To add more targets, use following environment variables:
|
||||
# monitoring.transports.lwm2m.targets[1].base_url, monitoring.transports.lwm2m.targets[2].base_url, etc.
|
||||
|
||||
notification_channels:
|
||||
notifications:
|
||||
message_prefix: '${NOTIFICATION_MESSAGE_PREFIX:}'
|
||||
slack:
|
||||
# Enable notifying via Slack
|
||||
enabled: '${SLACK_NOTIFICATION_CHANNEL_ENABLED:false}'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user