feat(staking-cli): add OpenMetrics support and fix flaky tests#3926
feat(staking-cli): add OpenMetrics support and fix flaky tests#3926
Conversation
Add support for fetching validator metadata from sequencer node's /status/metrics endpoint (OpenMetrics format) as an alternative to hosting a separate JSON metadata file. Changes: - Add --node-url CLI option for fetching metadata from node metrics - Add OpenMetrics parsing (prometheus-parse crate) - Auto-detect JSON vs OpenMetrics format via Content-Type header - Add serve_on_random_port() helper to fix flaky tests caused by race conditions with portpicker The OpenMetrics parsing code is copied from staking-ui-service and marked with TODO comments for future consolidation into a shared crate.
|
/gemini review |
Summary of ChangesHello @sveitser, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for fetching validator metadata from an OpenMetrics endpoint, which is a great alternative to hosting a separate JSON file. The implementation is solid, with auto-detection of the metadata format based on the Content-Type header. The addition of the serve_on_random_port test helper is a significant improvement for test reliability, effectively addressing flaky tests caused by port allocation race conditions. The code is well-structured, and the new functionality is thoroughly tested with unit and integration tests, including a parameterized test suite for both JSON and OpenMetrics formats. I've included a couple of suggestions to make parts of the new OpenMetrics parsing code more idiomatic and maintainable. Overall, this is an excellent contribution.
| if let Some(v) = sample.labels.get("name") { | ||
| if !v.is_empty() { | ||
| name = Some(v.to_string()); | ||
| } | ||
| } | ||
| if let Some(v) = sample.labels.get("description") { | ||
| if !v.is_empty() { | ||
| description = Some(v.to_string()); | ||
| } | ||
| } | ||
| if let Some(v) = sample.labels.get("company_name") { | ||
| if !v.is_empty() { | ||
| company_name = Some(v.to_string()); | ||
| } | ||
| } | ||
| if let Some(v) = sample.labels.get("company_website") { | ||
| if !v.is_empty() { | ||
| company_website = Url::parse(v).ok(); | ||
| } | ||
| } |
There was a problem hiding this comment.
The logic for extracting optional labels is repeated for multiple fields. This can be made more concise and idiomatic by using Option::filter to check for non-empty values. This improves readability and reduces code duplication.
if let Some(v) = sample.labels.get("name").filter(|v| !v.is_empty()) {
name = Some(v.to_string());
}
if let Some(v) = sample.labels.get("description").filter(|v| !v.is_empty()) {
description = Some(v.to_string());
}
if let Some(v) = sample.labels.get("company_name").filter(|v| !v.is_empty()) {
company_name = Some(v.to_string());
}
if let Some(v) = sample.labels.get("company_website").filter(|v| !v.is_empty()) {
company_website = Url::parse(v).ok();
}| for (label, value) in sample.labels.iter() { | ||
| if !value.is_empty() { | ||
| icon_urls.insert(label.to_string(), value.to_string()); | ||
| } | ||
| } |
There was a problem hiding this comment.
The loop for collecting icon URLs can be expressed more concisely and idiomatically using iterators and the extend method. This improves readability and aligns better with functional programming patterns in Rust.
icon_urls.extend(
sample.labels
.iter()
.filter(|(_, value)| !value.is_empty())
.map(|(label, value)| (label.to_string(), value.to_string())),
);
crates/cliquenet/Cargo.toml
Outdated
| bs58 = "0.5.1" | ||
| bytes = { workspace = true } | ||
| ed25519-compact = "2.2.0" | ||
| # optional: |
There was a problem hiding this comment.
Code Review
This pull request introduces support for fetching validator metadata from an OpenMetrics endpoint, which is a great alternative to hosting a separate JSON file. The implementation is solid, with auto-detection of the metadata format based on the Content-Type header and good test coverage for the new functionality. I particularly appreciate the addition of serve_on_random_port to fix flaky tests by avoiding race conditions with portpicker; this is a robust solution. The code duplication from staking-ui-service is clearly marked with TODOs for future consolidation, which is a good practice. Overall, this is a high-quality contribution that improves both functionality and test stability.
| } | ||
| if let Some(v) = sample.labels.get("company_website") { | ||
| if !v.is_empty() { | ||
| company_website = Url::parse(v).ok(); |
There was a problem hiding this comment.
For consistency with how icon URLs are handled, it would be beneficial to also log a warning when a company_website URL is malformed. This would help diagnose configuration issues. Currently, an invalid URL is silently ignored.
company_website = Url::parse(v)
.inspect_err(|e| {
tracing::warn!("skipping malformed company_website URL: {e}");
})
.ok();|
Created new PR to fix cargo audit. PR: #3924 Please merge that PR first to fix cargo-audit. |
jbearer
left a comment
There was a problem hiding this comment.
Functionality looks good, but I don't understand the need for some of the added complexity with the additional --node-url option and the MetadataSource enum.
|
|
||
| /// URL of the node's API. Metadata will be fetched from /status/metrics. | ||
| #[clap(long, env = "NODE_URL")] | ||
| pub node_url: Option<String>, |
There was a problem hiding this comment.
Not sure I understand the point of this option. Whenever I would do --node-url URL couldn't I just as easily do --metadata-uri URL/status/metrics?
| match self { | ||
| MetadataSource::Uri(url) => Some(url.clone()), | ||
| MetadataSource::NodeUrl(url) => match url.join("status/metrics") { | ||
| Ok(metrics_url) => Some(metrics_url), | ||
| Err(e) => { | ||
| tracing::warn!("failed to construct metrics URL from {url}: {e}"); | ||
| None | ||
| }, | ||
| }, | ||
| MetadataSource::None => None, | ||
| } | ||
| } |
There was a problem hiding this comment.
Nit: this seems to duplicate a lot of the logic from just above. Perhaps could be implemented as self.metadata_uri().inspect_err(|e| tracing::warn!("failed to construct metrics URL from {url}: {e})).ok()
Add support for fetching validator metadata from sequencer node's /status/metrics endpoint (OpenMetrics format) as an alternative to hosting a separate JSON metadata file.
Changes:
The OpenMetrics parsing code is copied from staking-ui-service and marked with TODO comments for future consolidation into a shared crate.
Manually tested the ignored tests against our mainnet node.