Copilot commented on code in PR #3612: URL: https://github.com/apache/hertzbeat/pull/3612#discussion_r2244944782
########## hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java: ########## @@ -604,6 +604,18 @@ private void parseResponseByJsonPath(String resp, List<String> aliasFields, Http } } builder.addValueRow(valueRowBuilder.build()); + } else if (objectValue instanceof Number numberValue) { Review Comment: The logic for handling Number values duplicates the alias field processing from the string array case above. Consider extracting the alias field processing into a helper method to reduce code duplication. ########## hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java: ########## @@ -186,4 +186,107 @@ public CollectRep.MetricsData.Builder addValueRow(CollectRep.ValueRow valueRow) 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); Review Comment: The reflection setup for parseMethod is duplicated code. Consider extracting this into a helper method or reusing the parseMethod variable from the earlier declaration. ```suggestion parseMethod = getParseMethod(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@hertzbeat.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@hertzbeat.apache.org For additional commands, e-mail: notifications-h...@hertzbeat.apache.org