Skip to content

Commit

Permalink
Using same request id for logger and tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Hahn committed Mar 11, 2019
1 parent 9b180d0 commit 7761bf6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var PeerServiceKey = "peer.service"
// StartTimeKey is the key holding the request start time (in ns) to be sent over the context
var StartTimeKey = "req-start-time"

// RequestIDKey is the key holding the request id to be sent over the context
var RequestIDKey = "request.id"

// RouteKey is the key holding the request route to be sent over the context
var RouteKey = "req-route"

Expand Down
3 changes: 3 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func GetFromPropagateCtx(ctx context.Context, key string) interface{} {

// ToMap returns the values that will be propagated through RPC calls in map[string]interface{} format
func ToMap(ctx context.Context) map[string]interface{} {
if ctx == nil {
return map[string]interface{}{}
}
p := ctx.Value(constants.PropagateCtxKey)
if p != nil {
return p.(map[string]interface{})
Expand Down
14 changes: 9 additions & 5 deletions service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
"strings"
"time"

"github.com/opentracing/opentracing-go"
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/topfreegames/pitaya/agent"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
Expand Down Expand Up @@ -262,13 +263,16 @@ func (h *HandlerService) processPacket(a *agent.Agent, p *packet.Packet) error {
}

func (h *HandlerService) processMessage(a *agent.Agent, msg *message.Message) {
requestID := uuid.New()
ctx := pcontext.AddToPropagateCtx(context.Background(), constants.StartTimeKey, time.Now().UnixNano())
ctx = pcontext.AddToPropagateCtx(ctx, constants.RouteKey, msg.Route)
ctx = pcontext.AddToPropagateCtx(ctx, constants.RequestIDKey, requestID.String())
tags := opentracing.Tags{
"local.id": h.server.ID,
"span.kind": "server",
"msg.type": strings.ToLower(msg.Type.String()),
"user.id": a.Session.UID(),
"local.id": h.server.ID,
"span.kind": "server",
"msg.type": strings.ToLower(msg.Type.String()),
"user.id": a.Session.UID(),
"request.id": requestID.String(),
}
ctx = tracing.StartSpan(ctx, msg.Route, tags)
ctx = context.WithValue(ctx, constants.SessionCtxKey, a.Session)
Expand Down
3 changes: 3 additions & 0 deletions service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func processHandlerMessage(
msgTypeIface interface{},
remote bool,
) ([]byte, error) {
if ctx == nil {
ctx = context.Background()
}
ctx = context.WithValue(ctx, constants.SessionCtxKey, session)
ctx = util.CtxWithDefaultLogger(ctx, rt.String(), session.UID())

Expand Down
11 changes: 10 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,18 @@ func CtxWithDefaultLogger(ctx context.Context, route, userID string) context.Con
var defaultLogger logger.Logger
logrusLogger, ok := logger.Log.(logrus.FieldLogger)
if ok {
requestID := pcontext.GetFromPropagateCtx(ctx, constants.RequestIDKey)
if rID, ok := requestID.(string); ok {
if rID == "" {
requestID = uuid.New()
}
} else {
requestID = uuid.New()
}
defaultLogger = logrusLogger.WithFields(
logrus.Fields{
"route": route,
"requestId": uuid.New(),
"requestId": requestID,
"userId": userID,
})
} else {
Expand All @@ -181,6 +189,7 @@ func GetContextFromRequest(req *protos.Request, serverID string) (context.Contex
"span.kind": "server",
"peer.id": pcontext.GetFromPropagateCtx(ctx, constants.PeerIDKey),
"peer.service": pcontext.GetFromPropagateCtx(ctx, constants.PeerServiceKey),
"request.id": pcontext.GetFromPropagateCtx(ctx, constants.RequestIDKey),
}
parent, err := tracing.ExtractSpan(ctx)
if err != nil {
Expand Down

0 comments on commit 7761bf6

Please sign in to comment.