-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to thread matched route key on request context #286
Add option to thread matched route key on request context #286
Conversation
2b8357f
to
34e411a
Compare
Thank you for this PR. I understand the need for it and we'll somehow find a way to integrate this without a big performance penalty / memory overhead for the common case. As a first thought: The full path only needs to be stored in nodes that have a handler.
I currently don't have much time for it, but I hope to get back to this soon. Feel free to do some further experiments in the mean time. |
The minimum constraints for adding this feature are from my point of view:
CC @jehiah |
var MatchedRouteKey = matchKey{} | ||
|
||
// MatchedRouteFromContext retrieves the matched route path from the context. | ||
func MatchedRouteFromContext(ctx context.Context) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it's even consistent in the existing code, but generally a route should be a tuple (Method, Path) in the context of this router. This is just the matched path.
I just had an idea... take a look at the Overhead for the common case: 0 Doesn't that already fulfill what you're looking for? |
Here is a quick-and-dirty implementation of the alternative using It indeed brings to cost down to almost 0, except for matched routes that don't contain params, where |
@julienschmidt adding the value into |
The params approach is now in master. Please test and provide feedback. |
Thank you @julienschmidt and @jehiah ! This great news. Sorry for the slow reply. |
👋 first off, thank you for such a great router!
I have a usecase where it would be really great (in terms of visibility) when handling a request to retrieve which router match occurred and called my handler. In my instance I want to create a middleware which can automatically enrich traces with this router path.
This is my little attempt to surface that information. There is probably a cleaner way than my approach.
Also here are the benchmarks:
OK I just saw #139 so perhaps this change is a little undesireable or just unlikely to go through. But I am going to try anyway.
This at least offers an interface which is backwards compatible with the existing one.