Skip to content
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

Merged
merged 1 commit into from
Dec 13, 2017
Merged

Request-ID Tracing #118

merged 1 commit into from
Dec 13, 2017

Conversation

BugRoger
Copy link
Contributor

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.

Copy link
Member

@databus23 databus23 left a 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

)

const (
KubernikusRequestID = "Kubernikus-Request-Id"
Copy link
Member

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. ...

Copy link
Member

@databus23 databus23 Dec 13, 2017

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.

Copy link
Contributor Author

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()))
Copy link
Member

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. ;)

Copy link
Contributor Author

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 ;)

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)))

Copy link
Member

@databus23 databus23 Dec 13, 2017

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).

Copy link
Contributor Author

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.

@BugRoger
Copy link
Contributor Author

rebased and squashed

@BugRoger BugRoger merged commit 53fac3a into master Dec 13, 2017
@BugRoger BugRoger deleted the tracing branch December 13, 2017 10:28
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