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

Fix request id key #197

Merged
merged 4 commits into from Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions logging/interceptor_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"google.golang.org/grpc/metadata"

"github.com/infobloxopen/atlas-app-toolkit/logging/mocks"
"github.com/infobloxopen/atlas-app-toolkit/requestid"
)

const (
Expand All @@ -38,7 +39,7 @@ var (
buf bytes.Buffer
reader io.Reader
testLogger = New("Info")
testMD = metautils.NiceMD{}.Set(testAuthorizationHeader, testJWT).Set(DefaultRequestIDKey, testRequestID).Set(testCustomHeaderKey, testCustomHeaderVal)
testMD = metautils.NiceMD{}.Set(testAuthorizationHeader, testJWT).Set(requestid.DefaultRequestIDKey, testRequestID).Set(testCustomHeaderKey, testCustomHeaderVal)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a case that looks for DeprecatedRequestIDKey so we can verify backwards compatibility.

Copy link
Author

Choose a reason for hiding this comment

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

Good point, did it

testSubject = map[string]interface{}{"id": "testID", "subject_type": "testUser", "authentication_type": "test"}
)

Expand Down Expand Up @@ -68,7 +69,11 @@ func TestUnaryClientInterceptor(t *testing.T) {
newMD, ok := metadata.FromIncomingContext(ctx)
assert.True(t, ok)
assert.Equal(t, testJWT, newMD.Get(testAuthorizationHeader)[0])
assert.Equal(t, testRequestID, newMD.Get(DefaultRequestIDKey)[0])

reqID, ok := requestid.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, testRequestID, reqID)

assert.Equal(t, testMethod, method)

return nil
Expand Down Expand Up @@ -126,7 +131,11 @@ func TestStreamClientInterceptor(t *testing.T) {
newMD, ok := metadata.FromIncomingContext(ctx)
assert.True(t, ok)
assert.Equal(t, testJWT, newMD.Get(testAuthorizationHeader)[0])
assert.Equal(t, testRequestID, newMD.Get(DefaultRequestIDKey)[0])

reqID, ok := requestid.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, testRequestID, reqID)

assert.Equal(t, testMethod, method)

return nil, nil
Expand Down Expand Up @@ -186,7 +195,10 @@ func TestUnaryServerInterceptor(t *testing.T) {
newMD, ok := metadata.FromIncomingContext(ctx)
assert.True(t, ok)
assert.Equal(t, testJWT, newMD.Get(testAuthorizationHeader)[0])
assert.Equal(t, testRequestID, newMD.Get(DefaultRequestIDKey)[0])

reqID, ok := requestid.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, testRequestID, reqID)

entry := ctxlogrus.Extract(ctx)
assert.Equal(t, testRequestID, entry.Data[DefaultRequestIDKey])
Expand Down Expand Up @@ -251,7 +263,10 @@ func TestStreamServerInterceptor(t *testing.T) {
newMD, ok := metadata.FromIncomingContext(stream.Context())
assert.True(t, ok)
assert.Equal(t, testJWT, newMD.Get(testAuthorizationHeader)[0])
assert.Equal(t, testRequestID, newMD.Get(DefaultRequestIDKey)[0])

reqID, ok := requestid.FromContext(stream.Context())
assert.True(t, ok)
assert.Equal(t, testRequestID, reqID)

entry := ctxlogrus.Extract(stream.Context())
assert.Equal(t, testRequestID, entry.Data[DefaultRequestIDKey])
Expand Down
4 changes: 3 additions & 1 deletion query/collection_operators.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions requestid/interceptor.go
Expand Up @@ -18,6 +18,7 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (res interface{}, err error) {

reqID := HandleRequestID(ctx)

// add request id to logger
addRequestIDToLogger(ctx, reqID)

Expand All @@ -36,6 +37,7 @@ func StreamServerInterceptor() grpc.StreamServerInterceptor {
ctx := stream.Context()

reqID := HandleRequestID(ctx)

// add request id to logger
addRequestIDToLogger(ctx, reqID)

Expand Down
5 changes: 3 additions & 2 deletions requestid/requestid.go
Expand Up @@ -14,7 +14,8 @@ import (
// DefaultRequestIDKey is the metadata key name for request ID
const (
DeprecatedRequestIDKey = "Request-Id"
DefaultRequestIDKey = "request_id"
DefaultRequestIDKey = "X-Request-ID"
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@kd7lxl, please avoid references to data from private repos in public repositories

Copy link
Author

Choose a reason for hiding this comment

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

@kd7lxl We probably can forward it, it a good point

Copy link
Contributor

Choose a reason for hiding this comment

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

RequestIDLogKey = "request_id"
)

// HandleRequestID either extracts a existing and valid request ID from the context or generates a new one
Expand Down Expand Up @@ -51,5 +52,5 @@ func NewContext(ctx context.Context, reqID string) context.Context {
}

func addRequestIDToLogger(ctx context.Context, reqID string) {
ctxlogrus.AddFields(ctx, logrus.Fields{DefaultRequestIDKey: reqID})
ctxlogrus.AddFields(ctx, logrus.Fields{RequestIDLogKey: reqID})
}