This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new c0c9ff079 [improve] optimize websocket monitor (#1838)
c0c9ff079 is described below

commit c0c9ff07926de1cc52352d801e7a40b86d676952
Author: LiuTianyou <[email protected]>
AuthorDate: Thu Apr 25 14:47:36 2024 +0800

    [improve] optimize websocket monitor (#1838)
    
    Co-authored-by: tomsun28 <[email protected]>
---
 .../collect/websocket/WebsocketCollectImpl.java    | 41 ++++++++++++++--------
 .../entity/job/protocol/WebsocketProtocol.java     |  5 +++
 .../src/main/resources/define/app-websocket.yml    | 16 +++++++++
 3 files changed, 47 insertions(+), 15 deletions(-)

diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java
index 5bdbd35ad..1f59f761a 100644
--- 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java
@@ -44,6 +44,7 @@ import org.apache.hertzbeat.common.entity.job.Metrics;
 import org.apache.hertzbeat.common.entity.job.protocol.WebsocketProtocol;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.apache.hertzbeat.common.util.CommonUtil;
+import org.springframework.util.Assert;
 
 /**
  * Websocket Collect
@@ -62,6 +63,11 @@ public class WebsocketCollectImpl extends AbstractCollect {
             return;
         }
         WebsocketProtocol websocketProtocol = metrics.getWebsocket();
+        // Compatible with monitoring templates without path parameters
+        if (StringUtils.isBlank(websocketProtocol.getPath())) {
+            websocketProtocol.setPath("/");
+        }
+        checkParam(websocketProtocol);
         String host = websocketProtocol.getHost();
         String port = websocketProtocol.getPort();
         Socket socket = null;
@@ -74,13 +80,12 @@ public class WebsocketCollectImpl extends AbstractCollect {
                 long responseTime = System.currentTimeMillis() - startTime;
                 OutputStream out = socket.getOutputStream();
                 InputStream in = socket.getInputStream();
-
-
-                send(out);
+                
+                send(out, websocketProtocol);
                 Map<String, String> resultMap = readHeaders(in);
                 resultMap.put(CollectorConstants.RESPONSE_TIME, 
Long.toString(responseTime));
 
-                //  关闭输出流和Socket连接
+                // Close the output stream and socket connection
                 in.close();
                 out.close();
                 socket.close();
@@ -118,10 +123,10 @@ public class WebsocketCollectImpl extends AbstractCollect 
{
         return DispatchConstants.PROTOCOL_WEBSOCKET;
     }
 
-    private static void send(OutputStream out) throws IOException {
+    private static void send(OutputStream out, WebsocketProtocol 
websocketProtocol) throws IOException {
         byte[] key = generateRandomKey();
         String base64Key = base64Encode(key);
-        String requestLine = "GET / HTTP/1.1\r\n";
+        String requestLine = "GET " + websocketProtocol.getPath() + " 
HTTP/1.1\r\n";
         out.write(requestLine.getBytes());
         String hostName = InetAddress.getLocalHost().getHostAddress();
         out.write(("Host:" + hostName + "\r\n").getBytes());
@@ -135,7 +140,7 @@ public class WebsocketCollectImpl extends AbstractCollect {
         out.flush();
     }
 
-    // 读取响应头
+    // Read response headers
     private static Map<String, String> readHeaders(InputStream in) throws 
IOException {
 
         Map<String, String> map = new HashMap<>(8);
@@ -147,19 +152,19 @@ public class WebsocketCollectImpl extends AbstractCollect 
{
             if (separatorIndex != -1) {
                 String key = line.substring(0, separatorIndex).trim();
                 String value = line.substring(separatorIndex + 1).trim();
-                // 首字母小写化
+                // Lowercase first letter
                 map.put(StringUtils.uncapitalize(key), value);
             } else {
-                // 切割HTTP/1.1, 101, Switching Protocols
+                // Cut HTTP/1.1, 101, Switching Protocols
                 String[] parts = line.split("\\s+", 3);
                 if (parts.length == 3) {
-                    for (int i = 0; i < parts.length; i++) {
-                        if (parts[i].startsWith("HTTP")) {
-                            map.put("httpVersion", parts[i]);
-                        } else if (Character.isDigit(parts[i].charAt(0))) {
-                            map.put("responseCode", parts[i]);
+                    for (String part : parts) {
+                        if (part.startsWith("HTTP")) {
+                            map.put("httpVersion", part);
+                        } else if (StringUtils.isNotBlank(part) && 
Character.isDigit(part.charAt(0))) {
+                            map.put("responseCode", part);
                         } else {
-                            map.put("statusMessage", parts[i]);
+                            map.put("statusMessage", part);
                         }
                     }
                 }
@@ -175,6 +180,12 @@ public class WebsocketCollectImpl extends AbstractCollect {
         return key;
     }
 
+    private void checkParam(WebsocketProtocol protocol) {
+        Assert.hasText(protocol.getHost(), "Websocket Protocol host is 
required.");
+        Assert.hasText(protocol.getPort(), "Websocket Protocol port is 
required.");
+        Assert.hasText(protocol.getPath(), "Websocket Protocol path is 
required.");
+    }
+    
     private static String base64Encode(byte[] data) {
         return Base64.getEncoder().encodeToString(data);
     }
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
index 9d46b0002..2118b13ca 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
@@ -39,4 +39,9 @@ public class WebsocketProtocol {
      * Port number
      */
     private String port;
+    
+    /**
+     * The path to the websocket endpoint
+     */
+    private String path;
 }
diff --git a/manager/src/main/resources/define/app-websocket.yml 
b/manager/src/main/resources/define/app-websocket.yml
index 4a3e3b9f8..d406c2f4e 100644
--- a/manager/src/main/resources/define/app-websocket.yml
+++ b/manager/src/main/resources/define/app-websocket.yml
@@ -63,6 +63,21 @@ params:
     # required-true or false
     # 是否是必输项 true-必填 false-可选
     required: true
+  # field-param field key
+  # field-字段名称标识符
+  - field: path
+    # name-param field display i18n name
+    # name-参数字段显示名称
+    name:
+      zh-CN: WebSocket服务的路径
+      en-US: Path of WebSocket service
+    # type-param field type(most mapping the html input type)
+    # type-字段类型,样式(大部分映射input标签type属性)
+    type: text
+    # required-true or false
+    # 是否是必输项 true-必填 false-可选
+    required: true
+    defaultValue: /
 # collect metrics config list
 # 采集指标配置列表
 metrics:
@@ -124,3 +139,4 @@ metrics:
       # 远程登录主机
       host: ^_^host^_^
       port: ^_^port^_^
+      path: ^_^path^_^


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to