Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: add BeginTime to stats.End #1907

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,15 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str
func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, md *MethodDesc, trInfo *traceInfo) (err error) {
sh := s.opts.statsHandler
if sh != nil {
beginTime := time.Now()
begin := &stats.Begin{
BeginTime: time.Now(),
BeginTime: beginTime,
}
sh.HandleRPC(stream.Context(), begin)
defer func() {
end := &stats.End{
EndTime: time.Now(),
BeginTime: beginTime,
EndTime: time.Now(),
}
if err != nil && err != io.EOF {
end.Error = toRPCErr(err)
Expand Down Expand Up @@ -977,13 +979,15 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, sd *StreamDesc, trInfo *traceInfo) (err error) {
sh := s.opts.statsHandler
if sh != nil {
beginTime := time.Now()
begin := &stats.Begin{
BeginTime: time.Now(),
BeginTime: beginTime,
}
sh.HandleRPC(stream.Context(), begin)
defer func() {
end := &stats.End{
EndTime: time.Now(),
BeginTime: beginTime,
EndTime: time.Now(),
}
if err != nil && err != io.EOF {
end.Error = toRPCErr(err)
Expand Down
2 changes: 2 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (s *OutTrailer) isRPCStats() {}
type End struct {
// Client is true if this End is from client side.
Client bool
// BeginTime is the time when the RPC began.
BeginTime time.Time
// EndTime is the time when the RPC ends.
EndTime time.Time
// Error is the error the RPC ended with. It is an error generated from
Expand Down
3 changes: 3 additions & 0 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,9 @@ func checkEnd(t *testing.T, d *gotData, e *expectedData) {
if d.ctx == nil {
t.Fatalf("d.ctx = nil, want <non-nil>")
}
if st.BeginTime.IsZero() {
t.Fatalf("st.BeginTime = %v, want <non-zero>", st.BeginTime)
}
if st.EndTime.IsZero() {
t.Fatalf("st.EndTime = %v, want <non-zero>", st.EndTime)
}
Expand Down
19 changes: 13 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,24 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
}
ctx = newContextWithRPCInfo(ctx, c.failFast)
sh := cc.dopts.copts.StatsHandler
var beginTime time.Time
if sh != nil {
ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
beginTime = time.Now()
begin := &stats.Begin{
Client: true,
BeginTime: time.Now(),
BeginTime: beginTime,
FailFast: c.failFast,
}
sh.HandleRPC(ctx, begin)
defer func() {
if err != nil {
// Only handle end stats if err != nil.
end := &stats.End{
Client: true,
Error: err,
Client: true,
Error: err,
BeginTime: beginTime,
EndTime: time.Now(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that end time was not being set here.

}
sh.HandleRPC(ctx, end)
}
Expand Down Expand Up @@ -280,6 +284,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth

statsCtx: ctx,
statsHandler: cc.dopts.copts.StatsHandler,
beginTime: beginTime,
}
if desc != unaryStreamDesc {
// Listen on cc and stream contexts to cleanup when the user closes the
Expand Down Expand Up @@ -334,6 +339,7 @@ type clientStream struct {
// so that all the generated stats for a particular RPC can be associated in the processing phase.
statsCtx context.Context
statsHandler stats.Handler
beginTime time.Time
}

func (cs *clientStream) Context() context.Context {
Expand Down Expand Up @@ -509,9 +515,10 @@ func (cs *clientStream) finish(err error) {
}
if cs.statsHandler != nil {
end := &stats.End{
Client: true,
EndTime: time.Now(),
Error: err,
Client: true,
BeginTime: cs.beginTime,
EndTime: time.Now(),
Error: err,
}
cs.statsHandler.HandleRPC(cs.statsCtx, end)
}
Expand Down