Skip to content

Commit

Permalink
add tracing for streaming rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhouhan committed Jun 18, 2015
1 parent 4782e69 commit 58dcee7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
3 changes: 0 additions & 3 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ type callInfo struct {
traceInfo traceInfo // in trace.go
}

// EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
// This should only be set before any RPCs are sent or received by this program.
var EnableTracing = true

// Invoke is called by the generated code. It sends the RPC request on the
// wire and returns after response is received.
Expand Down
49 changes: 39 additions & 10 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ package grpc
import (
"errors"
"io"
"time"

"golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/transport"
Expand Down Expand Up @@ -94,6 +96,7 @@ type ClientStream interface {
// by generated code.
func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
// TODO(zhaoq): CallOption is omitted. Add support when it is needed.
var trInfo traceInfo
callHdr := &transport.CallHdr{
Host: cc.authority,
Method: method,
Expand All @@ -102,26 +105,36 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
if err != nil {
return nil, toRPCErr(err)
}
if EnableTracing {
trInfo.tr = trace.New("Sent."+methodFamily(desc.StreamName), desc.StreamName)
trInfo.firstLine.client = true
if deadline, ok := ctx.Deadline(); ok {
trInfo.firstLine.deadline = deadline.Sub(time.Now())
}
trInfo.tr.LazyLog(&trInfo.firstLine, false)
}
s, err := t.NewStream(ctx, callHdr)
if err != nil {
return nil, toRPCErr(err)
}
return &clientStream{
t: t,
s: s,
p: &parser{s: s},
desc: desc,
codec: cc.dopts.codec,
t: t,
s: s,
p: &parser{s: s},
desc: desc,
codec: cc.dopts.codec,
traceInfo: trInfo,
}, nil
}

// clientStream implements a client side Stream.
type clientStream struct {
t transport.ClientTransport
s *transport.Stream
p *parser
desc *StreamDesc
codec Codec
t transport.ClientTransport
s *transport.Stream
p *parser
desc *StreamDesc
codec Codec
traceInfo traceInfo
}

func (cs *clientStream) Context() context.Context {
Expand Down Expand Up @@ -152,6 +165,7 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) {
}
err = toRPCErr(err)
}()

out, err := encode(cs.codec, m, compressionNone)
if err != nil {
return transport.StreamErrorf(codes.Internal, "grpc: %v", err)
Expand All @@ -172,23 +186,38 @@ func (cs *clientStream) RecvMsg(m interface{}) (err error) {
return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
}
if err == io.EOF {
if EnableTracing {
defer cs.traceInfo.tr.Finish()
}
if cs.s.StatusCode() == codes.OK {
return nil
}
return Errorf(cs.s.StatusCode(), cs.s.StatusDesc())
}
if EnableTracing {
cs.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
cs.traceInfo.tr.SetError()
}
return toRPCErr(err)
}
if _, ok := err.(transport.ConnectionError); !ok {
cs.t.CloseStream(cs.s, err)
}
if err == io.EOF {
if EnableTracing {
cs.traceInfo.tr.Finish()
}

if cs.s.StatusCode() == codes.OK {
// Returns io.EOF to indicate the end of the stream.
return
}
return Errorf(cs.s.StatusCode(), cs.s.StatusDesc())
}
if EnableTracing {
cs.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
cs.traceInfo.tr.SetError()
}
return toRPCErr(err)
}

Expand Down
4 changes: 4 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import (
"golang.org/x/net/trace"
)

// EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
// This should only be set before any RPCs are sent or received by this program.
var EnableTracing = true

// methodFamily returns the trace family for the given method.
// It turns "/pkg.Service/GetFoo" into "pkg.Service".
func methodFamily(m string) string {
Expand Down

0 comments on commit 58dcee7

Please sign in to comment.