Skip to content

Conversation

tzulingk
Copy link
Contributor

@tzulingk tzulingk commented Aug 20, 2025

Overview:

guard inflight_requests and request_duration from early returns.

Details:

guard inflight_requests and request_duration from early returns.

Where should the reviewer start?

lib/runtime/src/pipeline/network/ingress/push_handler.rs

Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)

DIS-496 some metrics leaks on early-returns

Summary by CodeRabbit

  • Bug Fixes

    • Request metrics for ingress pushes are now consistently tracked across all outcomes, ensuring accurate inflight counts, byte totals, and duration measurements. This improves reliability of monitoring, dashboards, and alerts.
  • Refactor

    • Streamlined metrics lifecycle management to automatically record completion and timing, reducing the risk of missed updates and inconsistencies in observability data.

@tzulingk tzulingk requested a review from a team as a code owner August 20, 2025 22:17
@github-actions github-actions bot added the fix label Aug 20, 2025
@tzulingk tzulingk enabled auto-merge (squash) August 20, 2025 22:18
Copy link
Contributor

coderabbitai bot commented Aug 20, 2025

Walkthrough

Introduces a RAII-based RequestMetricsGuard in Ingress push_handler to manage inflight gauge decrement and duration observation automatically. Metrics are incremented at request start, and the guard’s Drop handles cleanup on all exits, replacing the prior manual end-of-function metrics update.

Changes

Cohort / File(s) Summary of edits
Ingress metrics RAII guard
lib/runtime/src/pipeline/network/ingress/push_handler.rs
Added RequestMetricsGuard with Drop to observe duration and decrement inflight; imported Instant and tracked start_time; incremented metrics at entry (counter, inflight, bytes); stored guard in _inflight_guard; removed manual end-of-function metrics observation/decrement for Ingress<SingleIn, ManyOut> path.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant IngressHandler as Ingress::handle_payload
  participant Metrics as Metrics (counter/gauge/hist)
  participant Guard as RequestMetricsGuard

  Client->>IngressHandler: handle_payload(payload)
  IngressHandler->>Metrics: increment request_counter
  IngressHandler->>Metrics: inc inflight_requests
  IngressHandler->>Metrics: observe request_bytes
  IngressHandler->>Guard: create(start_time, inflight, duration)
  Note right of Guard: RAII guard ensures cleanup on any exit

  alt normal processing
    IngressHandler-->>Client: return result
  else early return / error
    IngressHandler-->>Client: return error
  end

  Note over Guard,Metrics: On drop
  Guard->>Metrics: dec inflight_requests
  Guard->>Metrics: observe request_duration(elapsed)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

A rabbit taps the gauge with glee,
“Inflight up, inflight down—automagically!”
A guard stands watch, quiet and spry,
Drops at the end, counting time as it flies.
Burrows of metrics neat and tight—
Hop along, requests, you’re measured right. 🐇⏱️

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

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 (4)
lib/runtime/src/pipeline/network/ingress/push_handler.rs (4)

110-113: Minor style consistency: use imported type names instead of fully-qualified ones.

You already import IntGauge and Histogram (Line 18). Using those here improves consistency and readability.

-    inflight_requests: prometheus::IntGauge,
-    request_duration: prometheus::Histogram,
+    inflight_requests: IntGauge,
+    request_duration: Histogram,

137-147: Prefer explicit control flow over Option::map when doing side effects.

Using map for side effects is slightly non-idiomatic and can be misread. An if let reads clearer and keeps the guard’s lifetime explicit. Functionally identical.

-        let _inflight_guard = self.metrics().map(|m| {
-            m.request_counter.inc();
-            m.inflight_requests.inc();
-            m.request_bytes.inc_by(payload.len() as u64);
-            RequestMetricsGuard {
-                inflight_requests: m.inflight_requests.clone(),
-                request_duration: m.request_duration.clone(),
-                start_time,
-            }
-        });
+        let _inflight_guard = if let Some(m) = self.metrics() {
+            m.request_counter.inc();
+            m.inflight_requests.inc();
+            m.request_bytes.inc_by(payload.len() as u64);
+            Some(RequestMetricsGuard {
+                inflight_requests: m.inflight_requests.clone(),
+                request_duration: m.request_duration.clone(),
+                start_time,
+            })
+        } else {
+            None
+        };

Optional note: if you care about avoiding the negligible Instant creation when metrics are disabled, move Instant::now() inside the if-let. It will shift the measured start by a few microseconds; probably not worth it unless you want absolute minimal overhead.


21-21: Tiny consistency nit: you import Instant but call std::time::Instant::now().

Use the imported alias for consistency, or drop the import and keep the fully-qualified path. I’d go with the alias.

Outside this selected range (Line 135), update the call site:

let start_time = Instant::now();

137-147: Add a regression test to prove the guard covers typical early-return sites.

Given the prior leaks, a focused test will prevent backslides. Table-test these cases and assert inflight returns to baseline and duration is observed at least once:

  • decode_message failure path
  • invalid TwoPartMessageType path
  • response stream creation failure path
  • generate() error path
  • publish send error path (mid-stream)

I can help scaffold a test using a test registry and a fake Ingress that forces these exits.

📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e31c879 and 9d6c04b.

📒 Files selected for processing (1)
  • lib/runtime/src/pipeline/network/ingress/push_handler.rs (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
lib/runtime/src/pipeline/network/ingress/push_handler.rs (1)
lib/llm/src/perf.rs (1)
  • start_time (125-127)
🔇 Additional comments (1)
lib/runtime/src/pipeline/network/ingress/push_handler.rs (1)

108-121: RAII guard cleanly fixes the metrics leak on early returns — nice.

Creating RequestMetricsGuard and letting Drop handle inflight dec + duration observe guarantees correctness across all exit paths, including errors and early returns. This aligns perfectly with the PR goal (DIS-496).

Copy link
Contributor

@nnshah1 nnshah1 left a comment

Choose a reason for hiding this comment

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

nice!

Copy link
Contributor

@keivenchang keivenchang left a comment

Choose a reason for hiding this comment

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

LGTM, after adding explicit drop per Graham -> +1

@tzulingk tzulingk merged commit 105436c into main Aug 21, 2025
13 of 14 checks passed
@tzulingk tzulingk deleted the tzulingk/guard_dis_496 branch August 21, 2025 22:31
hhzhang16 pushed a commit that referenced this pull request Aug 27, 2025
KrishnanPrash pushed a commit that referenced this pull request Sep 2, 2025
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.

4 participants