Skip to content

Conversation

hhk7734
Copy link

@hhk7734 hhk7734 commented Sep 16, 2025

I use middleware to handle x-request-id for all endpoints. If an x-request-id already exists in the request, it does not generate a new one.

Signed-off-by: Hyeonki Hong <hyeonki.hong@moreh.io>
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get(requestHeaderRequestID)
if requestID == "" {
requestID = uuid.New().String()
Copy link
Collaborator

Choose a reason for hiding this comment

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

New can panic, can we use the NewRandom() version and handle the error accordingly without panics?

Copy link
Author

Choose a reason for hiding this comment

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

325c27b
Updated it as you suggested.

Signed-off-by: Hyeonki Hong <hyeonki.hong@moreh.io>
Comment on lines +296 to +297

w.Header().Set(requestHeaderRequestID, requestID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
w.Header().Set(requestHeaderRequestID, requestID)

Copy link
Collaborator

Choose a reason for hiding this comment

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

if it's unset we set it at line 294

Copy link
Author

Choose a reason for hiding this comment

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

Line 294 is the request and line 297 is the response. Setting the X-Request-Id on the response makes it easier to find the corresponding traces or logs when encountering 500 or similar errors.

What do you think about keeping it instead of deleting it?

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.

2 participants