-
Notifications
You must be signed in to change notification settings - Fork 0
[improve] Add jdbc common collect e2e code (#3273) #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Cyanty <[email protected]>
Signed-off-by: Cyanty <[email protected]>
Signed-off-by: Cyanty <[email protected]>
Signed-off-by: Cyanty <[email protected]>
Signed-off-by: Cyanty <[email protected]>
please fix ci |
WalkthroughThe changes introduce enhanced support for database testing using Testcontainers within the Hertzbeat collector modules. A new enum for database image management is added, and a new end-to-end test class is implemented to verify JDBC metric collection against a PostgreSQL Testcontainer. The JDBC URL construction logic is extended to handle "testcontainers" as a platform. Maven configuration files are updated to manage Testcontainers dependencies via a BOM and to include specific JDBC drivers and Testcontainers modules for MySQL and PostgreSQL, ensuring consistent versions and improved test isolation. Changes
Sequence Diagram(s)sequenceDiagram
participant TestRunner
participant JdbcCommonCollectE2eTest
participant Testcontainer(PostgreSQL)
participant JdbcCommonCollect
TestRunner->>JdbcCommonCollectE2eTest: setUp()
JdbcCommonCollectE2eTest->>Testcontainer(PostgreSQL): Start container
JdbcCommonCollectE2eTest->>JdbcCommonCollect: Initialize with test config
loop For each metric
JdbcCommonCollectE2eTest->>JdbcCommonCollect: collectMetrics(metric)
JdbcCommonCollect->>Testcontainer(PostgreSQL): Connect via JDBC (testcontainers URL)
JdbcCommonCollect-->>JdbcCommonCollectE2eTest: Return collected data
JdbcCommonCollectE2eTest->>JdbcCommonCollectE2eTest: Validate results
end
JdbcCommonCollectE2eTest-->>TestRunner: Test results
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
@coderabbitai full review |
✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (7)
hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java (2)
56-63
: Consider supporting custom tags in lookup.The current implementation only matches exact "imageName:defaultTag" combinations. Consider enhancing the method to parse custom tags.
public static DatabaseImagesEnum fromFullImageName(String fullImageName) { + // Split the image name and tag + String[] parts = fullImageName.split(":"); + if (parts.length > 0) { + // Match by image name only, ignoring tag differences + return fromImageName(parts[0]); + } for (DatabaseImagesEnum value : values()) { if (value.getFullImageName().equalsIgnoreCase(fullImageName)) { return value; } } throw new IllegalArgumentException("Unknown full database image name: " + fullImageName); }
65-71
: Fix class name in toString method.The toString method references an incorrect class name "DatabaseImageNameEnum" which doesn't match the actual class name "DatabaseImagesEnum".
@Override public String toString() { - return "DatabaseImageNameEnum{" + + return "DatabaseImagesEnum{" + "imageName='" + imageName + '\'' + ", defaultTag='" + defaultTag + '\'' + '}'; }hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java (2)
386-387
: Good implementation of Testcontainers JDBC URL support.The addition of the "testcontainers" case to the URL construction switch statement enables integration with Testcontainers for end-to-end testing. However, hard-coding credentials limits flexibility.
Consider parameterizing the credentials rather than hard-coding them:
-case "testcontainers" -> "jdbc:tc:" + host + ":" + port - + ":///" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + "?user=root&password=root"; +case "testcontainers" -> "jdbc:tc:" + host + ":" + port + + ":///" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + + "?user=" + (StringUtils.hasText(jdbcProtocol.getUsername()) ? jdbcProtocol.getUsername() : "root") + + "&password=" + (StringUtils.hasText(jdbcProtocol.getPassword()) ? jdbcProtocol.getPassword() : "root");
386-387
: Add explanatory comment for the Testcontainers case.Adding a comment explaining the purpose and usage of the Testcontainers case would improve maintainability.
+// Special case for test containers JDBC URL format (jdbc:tc:postgresql:15:///test?user=root&password=root) case "testcontainers" -> "jdbc:tc:" + host + ":" + port + ":///" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + "?user=root&password=root";
hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java (3)
48-53
: Consider adding a comment explaining the database image selectionThe code references a
DatabaseImagesEnum
class which appears to be used for selecting test container images. I recommend adding a brief comment explaining the purpose of this enum and why PostgreSQL was chosen as the default test database.
86-94
: Document why 'slow_sql' metric requires special handlingThe code has special handling for the "slow_sql" metric, only verifying that it doesn't throw an exception. Please add a comment explaining why this metric needs different validation logic than other metrics.
if ("slow_sql".equals(metricName)) { + // The slow_sql metric requires special handling because it depends on database-specific features + // that may not be available in the test container or requires additional setup. + // For E2E testing purposes, we only verify it doesn't throw exceptions. Metrics finalMetricsDef = metricsDef; assertDoesNotThrow(() -> collectMetrics(finalMetricsDef), String.format("%s failed to collect metrics)", metricName)); log.info("{} metrics validation passed", metricName); continue; // skip slow_sql, extra extensions }
54-69
: Consider adding more specific assertions for collection resultsThe test effectively sets up the collection environment, but could benefit from more specific assertions to validate the collected data, not just that collection occurred without errors.
For example, you could enhance the test by validating specific fields or values in the collected metrics:
// Example of more specific validation CollectRep.MetricsData metricsData = validateMetricsCollection(metricsDef, metricName, true); // Verify specific metrics were collected if ("database_size".equals(metricName)) { assertFalse(metricsData.getValuesList().isEmpty(), "Should have collected database size metrics"); // Further specific assertions based on expected values }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java
(1 hunks)hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml
(2 hunks)hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java
(1 hunks)hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java
(1 hunks)hertzbeat-e2e/pom.xml
(1 hunks)
🔇 Additional comments (8)
hertzbeat-e2e/pom.xml (1)
77-88
: Well-structured dependency management addition.Adding the Testcontainers BOM (Bill of Materials) as part of dependencyManagement is a great practice. This centralizes version management for all Testcontainers dependencies across the project, ensuring consistency and simplifying future version upgrades.
hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml (3)
34-35
: Good versioning practice.Defining database driver versions as properties follows Maven best practices, enabling centralized version management.
59-81
: Well-organized Testcontainers dependencies.The dependencies are properly organized with clear comments, correct scoping as "test", and appropriate module selection for database testing with Testcontainers.
83-97
: Appropriate JDBC driver configuration.The JDBC drivers are correctly configured with:
- Specific versions tied to properties
- Test scope to limit their inclusion to test classpath only
- Optional flag to prevent transitive inclusion in dependent projects
hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java (2)
23-26
: Clean enum design for test database types.The enum effectively defines the supported database types with appropriate image names and default tags for testing.
47-54
: Well-implemented lookup method.The lookup method correctly handles case-insensitive matching of database image names, ensuring flexibility in how tests reference image types.
hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java (2)
1-47
: Good implementation of E2E tests for JDBC collectionThe class structure and test setup with Mockito look good. The class extends AbstractCollectE2eTest which is appropriate for E2E testing.
81-85
: LGTM: Test method initialization looks goodThe test initialization correctly retrieves the job definition and sets up the collection environment.
CollectRep.MetricsData metricsData = validateMetricsCollection(metricsDef, metricName, true); | ||
CollectUtil.getConfigmapFromPreCollectData(metricsData); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
The return value of getConfigmapFromPreCollectData is not used
Line 96 calls CollectUtil.getConfigmapFromPreCollectData(metricsData)
but doesn't use the return value. Consider either storing and using the return value or adding a comment explaining why it's called without using the result.
-CollectUtil.getConfigmapFromPreCollectData(metricsData);
+// Extract configmap data for potential use in subsequent test iterations
+Map<String, Configmap> configmap = CollectUtil.getConfigmapFromPreCollectData(metricsData);
+if (!configmap.isEmpty()) {
+ configmapFromPreCollectData.add(configmap);
+}
Committable suggestion skipped: line range outside the PR's diff.
@Override | ||
protected Protocol buildProtocol(Metrics metricsDef) { | ||
JdbcProtocol jdbcProtocol = metricsDef.getJdbc(); | ||
jdbcProtocol.setHost(DATABASE_IMAGE_NAME); | ||
jdbcProtocol.setPort(DATABASE_IMAGE_TAG); | ||
jdbcProtocol.setDatabase("test"); | ||
jdbcProtocol.setPlatform("testcontainers"); | ||
return jdbcProtocol; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify if port assignment is correct
Line 75 assigns DATABASE_IMAGE_TAG
to the port
property, which seems unusual as ports are typically numeric values. Consider verifying if this is the intended behavior or if it should be using an actual port number.
🏁 Script executed:
#!/bin/bash
# Check how DATABASE_IMAGE_TAG is used and if it's appropriate for port configuration
echo "Checking DatabaseImagesEnum class for tag definition..."
fd -t f -e java "DatabaseImagesEnum.java" | xargs cat
echo "Checking JdbcProtocol class for port field type..."
fd -t f -e java "JdbcProtocol.java" | xargs cat | grep -A 10 "private.*port"
Length of output: 2986
🏁 Script executed:
#!/bin/bash
# Display the relevant test file context around the buildProtocol method
fd -t f -e java "JdbcCommonCollectE2eTest.java" | xargs sed -n '1,200p'
Length of output: 4427
🏁 Script executed:
#!/bin/bash
# Locate and display AbstractCollectE2eTest class implementation context around container setup or protocol handling
fd -t f -e java "AbstractCollectE2eTest.java" | xargs sed -n '1,200p'
Length of output: 6135
Confirm port assignment in buildProtocol
The test currently does:
jdbcProtocol.setHost(DATABASE_IMAGE_NAME);
jdbcProtocol.setPort(DATABASE_IMAGE_TAG);
Here, DATABASE_IMAGE_TAG
comes from DatabaseImagesEnum.getDefaultTag()
(e.g. "15"
), which is the Docker image version—not a TCP port. Since JdbcProtocol.port
is the port used to connect to the database, this will direct the driver to port 15 rather than the actual database port (e.g. 5432 or the Testcontainers–mapped port).
Please verify and update accordingly:
- If you’re using Testcontainers, start the container and set:
jdbcProtocol.setPort(String.valueOf(container.getFirstMappedPort()));
- Otherwise, use the default database port (e.g.
"5432"
) or inject it via configuration.
Location:
hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java
protected Protocol buildProtocol(Metrics metricsDef) { … }
What's changed?
Benefit from testcontainers' rich jdbc driver support (SPI implementation), add jdbc common collect e2e code (postgresql/mysql supported for now)
Checklist
Add or update API
Summary by CodeRabbit
New Features
Chores