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