Browse Source

linkify the URL in HTTP failure messages as a Slack hyperlink

Transforms 'request for "<URL>"' into Slack mrkdwn <URL|request>, so the
failure line renders the word 'request' as a clickable link instead of
showing the raw URL in quotes.
pull/15456/head
Oleksii Kuripko 5 months ago
parent
commit
c87b20e0c2
  1. 17
      monitoring/src/main/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotification.java
  2. 19
      monitoring/src/test/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotificationTest.java

17
monitoring/src/main/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotification.java

@ -19,6 +19,8 @@ import lombok.Getter;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Getter @Getter
public class ServiceFailureNotification implements Notification { public class ServiceFailureNotification implements Notification {
@ -46,9 +48,24 @@ public class ServiceFailureNotification implements Notification {
errorMsg = error.getClass().getSimpleName(); errorMsg = error.getClass().getSimpleName();
} }
errorMsg = stripResponseBody(errorMsg); errorMsg = stripResponseBody(errorMsg);
errorMsg = linkifyRequestUrl(errorMsg);
return String.format("%s - Failure: %s (number of subsequent failures: %s)", serviceKey, errorMsg, failuresCount); return String.format("%s - Failure: %s (number of subsequent failures: %s)", serviceKey, errorMsg, failuresCount);
} }
private static final Pattern REQUEST_URL_PATTERN = Pattern.compile("request for \"(https?://[^\"\\s]+)\"");
static String linkifyRequestUrl(String msg) {
if (msg == null) {
return null;
}
Matcher m = REQUEST_URL_PATTERN.matcher(msg);
if (!m.find()) {
return msg;
}
// Slack mrkdwn link: <url|label>
return m.replaceAll("<$1|request>");
}
static String stripResponseBody(String msg) { static String stripResponseBody(String msg) {
if (msg == null) { if (msg == null) {
return null; return null;

19
monitoring/src/test/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotificationTest.java

@ -53,6 +53,25 @@ class ServiceFailureNotificationTest {
assertThat(ServiceFailureNotification.stripResponseBody(null)).isNull(); assertThat(ServiceFailureNotification.stripResponseBody(null)).isNull();
} }
@Test
void linkifyReplacesRequestForUrlWithSlackMrkdwnLink() {
String msg = "503 Service Temporarily Unavailable on POST request for \"https://qa-tb-pe-lts43.iot-private.cloud/api/auth/login\"";
assertThat(ServiceFailureNotification.linkifyRequestUrl(msg))
.isEqualTo("503 Service Temporarily Unavailable on POST <https://qa-tb-pe-lts43.iot-private.cloud/api/auth/login|request>");
}
@Test
void linkifyLeavesMessagesWithoutRequestUrlUntouched() {
String msg = "Connection refused";
assertThat(ServiceFailureNotification.linkifyRequestUrl(msg)).isEqualTo(msg);
}
@Test
void linkifyHandlesNull() {
assertThat(ServiceFailureNotification.linkifyRequestUrl(null)).isNull();
}
@Test @Test
void shortNameUsesShortNameProviderWhenAvailable() { void shortNameUsesShortNameProviderWhenAvailable() {
ShortNameProvider provider = () -> "MQTT"; ShortNameProvider provider = () -> "MQTT";

Loading…
Cancel
Save