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

yinyijun 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 17d836752e [feat] Http collect supports jsonpath parsing of numeric 
type (#3612)
17d836752e is described below

commit 17d836752edc996cb120fd8176b3fd14c71b0443
Author: Duansg <siguod...@gmail.com>
AuthorDate: Fri Aug 1 19:05:08 2025 +0800

    [feat] Http collect supports jsonpath parsing of numeric type (#3612)
    
    Co-authored-by: tomsun28 <tomsu...@outlook.com>
    Co-authored-by: Sherlock Yin <sherlock.yin1...@gmail.com>
---
 .../collector/collect/http/HttpCollectImpl.java    |  12 ++
 .../collect/http/HttpCollectImplTest.java          | 121 +++++++++++++++++++--
 2 files changed, 124 insertions(+), 9 deletions(-)

diff --git 
a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java
 
b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java
index 8af8a19fa1..833c5f2e20 100644
--- 
a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java
+++ 
b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java
@@ -604,6 +604,18 @@ public class HttpCollectImpl extends AbstractCollect {
                     }
                 }
                 builder.addValueRow(valueRowBuilder.build());
+            } else if (objectValue instanceof Number numberValue) {
+                CollectRep.ValueRow.Builder valueRowBuilder = 
CollectRep.ValueRow.newBuilder();
+                for (String alias : aliasFields) {
+                    if 
(NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
+                        valueRowBuilder.addColumn(responseTime.toString());
+                    } else if 
(CollectorConstants.KEYWORD.equalsIgnoreCase(alias)) {
+                        
valueRowBuilder.addColumn(Integer.toString(keywordNum));
+                    } else {
+                        valueRowBuilder.addColumn(numberValue.toString());
+                    }
+                }
+                builder.addValueRow(valueRowBuilder.build());
             }
         }
     }
diff --git 
a/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java
 
b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java
index 9aebc36af0..15b40efbf8 100644
--- 
a/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java
+++ 
b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java
@@ -17,15 +17,6 @@
 
 package org.apache.hertzbeat.collector.collect.http;
 
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.List;
-import java.util.ArrayList;
-
 import com.google.common.collect.Lists;
 import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
 import org.apache.hertzbeat.common.entity.job.Metrics;
@@ -34,6 +25,15 @@ import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
 /**
  * Test case for {@link HttpCollectImpl}
  */
@@ -186,4 +186,107 @@ class HttpCollectImplTest {
         assertEquals("0.0", secondRow.getColumns(2), "Second server CPU should 
be 0.0");
         assertEquals("0", secondRow.getColumns(3), "Second server memory 
should be 0");
     }
+
+    @Test
+    void parseResponseByJsonPath() throws Exception {
+        String jsonResponse = "{"
+                + "  \"name\": \"jvm.memory.used\","
+                + "  \"description\": \"The amount of used memory\","
+                + "  \"baseUnit\": \"bytes\","
+                + "  \"measurements\": ["
+                + "    {"
+                + "      \"statistic\": \"VALUE\","
+                + "      \"value\": 90282296"
+                + "    }"
+                + "  ],"
+                + "  \"availableTags\": ["
+                + "    {"
+                + "      \"tag\": \"area\","
+                + "      \"values\": ["
+                + "        \"heap\","
+                + "        \"nonheap\""
+                + "      ]"
+                + "    },"
+                + "    {"
+                + "      \"tag\": \"id\","
+                + "      \"values\": ["
+                + "        \"G1 Survivor Space\","
+                + "        \"G1 Eden Space\""
+                + "      ]"
+                + "    }"
+                + "  ]"
+                + "}";
+
+        HttpProtocol http = HttpProtocol.builder()
+                .parseType(DispatchConstants.PARSE_JSON_PATH)
+                .parseScript("$.availableTags[?(@.tag == \"id\")].values[*]")
+                .build();
+        List<CollectRep.ValueRow> capturedRows = new ArrayList<>();
+        CollectRep.MetricsData.Builder builder = new 
CollectRep.MetricsData.Builder() {
+            @Override
+            public CollectRep.MetricsData.Builder 
addValueRow(CollectRep.ValueRow valueRow) {
+                capturedRows.add(valueRow);
+                return super.addValueRow(valueRow);
+            }
+        };
+        Method parseMethod = HttpCollectImpl.class.getDeclaredMethod(
+                "parseResponseByJsonPath",
+                String.class,
+                List.class,
+                HttpProtocol.class,
+                CollectRep.MetricsData.Builder.class,
+                Long.class);
+        parseMethod.setAccessible(true);
+
+        // Call the method
+        parseMethod.invoke(httpCollectImpl, jsonResponse, 
Lists.newArrayList("id"), http, builder, 100L);
+
+        // Verify the results
+        assertEquals(2, capturedRows.size());
+        CollectRep.ValueRow firstRow = capturedRows.get(0);
+        assertEquals("G1 Survivor Space", firstRow.getColumns(0));
+        CollectRep.ValueRow secondRow = capturedRows.get(1);
+        assertEquals("G1 Eden Space", secondRow.getColumns(0));
+
+        // number
+        String numberJson = "{"
+                + "  \"name\": \"system.cpu.usage\","
+                + "  \"description\": \"The \\\"recent cpu usage\\\" of the 
system the application is running in\","
+                + "  \"measurements\": ["
+                + "    {"
+                + "      \"statistic\": \"VALUE\","
+                + "      \"value\": 0.268751364291017"
+                + "    }"
+                + "  ],"
+                + "  \"availableTags\": []"
+                + "}";
+        http = HttpProtocol.builder()
+                .parseType(DispatchConstants.PARSE_JSON_PATH)
+                .parseScript("$.measurements[?(@.statistic == 
\"VALUE\")].value")
+                .build();
+        capturedRows.clear();
+        builder = new CollectRep.MetricsData.Builder() {
+            @Override
+            public CollectRep.MetricsData.Builder 
addValueRow(CollectRep.ValueRow valueRow) {
+                capturedRows.add(valueRow);
+                return super.addValueRow(valueRow);
+            }
+        };
+        parseMethod = HttpCollectImpl.class.getDeclaredMethod(
+                "parseResponseByJsonPath",
+                String.class,
+                List.class,
+                HttpProtocol.class,
+                CollectRep.MetricsData.Builder.class,
+                Long.class);
+        parseMethod.setAccessible(true);
+
+        // Call the method
+        parseMethod.invoke(httpCollectImpl, numberJson, 
Lists.newArrayList("usage"), http, builder, 100L);
+
+        // Verify the results
+        assertEquals(1, capturedRows.size());
+        firstRow = capturedRows.get(0);
+        assertEquals("0.268751364291017", firstRow.getColumns(0));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@hertzbeat.apache.org
For additional commands, e-mail: notifications-h...@hertzbeat.apache.org

Reply via email to