Minor refactoring for monitoring service

This commit is contained in:
ViacheslavKlimov 2023-04-10 12:12:57 +03:00
parent df16a6a310
commit 470fbfa768
12 changed files with 25 additions and 68 deletions

View File

@ -33,11 +33,6 @@
<pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org" level="WARN"/>
<logger name="org.thingsboard.server" level="INFO"/>
@ -46,7 +41,6 @@
<root level="INFO">
<appender-ref ref="fileLogAppender"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -19,8 +19,8 @@ import lombok.RequiredArgsConstructor;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.thingsboard.monitoring.config.WsConfig;
import org.thingsboard.monitoring.data.Latencies;
import org.thingsboard.monitoring.service.MonitoringReporter;
import org.thingsboard.monitoring.util.TbStopWatch;
@ -32,20 +32,23 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class WsClientFactory {
private final WsConfig wsConfig;
private final MonitoringReporter monitoringReporter;
private final TbStopWatch stopWatch;
@Value("${monitoring.ws.base_url}")
private String baseUrl;
@Value("${monitoring.ws.request_timeout_ms}")
private int requestTimeoutMs;
public WsClient createClient(String accessToken) throws Exception {
URI uri = new URI(wsConfig.getBaseUrl() + "/api/ws/plugins/telemetry?token=" + accessToken);
URI uri = new URI(baseUrl + "/api/ws/plugins/telemetry?token=" + accessToken);
stopWatch.start();
WsClient wsClient = new WsClient(uri, wsConfig.getRequestTimeoutMs());
if (wsConfig.getBaseUrl().startsWith("wss")) {
WsClient wsClient = new WsClient(uri, requestTimeoutMs);
if (baseUrl.startsWith("wss")) {
SSLContextBuilder builder = SSLContexts.custom();
builder.loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true);
wsClient.setSocketFactory(builder.build().getSocketFactory());
}
boolean connected = wsClient.connectBlocking(wsConfig.getRequestTimeoutMs(), TimeUnit.MILLISECONDS);
boolean connected = wsClient.connectBlocking(requestTimeoutMs, TimeUnit.MILLISECONDS);
if (!connected) {
throw new IllegalStateException("Failed to establish WS session");
}

View File

@ -1,33 +0,0 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.monitoring.config;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Getter
public class WsConfig {
@Value("${monitoring.ws.base_url}")
private String baseUrl;
@Value("${monitoring.ws.request_timeout_ms}")
private int requestTimeoutMs;
@Value("${monitoring.ws.check_timeout_ms}")
private int resultCheckTimeoutMs;
}

View File

@ -22,10 +22,8 @@ public class Latencies {
public static final String WS_UPDATE = "wsUpdate";
public static final String WS_CONNECT = "wsConnect";
public static final String LOG_IN = "logIn";
public static final String WEB_UI_LOAD = "webUiLoad";
public static String transportRequest(TransportType transportType) {
int a = 2;
return String.format("%sTransportRequest", transportType.name().toLowerCase());
}

View File

@ -21,6 +21,4 @@ public interface NotificationChannel {
void sendNotification(Notification notification);
void sendNotification(String message);
}

View File

@ -51,8 +51,7 @@ public class SlackNotificationChannel implements NotificationChannel {
sendNotification(notification.getText());
}
@Override
public void sendNotification(String message) {
private void sendNotification(String message) {
restTemplate.postForObject(webhookUrl, Map.of("text", message), String.class);
}

View File

@ -18,11 +18,11 @@ package org.thingsboard.monitoring.transport;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.monitoring.client.WsClient;
import org.thingsboard.monitoring.config.MonitoringTargetConfig;
import org.thingsboard.monitoring.config.TransportType;
import org.thingsboard.monitoring.config.WsConfig;
import org.thingsboard.monitoring.config.service.TransportMonitoringConfig;
import org.thingsboard.monitoring.data.Latencies;
import org.thingsboard.monitoring.data.MonitoredServiceKey;
@ -41,13 +41,13 @@ public abstract class TransportHealthChecker<C extends TransportMonitoringConfig
protected final C config;
protected final MonitoringTargetConfig target;
private TransportInfo transportInfo;
@Autowired
private WsConfig wsConfig;
@Autowired
private MonitoringReporter reporter;
@Autowired
private TbStopWatch stopWatch;
@Value("${monitoring.check_timeout_ms}")
private int resultCheckTimeoutMs;
public static final String TEST_TELEMETRY_KEY = "testData";
@ -96,11 +96,11 @@ public abstract class TransportHealthChecker<C extends TransportMonitoringConfig
private void checkWsUpdate(WsClient wsClient, String testValue) {
stopWatch.start();
wsClient.waitForUpdate(wsConfig.getResultCheckTimeoutMs());
wsClient.waitForUpdate(resultCheckTimeoutMs);
log.trace("[{}] Waited for WS update. Last WS msg: {}", transportInfo, wsClient.lastMsg);
Object update = wsClient.getTelemetryUpdate(target.getDevice().getId(), TEST_TELEMETRY_KEY);
if (update == null) {
throw new TransportFailureException("No WS update arrived within " + wsConfig.getResultCheckTimeoutMs() + " ms");
throw new TransportFailureException("No WS update arrived within " + resultCheckTimeoutMs + " ms");
} else if (!update.toString().equals(testValue)) {
throw new TransportFailureException("Was expecting value " + testValue + " but got " + update);
}

View File

@ -16,29 +16,26 @@
package org.thingsboard.monitoring.util;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class TbStopWatch {
private final ThreadLocal<StopWatch> internal = ThreadLocal.withInitial(StopWatch::new);
private final StopWatch internal = new StopWatch();
public void start() {
StopWatch internal = getInternal();
internal.reset();
internal.start();
}
public long getTime() {
StopWatch internal = getInternal();
internal.stop();
long nanoTime = internal.getNanoTime();
internal.reset();
return nanoTime;
}
private StopWatch getInternal() {
return this.internal.get();
}
}

View File

@ -29,11 +29,12 @@ monitoring:
ws:
# WebSocket url, wss://DOMAIN by default
base_url: '${WS_BASE_URL:wss://${monitoring.domain}}'
# Maximum time between request to transport and WebSocket update
check_timeout_ms: '${WS_CHECK_TIMEOUT_MS:5000}'
# WebSocket request timeout
request_timeout_ms: '${WS_REQUEST_TIMEOUT_MS:3000}'
# Maximum time between request to transport and WebSocket update
check_timeout_ms: '${CHECK_TIMEOUT_MS:5000}'
# Failures threshold for notifying
failures_threshold: '${FAILURES_THRESHOLD:2}'
# Whether to notify about next failures after first notification (will notify after each FAILURES_THRESHOLD failures)

View File

@ -1,5 +1,5 @@
#
# Copyright © 2016-2022 The Thingsboard Authors
# Copyright © 2016-2023 The Thingsboard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,6 +1,6 @@
#!/bin/bash
#
# Copyright © 2016-2022 The Thingsboard Authors
# Copyright © 2016-2023 The Thingsboard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright © 2016-2022 The Thingsboard Authors
Copyright © 2016-2023 The Thingsboard Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.