Skip to content

Conversation

NumberFour8
Copy link
Contributor

@NumberFour8 NumberFour8 commented Nov 20, 2024

Since all metrics are lazy_static!, it means they are not primed (set to 0) immediately, but at a point they are first accessed.

This means, that they do not appear in the node/metrics endpoint until they were accessed for the first time.

This PR forcibly initializes the lazy static metrics everywhere, where they might not be used early.

Since all metrics are `lazy_static!`, it means thay are not primed (set to 0) immediately, but at a point they are first accessed.

This means, that they do not appear in the `node/metrics` endpoint until they were accessed for the first time.

This PR forcibly initializes the lazy static metrics everywhere, where they might not be used early.
@NumberFour8 NumberFour8 added this to the 2.2.0-rc.1 milestone Nov 20, 2024
@NumberFour8 NumberFour8 requested a review from a team November 20, 2024 20:35
@NumberFour8 NumberFour8 self-assigned this Nov 20, 2024
Copy link
Contributor

coderabbitai bot commented Nov 20, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request includes updates to the Cargo.toml file for the hopr-transport-protocol package, specifically incrementing the version from "0.3.0" to "0.3.1". Additionally, modifications were made to the run_msg_ack_protocol function in lib.rs, where metrics initialization and error handling enhancements were introduced, particularly when the "prometheus" feature is enabled. These changes improve the observability of the protocol's performance without altering the overall control flow or structure of the function.

Changes

File Change Summary
transport/protocol/Cargo.toml Version updated from 0.3.0 to 0.3.1. No other changes to metadata or dependencies.
transport/protocol/src/lib.rs Modified run_msg_ack_protocol to include metrics initialization when "prometheus" feature is enabled. Enhanced acknowledgment handling and error tracking with metric increments. Minor comment adjustments for clarity.

Possibly related PRs

Suggested labels

dependencies, crate:core-network, crate:hopr-crypto-packet, crate:hopr-db-api

Suggested reviewers

  • Teebor-Choka

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@NumberFour8 NumberFour8 marked this pull request as ready for review November 20, 2024 20:35
@github-actions github-actions bot added dependencies Pull requests that update a dependency file crate:core-protocol labels Nov 20, 2024
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

🧹 Outside diff range and nitpick comments (2)
transport/protocol/src/lib.rs (2)

163-175: Consider adding error handling for metric initialization

While the early initialization of metrics aligns with the PR objectives, the lazy_static::initialize calls could panic if metric registration fails (e.g., due to duplicate metric names). Consider wrapping the initialization in a tracing::warn! to gracefully handle failures in production.

 #[cfg(all(feature = "prometheus", not(test)))]
 {
     // Initialize the lazy statics here
-    lazy_static::initialize(&METRIC_RECEIVED_ACKS);
-    lazy_static::initialize(&METRIC_SENT_ACKS);
-    lazy_static::initialize(&METRIC_TICKETS_COUNT);
-    lazy_static::initialize(&METRIC_PACKET_COUNT);
-    lazy_static::initialize(&METRIC_PACKET_COUNT_PER_PEER);
-    lazy_static::initialize(&METRIC_REPLAYED_PACKET_COUNT);
-    lazy_static::initialize(&METRIC_REJECTED_TICKETS_COUNT);
-    lazy_static::initialize(&METRIC_QUEUE_SIZE);
-    lazy_static::initialize(&METRIC_MIXER_AVERAGE_DELAY);
+    for metric in [
+        &METRIC_RECEIVED_ACKS,
+        &METRIC_SENT_ACKS,
+        &METRIC_TICKETS_COUNT,
+        &METRIC_PACKET_COUNT,
+        &METRIC_PACKET_COUNT_PER_PEER,
+        &METRIC_REPLAYED_PACKET_COUNT,
+        &METRIC_REJECTED_TICKETS_COUNT,
+        &METRIC_QUEUE_SIZE,
+        &METRIC_MIXER_AVERAGE_DELAY,
+    ] {
+        if let Err(e) = std::panic::catch_unwind(|| lazy_static::initialize(metric)) {
+            tracing::warn!("Failed to initialize metric: {:?}", e);
+        }
+    }
 }

Line range hint 449-458: Consider using integer arithmetic for mixer delay calculation

The current implementation uses floating-point arithmetic for calculating the average delay, which could lead to precision issues over time. Consider using integer arithmetic or a more robust averaging algorithm.

-            let weight = 1.0f64 / cfg.metric_delay_window as f64;
-            METRIC_MIXER_AVERAGE_DELAY.set(
-                (weight * random_delay.as_millis() as f64)
-                    + ((1.0f64 - weight) * METRIC_MIXER_AVERAGE_DELAY.get()),
-            );
+            // Use exponential moving average with integer arithmetic
+            let window = cfg.metric_delay_window as u128;
+            let current_avg = METRIC_MIXER_AVERAGE_DELAY.get() as u128;
+            let new_delay = random_delay.as_millis();
+            let new_avg = ((current_avg * (window - 1) + new_delay) / window) as f64;
+            METRIC_MIXER_AVERAGE_DELAY.set(new_avg);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6aeede6 and 17ea9a3.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • transport/protocol/Cargo.toml (1 hunks)
  • transport/protocol/src/lib.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • transport/protocol/Cargo.toml
🔇 Additional comments (1)
transport/protocol/src/lib.rs (1)

163-175: LGTM! The changes effectively address the metrics initialization issue.

The implementation successfully ensures that metrics are initialized early and visible in the node/metrics endpoint from the start. The code is well-structured with proper feature isolation and comprehensive metric coverage.

@NumberFour8 NumberFour8 merged commit d7e4a5d into master Nov 21, 2024
28 checks passed
@NumberFour8 NumberFour8 deleted the lukas/prime-lazy-statics branch November 21, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate:core-protocol dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants