Skip to content

Commit

Permalink
Merge pull request #83 from dongxuny/master
Browse files Browse the repository at this point in the history
Add GormCtx() function for rk-db traceId injection
  • Loading branch information
dongxuny authored Sep 6, 2022
2 parents 7fc2b90 + 743576d commit 293a008
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions middleware/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ func GetLogger(ctx context.Context) *zap.Logger {
return logger
}

func GormCtx(ctx context.Context) context.Context {
res := context.Background()
res = context.WithValue(res, rkmid.LoggerKey.String(), GetLogger(ctx))
res = context.WithValue(res, rkmid.EventKey.String(), GetEvent(ctx))
return res
}

// GetRequestId Get request id in outgoing metadata.
func GetRequestId(ctx context.Context) string {
// case 1: called from server side context which wrapped with WrapContextForServer()'
Expand Down
4 changes: 4 additions & 0 deletions middleware/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ func TestGetJwtToken(t *testing.T) {
assert.Equal(t, token, GetJwtToken(ctx))
}

func TestGormCtx(t *testing.T) {
assert.NotNil(t, GormCtx(context.TODO()))
}

func assertNotPanic(t *testing.T) {
if r := recover(); r != nil {
// Expect panic to be called with non nil error
Expand Down

0 comments on commit 293a008

Please sign in to comment.