Add X-Request-Start header middleware#85
Merged
kevinmcconnell merged 2 commits intobasecamp:mainfrom Jul 26, 2025
Merged
Conversation
Adds middleware that sets X-Request-Start header with millisecond timestamp for request timing measurement. Header format follows convention of t=<milliseconds_since_epoch>. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Collaborator
kevinmcconnell
left a comment
There was a problem hiding this comment.
Thanks @nateberkopec, this is great!
I did not include a config option because I felt it was unnecessary.
Agreed, yes. Much prefer just doing this automatically 👍
I've left one comment for you, but otherwise LGTM.
internal/request_start_middleware.go
Outdated
| func NewRequestStartMiddleware(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| timestamp := time.Now().UnixMilli() | ||
| r.Header.Set("X-Request-Start", fmt.Sprintf("t=%d", timestamp)) |
Collaborator
There was a problem hiding this comment.
If this header already exists in the request, I don't think we should overwrite it. For example, Kamal-deployed apps will already have an X-Request-Start because we populate it in the proxy. We'll presumably want the queue time to reflect that initial entrypoint.
Contributor
Author
There was a problem hiding this comment.
Good point, I didn't realize that was a common setup.
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Collaborator
|
Great, thanks again @nateberkopec, I'll get this out in the next release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Personally, I'm of the belief that everyone should be measuring their request queue times. To do that, we need something in front of the application stamping requests with an
X-Request-Startheader, so that our application can read that and calculate time spent queueing.This is usually done in nginx/some other reverse proxy for those using those projects.
This header and format is a common convention and used by New Relic, Datadog and others. If you use any of these APMs and incoming requests start carrying this header, they'll automatically start calculating request queue time.
I did not include a config option because I felt it was unnecessary.