-
Notifications
You must be signed in to change notification settings - Fork 25
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
Request-ID Tracing #118
Request-ID Tracing #118
Conversation
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.
Two pedantic nits, otherwise LGTM
pkg/util/log/middleware.go
Outdated
) | ||
|
||
const ( | ||
KubernikusRequestID = "Kubernikus-Request-Id" |
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.
Quoting the golang docs for WithValue
:
The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context. Users of WithValue should define their own types for keys. ...
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.
The original blog post about Context
gives an example on how they suggest this should be used: https://blog.golang.org/context#TOC_3.2.
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.
Thanks, good tip. Much better...
func RequestIDHandler(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) { | ||
if id := request.Context().Value(KubernikusRequestID); id == nil { | ||
request = request.WithContext(context.WithValue(request.Context(), KubernikusRequestID, uuid.NewV4())) |
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.
For my taste there we should have the world context
in this line more often. ;)
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 resisted the temptation to make this wish come true ;)
pkg/util/log/middleware.go
Outdated
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) { | ||
wrapper := makeWrapper(rw) | ||
|
||
id := fmt.Sprintf("%s", request.Context().Value(KubernikusRequestID)) | ||
request = request.WithContext(context.WithValue(request.Context(), "logger", kitlog.With(logger, "id", id))) | ||
|
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 know it annoying to handle this but we should handle the case were the there is no kubernikus request id in the context (because the requestid middleware is disabled).
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 omitting the id
key now if the context does not contain the requestId.
rebased and squashed |
This adds a middleware that generates a UUID on each incoming API request. For a poor-man's tracing this
id
is passed as value in a request specific logger.