fix: add FromStdContext to retrieve request ID from context.Context#4068
fix: add FromStdContext to retrieve request ID from context.Context#4068veeceey wants to merge 1 commit intogofiber:mainfrom
Conversation
The requestid middleware only exposed FromContext(fiber.Ctx) which made it impossible to retrieve the request ID further down the call stack when only a standard context.Context was available (e.g. via c.Context() or c.RequestCtx()). This stores the request ID in the user context via context.WithValue and adds FromStdContext(context.Context) so service layers that only receive a context.Context can access the request ID. Fixes gofiber#4066
Summary of ChangesHello @veeceey, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the usability of the request ID middleware by integrating it more deeply with Go's standard Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughThe changes add support for retrieving request IDs from standard context.Context by introducing a new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces a valuable enhancement to the requestid middleware by adding the FromStdContext function. This allows for retrieving the request ID from a standard context.Context, which is particularly useful for propagating the ID into service layers that are decoupled from Fiber's specific context. The implementation correctly stores the request ID using context.WithValue and is accompanied by thorough tests covering various scenarios, including empty and wrapped contexts. The changes are well-executed and improve the middleware's utility.
| ctx := context.WithValue(c.Context(), requestIDKey, rid) | ||
| c.SetContext(ctx) |
|
@veeceey Please discuss in the issue first, we are still investigation internally. |
Summary
FromStdContext(ctx context.Context) stringfunction to the requestid middleware, allowing retrieval of the request ID from any standardcontext.Contextcontext.WithValue) in addition to Fiber locals, so it propagates throughc.Context()and wrapped contextsFromStdContextincluding empty context and wrapped context scenariosFixes #4066
Test plan
Test_RequestID_FromStdContextverifies retrieval viac.Context()Test_RequestID_FromStdContext_Emptyverifies empty string on plaincontext.Background()Test_RequestID_FromStdContext_WrappedContextverifies retrieval still works when the context is further wrapped withcontext.WithValueclient/Test_Client_SetProxyURL/errorfailure unrelated to this change)Usage example
```go
import "github.com/gofiber/fiber/v3/middleware/requestid"
func service(ctx context.Context) {
rid := requestid.FromStdContext(ctx) // works!
fmt.Println("Request ID:", rid)
}
app.Get("/test", func(c fiber.Ctx) error {
service(c.Context()) // pass standard context.Context
return c.SendStatus(200)
})
```