Skip to content

Add traceID if possible when using context logger. #2518

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

Merged
merged 3 commits into from
Apr 24, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ func WithContext(ctx context.Context, l log.Logger) log.Logger {
// Weaveworks uses "orgs" and "orgID" to represent Cortex users,
// even though the code-base generally uses `userID` to refer to the same thing.
userID, err := user.ExtractOrgID(ctx)
if err != nil {
if err != nil && err != user.ErrNoOrgID {
return l
}
l = WithUserID(userID, l)
if err == nil {
l = WithUserID(userID, l)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we simplify it as the following, so that the trace ID is injected in any case?

if err == nil {
    l = WithUserID(userID, l)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, let's do that.


traceID, ok := middleware.ExtractTraceID(ctx)
if !ok {
Expand All @@ -127,7 +129,7 @@ func WithUserID(userID string, l log.Logger) log.Logger {
// its details.
func WithTraceID(traceID string, l log.Logger) log.Logger {
// See note in WithContext.
return log.With(l, "trace_id", traceID)
return log.With(l, "traceID", traceID)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also renaming span_id to spanID because we have inconsistency with weaveworks server which uses spanID.

Note to myself: I guess you meant traceID and not spanID.

}

// CheckFatal prints an error and exits with error code 1 if err is non-nil
Expand Down