Skip to content

Commit

Permalink
Merge 66c02ac into 4a5a233
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmgleite authored Aug 20, 2018
2 parents 4a5a233 + 66c02ac commit b95add1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
Expand Down Expand Up @@ -502,6 +503,16 @@ func GetSessionFromCtx(ctx context.Context) *session.Session {
return ctx.Value(constants.SessionCtxKey).(*session.Session)
}

// GetDefaultLoggerFromCtx returns the default logger from the given context
func GetDefaultLoggerFromCtx(ctx context.Context) logrus.FieldLogger {
v := ctx.Value(constants.LoggerCtxKey)
if v == nil {
return logrus.New()
}

return v.(logrus.FieldLogger)
}

// AddToPropagateCtx adds a key and value that will be propagated through RPC calls
func AddToPropagateCtx(ctx context.Context, key string, val interface{}) context.Context {
return pcontext.AddToPropagateCtx(ctx, key, val)
Expand Down
3 changes: 3 additions & 0 deletions constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const (
// SessionCtxKey is the context key where the session will be set
var SessionCtxKey = "session"

// LoggerCtxKey is the context key where the default logger will be set
var LoggerCtxKey = "default-logger"

type propagateKey struct{}

// PropagateCtxKey is the context key where the content that will be
Expand Down
2 changes: 2 additions & 0 deletions service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func processHandlerMessage(
remote bool,
) ([]byte, error) {
ctx = context.WithValue(ctx, constants.SessionCtxKey, session)
ctx = util.CtxWithDefaultLogger(ctx, rt.String(), session.UID())

h, err := getHandler(rt)
if err != nil {
return nil, e.NewError(err, e.ErrNotFoundCode)
Expand Down
23 changes: 23 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"reflect"
"runtime/debug"

"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
e "github.com/topfreegames/pitaya/errors"
Expand Down Expand Up @@ -126,6 +128,26 @@ func ConvertProtoToMessageType(protoMsgType protos.MsgType) message.Type {
return msgType
}

// CtxWithDefaultLogger inserts a default logger on ctx to be used on handlers and remotes.
// If using logrus, sessionsId, route and requestId will be added as fields.
// Otherwise the pitaya logger will be used as it is.
func CtxWithDefaultLogger(ctx context.Context, route, sessionID string) context.Context {
var defaultLogger logger.Logger
logrusLogger, ok := logger.Log.(*logrus.Logger)
if ok {
defaultLogger = logrusLogger.WithFields(
logrus.Fields{
"sessionId": sessionID,
"route": route,
"requestId": uuid.New(),
})
} else {
defaultLogger = logger.Log
}

return context.WithValue(ctx, constants.LoggerCtxKey, defaultLogger)
}

// GetContextFromRequest gets the context from a request
func GetContextFromRequest(req *protos.Request, serverID string) (context.Context, error) {
ctx, err := pcontext.Decode(req.GetMetadata())
Expand All @@ -146,5 +168,6 @@ func GetContextFromRequest(req *protos.Request, serverID string) (context.Contex
logger.Log.Warnf("failed to retrieve parent span: %s", err.Error())
}
ctx = tracing.StartSpan(ctx, req.GetMsg().GetRoute(), tags, parent)
ctx = CtxWithDefaultLogger(ctx, req.GetMsg().GetRoute(), "")
return ctx, nil
}

0 comments on commit b95add1

Please sign in to comment.