Browse Source

linkify URL for 'request: Connect to <URL> failed' error variant

Spring wraps Apache HttpClient connect failures as 'I/O error on POST request:
Connect to <URL> failed: <reason>'. Same treatment as the RestClient variant:
the URL is embedded in the 'request' word and the 'Connect to ... failed'
restatement is dropped from the visible text.
pull/15456/head
Oleksii Kuripko 3 months ago
parent
commit
27c2844fec
  1. 19
      monitoring/src/main/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotification.java
  2. 12
      monitoring/src/test/java/org/thingsboard/monitoring/data/notification/ServiceFailureNotificationTest.java

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

@ -52,18 +52,25 @@ public class ServiceFailureNotification implements Notification {
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]+)\"");
// Spring RestClient: '... request for "<URL>"'
private static final Pattern REQUEST_FOR_URL_PATTERN = Pattern.compile("request for \"(https?://[^\"\\s]+)\"");
// Apache HttpClient wrapped by Spring: 'I/O error on POST request: Connect to <URL> failed: <reason>'
private static final Pattern REQUEST_CONNECT_PATTERN = Pattern.compile("request: Connect to (https?://\\S+?) failed:");
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>");
Matcher m = REQUEST_FOR_URL_PATTERN.matcher(msg);
if (m.find()) {
return m.replaceAll("<$1|request>");
}
Matcher m2 = REQUEST_CONNECT_PATTERN.matcher(msg);
if (m2.find()) {
return m2.replaceAll("<$1|request>:");
}
return msg;
}
static String stripResponseBody(String msg) {

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

@ -55,10 +55,18 @@ class ServiceFailureNotificationTest {
@Test
void linkifyReplacesRequestForUrlWithSlackMrkdwnLink() {
String msg = "503 Service Temporarily Unavailable on POST request for \"https://qa-tb-pe-lts43.iot-private.cloud/api/auth/login\"";
String msg = "503 Service Temporarily Unavailable on POST request for \"https://example.com/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>");
.isEqualTo("503 Service Temporarily Unavailable on POST <https://example.com/api/auth/login|request>");
}
@Test
void linkifyReplacesRequestConnectToUrlFailed() {
String msg = "I/O error on POST request: Connect to https://example.com:443 failed: Connect timed out";
assertThat(ServiceFailureNotification.linkifyRequestUrl(msg))
.isEqualTo("I/O error on POST <https://example.com:443|request>: Connect timed out");
}
@Test

Loading…
Cancel
Save