Skip to content

Conversation

Teebor-Choka
Copy link
Contributor

@Teebor-Choka Teebor-Choka commented Jun 3, 2025

This pull request refactors the hoprd/rest-api module to reorganize middleware and metrics-related functionality. The changes primarily involve moving Prometheus metrics handling from the node module to a new root module, restructuring middleware imports, and updating related API routes.

Middleware Refactoring and Organization:

Metrics Handling Relocation:

  • hoprd/rest-api/src/node.rs: Removed the metrics function and associated logic for Prometheus metrics from the node module.

  • hoprd/rest-api/src/root.rs: Added a new metrics function to the root module, replicating the Prometheus metrics functionality previously in the node module. This change includes updated API route definitions and error handling.

API Route Updates:

Notes

Closes #7143

@Teebor-Choka Teebor-Choka self-assigned this Jun 3, 2025
Copy link
Contributor

coderabbitai bot commented Jun 3, 2025

📝 Walkthrough

Walkthrough

The changes refactor the REST API by introducing a new middleware module and a new root module. The /metrics endpoint handler is moved from the node module to the root module, with updated OpenAPI documentation. Middleware application is revised to use the new module structure and explicit Axum middleware functions.

Changes

File(s) Change Summary
hoprd/rest-api/src/lib.rs Refactored API router to use new middleware and root modules; updated /metrics route handler and middleware.
hoprd/rest-api/src/middleware/mod.rs Added new internal modules: preconditions and prometheus.
hoprd/rest-api/src/node.rs Removed all /metrics endpoint logic and related OpenAPI documentation.
hoprd/rest-api/src/root.rs Added new /metrics endpoint logic and OpenAPI documentation; feature-gated metrics collection.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API_Router
    participant Middleware
    participant RootModule

    Client->>API_Router: GET /metrics
    API_Router->>Middleware: Authenticate, Cap Websockets, Record
    Middleware->>RootModule: metrics()
    RootModule->>RootModule: collect_hopr_metrics()
    RootModule-->>Middleware: Metrics String or Error
    Middleware-->>API_Router: Response
    API_Router-->>Client: 200 OK (metrics) or 422 (error)
Loading

Assessment against linked issues

Objective Addressed Explanation
Expose /metrics endpoint at root and document in OpenAPI (#7143)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Suggested labels

bugfix

Suggested reviewers

  • Teebor-Choka

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7fc46da and 3f42c81.

📒 Files selected for processing (2)
  • hoprd/rest-api/src/lib.rs (7 hunks)
  • hoprd/rest-api/src/root.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • hoprd/rest-api/src/root.rs
  • hoprd/rest-api/src/lib.rs
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: zizmor
  • GitHub Check: Linter
  • GitHub Check: hoprd / docker
  • GitHub Check: hopli / docker
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Teebor-Choka Teebor-Choka marked this pull request as ready for review June 3, 2025 14:34
@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 14:34
Copy link
Contributor

@Copilot 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

Refactor and relocate the Prometheus metrics endpoint while reorganizing middleware to improve modularity and update API route definitions.

  • Metrics endpoint moved from the node module to a new root module.
  • Middleware reorganized into preconditions and prometheus submodules.
  • API routes updated to reflect the new module structure.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

File Description
hoprd/rest-api/src/root.rs Added new metrics endpoint with Prometheus support in the root module.
hoprd/rest-api/src/node.rs Removed the metrics function and its associated logic.
hoprd/rest-api/src/middleware/mod.rs Created preconditions and prometheus submodules for middleware.
hoprd/rest-api/src/lib.rs Updated API routes and middleware wiring to use the new modules.

@Teebor-Choka Teebor-Choka added this to the 3.0.0 milestone Jun 3, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
hoprd/rest-api/src/root.rs (1)

32-37: Review HTTP status code usage for error handling.

The current implementation returns HTTP 422 (Unprocessable Entity) for metrics collection failures. Consider if HTTP 503 (Service Unavailable) might be more appropriate when metrics collection fails due to system issues rather than client input problems.

-        Err(error) => (StatusCode::UNPROCESSABLE_ENTITY, error).into_response(),
+        Err(error) => (StatusCode::SERVICE_UNAVAILABLE, error).into_response(),
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e5cb68 and 7fc46da.

📒 Files selected for processing (4)
  • hoprd/rest-api/src/lib.rs (6 hunks)
  • hoprd/rest-api/src/middleware/mod.rs (1 hunks)
  • hoprd/rest-api/src/node.rs (0 hunks)
  • hoprd/rest-api/src/root.rs (1 hunks)
💤 Files with no reviewable changes (1)
  • hoprd/rest-api/src/node.rs
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Docs / Rust docs
  • GitHub Check: Build Candidate Binaries (hopli)
  • GitHub Check: tests-unit-nightly
  • GitHub Check: tests-unit
  • GitHub Check: Cargo Audit
  • GitHub Check: Linter
  • GitHub Check: zizmor
🔇 Additional comments (9)
hoprd/rest-api/src/middleware/mod.rs (1)

1-2: LGTM! Clean module organization.

The module declarations follow Rust conventions with appropriate pub(crate) visibility for internal middleware components.

hoprd/rest-api/src/root.rs (3)

5-9: LGTM! Proper feature-gated implementation.

The conditional compilation correctly handles the prometheus feature case with appropriate error mapping.


11-14: LGTM! Consistent fallback implementation.

The fallback implementation properly handles the case when prometheus feature is disabled or during tests.


16-31: OpenAPI documentation looks comprehensive.

The endpoint documentation includes proper response codes, security requirements, and descriptions. The tag assignment to "Node" category maintains consistency with other node-related endpoints.

hoprd/rest-api/src/lib.rs (5)

7-7: LGTM! Clean module organization.

The addition of middleware and root modules aligns well with the refactoring objectives to improve code organization.

Also applies to: 11-11


107-107: Consistent OpenAPI documentation update.

The path update from node::metrics to root::metrics correctly reflects the code reorganization.


253-253: Route endpoint correctly updated.

The metrics route now properly references the new root::metrics handler.


254-261: Improved middleware application pattern.

The refactoring to use axum::middleware::from_fn_with_state provides better type safety and clearer middleware composition compared to direct function usage. This follows Axum best practices for middleware application.

Also applies to: 317-324


272-272: Consistent middleware pattern across routes.

The axum::middleware::from_fn usage for the prometheus recording middleware maintains consistency with the other middleware applications and follows Axum conventions.

Also applies to: 335-335

@Teebor-Choka Teebor-Choka requested a review from a team June 3, 2025 19:05
@Teebor-Choka Teebor-Choka changed the title api: Fix missing endpoint in openapi api: Fix missing endpoint in OpenAPI Jun 3, 2025
@Teebor-Choka Teebor-Choka merged commit 35d039f into master Jun 4, 2025
54 of 57 checks passed
@Teebor-Choka Teebor-Choka deleted the kauki/api/fix-missing-api-endpoints branch June 4, 2025 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing API endpoints in swagger/scalar
2 participants