Skip to content

Commit

Permalink
Avoid creating two spans for nats rpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Hahn committed May 29, 2019
1 parent d1c207f commit 3ef0251
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions cluster/nats_rpc_server.go
Expand Up @@ -203,6 +203,7 @@ func (ns *NatsRPCServer) handleMessages() {
maxPending = math.Max(float64(maxPending), subsChanLen)
logger.Log.Debugf("subs channel size: %d, max: %d, dropped: %d", subsChanLen, maxPending, dropped)
req := &protos.Request{}
// TODO: Add tracing here to report delay to start processing message in spans
err = proto.Unmarshal(msg.Data, req)
if err != nil {
// should answer rpc with an error
Expand Down
1 change: 1 addition & 0 deletions service/remote.go
Expand Up @@ -126,6 +126,7 @@ func (r *RemoteService) AddRemoteBindingListener(bindingListener cluster.RemoteB
// Call processes a remote call
func (r *RemoteService) Call(ctx context.Context, req *protos.Request) (*protos.Response, error) {
c, err := util.GetContextFromRequest(req, r.server.ID)
c = util.StartSpanFromRequest(c, r.server.ID, req.GetMsg().GetRoute())
var res *protos.Response
if err != nil {
res = &protos.Response{
Expand Down
27 changes: 19 additions & 8 deletions util/util.go
Expand Up @@ -175,14 +175,13 @@ func CtxWithDefaultLogger(ctx context.Context, route, userID string) context.Con
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())
if err != nil {
return nil, err
}
// StartSpanFromRequest starts a tracing span from the request
func StartSpanFromRequest(
ctx context.Context,
serverID, route string,
) context.Context {
if ctx == nil {
return nil, constants.ErrNoContextFound
return nil
}
tags := opentracing.Tags{
"local.id": serverID,
Expand All @@ -195,7 +194,19 @@ func GetContextFromRequest(req *protos.Request, serverID string) (context.Contex
if err != nil {
logger.Log.Warnf("failed to retrieve parent span: %s", err.Error())
}
ctx = tracing.StartSpan(ctx, req.GetMsg().GetRoute(), tags, parent)
ctx = tracing.StartSpan(ctx, route, tags, parent)
return ctx
}

// GetContextFromRequest gets the context from a request
func GetContextFromRequest(req *protos.Request, serverID string) (context.Context, error) {
ctx, err := pcontext.Decode(req.GetMetadata())
if err != nil {
return nil, err
}
if ctx == nil {
return nil, constants.ErrNoContextFound
}
ctx = CtxWithDefaultLogger(ctx, req.GetMsg().GetRoute(), "")
return ctx, nil
}

0 comments on commit 3ef0251

Please sign in to comment.