Skip to content

fix: re-use x-request-id if sent in request#992

Merged
Datron merged 1 commit intomainfrom
reuse-request-id
May 8, 2026
Merged

fix: re-use x-request-id if sent in request#992
Datron merged 1 commit intomainfrom
reuse-request-id

Conversation

@Datron
Copy link
Copy Markdown
Collaborator

@Datron Datron commented May 8, 2026

Problem

Superposition is ignoring x-request-id from proxy

Solution

Read the header and use in logs, if missing then generate a request ID and use that

Summary by CodeRabbit

  • Refactor
    • Improved request ID extraction in logging middleware by prioritizing header values with enhanced fallback handling to ensure consistent request tracking across services.
    • Enhanced root span tracing to conditionally populate request ID information when available from incoming request headers, improving request correlation and debugging capabilities in distributed tracing.

Signed-off-by: datron <Datron@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 8, 2026 07:11
@Datron Datron requested a review from a team as a code owner May 8, 2026 07:11
@semanticdiff-com
Copy link
Copy Markdown

semanticdiff-com Bot commented May 8, 2026

Review changes with  SemanticDiff

Changed Files
File Status
  crates/service_utils/src/middlewares/request_response_logging.rs  48% smaller
  crates/superposition/src/log_span.rs  29% smaller

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 49ce8d98-e550-4730-bdfa-f38385d3f954

📥 Commits

Reviewing files that changed from the base of the PR and between 5e25e4a and cc5494f.

📒 Files selected for processing (2)
  • crates/service_utils/src/middlewares/request_response_logging.rs
  • crates/superposition/src/log_span.rs

Walkthrough

This PR updates request ID handling to prioritize the x-request-id header from incoming requests. The logging middleware now extracts the header first and falls back to the tracing extension, simplifying response header insertion. The tracing span builder conditionally includes the request ID in the root span when present.

Changes

Request ID Header-First Resolution

Layer / File(s) Summary
Request ID Extraction Priority
crates/service_utils/src/middlewares/request_response_logging.rs
Request ID resolution checks the x-request-id request header first before falling back to tracing_actix_web::RequestId from request extensions.
Response Header Insertion
crates/service_utils/src/middlewares/request_response_logging.rs
The conditional for inserting x-request-id into the response was simplified from pattern-matching Option<Result<...>> to directly matching on Option<HeaderValue>.
Tracing Span Construction
crates/superposition/src/log_span.rs
The root span builder extracts x-request-id from request headers and conditionally includes request_id as a structured span field when present; otherwise, the span is created without that field.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • juspay/superposition#842: Both PRs modify request/response logging to source the request ID from the incoming x-request-id header and propagate it into tracing/root spans and response headers.

Suggested reviewers

  • sauraww
  • ayushjain17

Poem

🐰 The request IDs now dance with care,
preferring headers floating in the air!
When headers vanish, extensions take the stage—
and roots span brightly on the tracing page. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: re-use x-request-id if sent in request' directly and clearly describes the main change across both modified files: extracting and reusing the x-request-id from incoming request headers instead of always generating a new one.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch reuse-request-id

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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

This PR updates request correlation handling so that, when an upstream proxy supplies x-request-id, Superposition reuses it for observability and propagation instead of always relying on an internally generated request ID.

Changes:

  • Add x-request-id extraction to the custom Actix root span builder so logs can include the inbound request ID.
  • Update the request/response logging middleware to prefer the inbound x-request-id header and fall back to the generated tracing_actix_web::RequestId, then set x-request-id on the response.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/superposition/src/log_span.rs Includes inbound x-request-id in the tracing root span when present.
crates/service_utils/src/middlewares/request_response_logging.rs Prefers inbound x-request-id (fallback to generated) and returns it on the response header.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +42 to +47
let request_id = header_extractor(headers, "x-request-id");
let method = request.method().to_string();
tracing_actix_web::root_span!(request, workspace, org, method, path,)
if let Some(request_id) = request_id {
tracing_actix_web::root_span!(request, request_id = %request_id, workspace, org, method, path,)
} else {
tracing_actix_web::root_span!(request, workspace, org, method, path,)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good one - should we move the sanitization ahead - so that what we log and what we use / send back are the same ?

Comment on lines +136 to +143
// Check for x-request-id header first, fallback to generated one from extensions
let request_id = req.headers().get("x-request-id").cloned().or_else(|| {
req.extensions()
.get::<tracing_actix_web::RequestId>()
.and_then(|req_id| {
header::HeaderValue::from_str(&req_id.to_string()).ok()
})
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The header_extractor enforces a 256 char limit which will address this as well - so moving the header_extraction here will solve both issues I believe.

@Datron Datron added this pull request to the merge queue May 8, 2026
Merged via the queue into main with commit 4f57a97 May 8, 2026
24 of 25 checks passed
@Datron Datron deleted the reuse-request-id branch May 8, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants