Skip to content

feat: add requestId middleware for tracing#1401

Open
leno23 wants to merge 1 commit into
h3js:mainfrom
leno23:feat/request-id-middleware-1389
Open

feat: add requestId middleware for tracing#1401
leno23 wants to merge 1 commit into
h3js:mainfrom
leno23:feat/request-id-middleware-1389

Conversation

@leno23

@leno23 leno23 commented May 26, 2026

Copy link
Copy Markdown

Summary

  • Adds requestId() middleware to generate or propagate request IDs
  • Adds getRequestId() helper and event.context.requestId
  • Sets response header on success and error responses

Closes #1389

Usage

import { H3, requestId } from "h3";

const app = new H3();
app.use(requestId());

Test plan

  • Added tests for generation, incoming header reuse, trustIncoming, and error responses
  • pnpm vitest run test/request-id.test.ts (8/8)
  • pnpm lint

Made with Cursor

Summary by CodeRabbit

  • New Features
    • Added request ID middleware for automatic generation and propagation to response and error headers
    • Configurable header names, custom ID generators, and incoming header trust settings for flexible request tracking

Review Change Stack

Closes h3js#1389

Co-authored-by: Cursor <cursoragent@cursor.com>
@leno23 leno23 requested a review from pi0 as a code owner May 26, 2026 13:48
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown

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: 2810dfa1-00c2-48d6-bae2-fa296fd2b8c2

📥 Commits

Reviewing files that changed from the base of the PR and between 84244b4 and 8828efe.

📒 Files selected for processing (4)
  • src/index.ts
  • src/types/context.ts
  • src/utils/request-id.ts
  • test/request-id.test.ts

📝 Walkthrough

Walkthrough

This PR adds built-in request ID generation and propagation middleware to h3. It introduces configurable ID generation with optional header name and custom generator support, automatic propagation to response and error headers, and integration with the event context for request tracing.

Changes

Request ID feature implementation

Layer / File(s) Summary
Request ID types and core middleware
src/types/context.ts, src/utils/request-id.ts
H3EventContext is extended with an optional requestId field. RequestIdOptions interface defines configuration for header name (default x-request-id), custom ID generator (default crypto.randomUUID()), and whether to trust incoming request IDs (default true). getRequestId() reads the stored ID from event context. requestId() middleware resolves an ID from the incoming header when trusted, otherwise generates one; stores it in context; and propagates to both response headers and error headers.
Public API exports and test validation
src/index.ts, test/request-id.test.ts
RequestIdOptions, getRequestId, and requestId are exported in the public barrel. Tests verify x-request-id generation and propagation on success responses, reuse of incoming request IDs, generation of new IDs when trustIncoming is disabled, and propagation on error responses.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • pi0

Poem

🐰 A request trace hops through the wire,
With IDs that sparkle and inspire,
From context to headers they play,
Distributed logs save the day—
h3's request tracking won't tire!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add requestId middleware for tracing' accurately summarizes the main change: introducing a new requestId middleware for request tracing, which is the primary focus of the changeset.
Linked Issues check ✅ Passed The PR fully addresses issue #1389 by providing a requestId() middleware for auto-generating request IDs, propagating them via HTTP headers, and enabling distributed tracing support.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the requestId middleware feature: new request-id utility, type updates, public exports, and comprehensive tests; no unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

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.

Enhancement: add built-in request ID generation and propagation

1 participant