Skip to content

Conversation

@Duansg
Copy link
Contributor

@Duansg Duansg commented Jul 28, 2025

What's changed?

Please refer to: #3609

For details:

  1. Added support for numeric type to the org.apache.hertzbeat.collector.collect.http.HttpCollectImpl#parseResponseByJsonPath method.
  2. Add new test cases.

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

  • I have added the necessary e2e tests and all cases have passed.
Snipaste_2025-07-29_00-46-30

Copy link
Contributor Author

@Duansg Duansg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configuration is as follows:

- name: system_cpu_usage
    i18n:
      zh-CN: CPU Usage
      en-US: CPU Usage
    priority: 5
    fields:
      - field: usage
        i18n:
          zh-CN: usage
          en-US: usage
        type: 0
        unit: '%'
    calculates:
      - usage=usage*100
    protocol: http
    http:
      host: ^_^host^_^
      port: ^_^port^_^
      url: ^_^base_path^_^/metrics/system.cpu.usage
      method: GET
      ssl: ^_^ssl^_^
      authorization:
        type: Basic Auth
        basicAuthUsername: ^_^username^_^
        basicAuthPassword: ^_^password^_^
      parseType: jsonPath
      parseScript: '$.measurements[?(@.statistic == "VALUE")].value'

@MasamiYui MasamiYui requested a review from Copilot July 31, 2025 10:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for parsing numeric types in JSONPath expressions for HTTP collection in HertzBeat. Previously, the JSONPath parser only handled string values; now it can also extract and process numeric values from JSON responses.

  • Added numeric type handling in the parseResponseByJsonPath method
  • Added comprehensive test coverage for both string and numeric JSONPath parsing scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
HttpCollectImpl.java Adds numeric type support to JSONPath parsing with proper handling of Number instances
HttpCollectImplTest.java Adds new test case covering both string array and numeric value extraction scenarios

Comment on lines +275 to +282
parseMethod = HttpCollectImpl.class.getDeclaredMethod(
"parseResponseByJsonPath",
String.class,
List.class,
HttpProtocol.class,
CollectRep.MetricsData.Builder.class,
Long.class);
parseMethod.setAccessible(true);
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reflection setup for parseMethod is duplicated code. Consider extracting this into a helper method or reusing the parseMethod variable from the earlier declaration.

Suggested change
parseMethod = HttpCollectImpl.class.getDeclaredMethod(
"parseResponseByJsonPath",
String.class,
List.class,
HttpProtocol.class,
CollectRep.MetricsData.Builder.class,
Long.class);
parseMethod.setAccessible(true);
parseMethod = getParseMethod();

Copilot uses AI. Check for mistakes.
}
}
builder.addValueRow(valueRowBuilder.build());
} else if (objectValue instanceof Number numberValue) {
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Member

@MasamiYui MasamiYui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@MasamiYui MasamiYui merged commit 17d8367 into apache:master Aug 1, 2025
3 checks passed
@github-project-automation github-project-automation bot moved this from To do to Done in Apache HertzBeat Aug 1, 2025
@Duansg Duansg deleted the feat-3609 branch August 1, 2025 13:39
sarthakeash pushed a commit to sarthakeash/hertzbeat that referenced this pull request Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants