From 040efe0840aee8b5e214159fef51bcd0b7565b28 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Thu, 8 Sep 2022 23:12:37 +0800 Subject: [PATCH] fix for comments Signed-off-by: Ziqi Zhao --- .../grpc/otelgrpc/interceptor.go | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go b/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go index 811577596b4..ae2098c1d64 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go @@ -320,16 +320,6 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor { info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error) { - var statusCode grpc_codes.Code - var startTime = time.Now() - var attr []attribute.KeyValue - - defer func() { - elapsedTime := float64(time.Since(startTime)) / float64(time.Millisecond) - attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(statusCode))) - cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...) - }() - i := &InterceptorInfo{ UnaryServerInfo: info, Type: UnaryServer, @@ -360,6 +350,13 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor { messageReceived.Event(ctx, 1, req) + var statusCode grpc_codes.Code + defer func(t time.Time) { + elapsedTime := float64(time.Since(t)) / float64(time.Millisecond) + attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(statusCode))) + cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...) + }(time.Now()) + resp, err := handler(ctx, req) if err != nil { s, _ := status.FromError(err) @@ -428,17 +425,8 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor { info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error { - var statusCode grpc_codes.Code - var startTime = time.Now() - var attr []attribute.KeyValue ctx := ss.Context() - defer func() { - elapsedTime := float64(time.Since(startTime)) / float64(time.Millisecond) - attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(statusCode))) - cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...) - }() - i := &InterceptorInfo{ StreamServerInfo: info, Type: StreamServer, @@ -467,6 +455,13 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor { ) defer span.End() + var statusCode grpc_codes.Code + defer func(t time.Time) { + elapsedTime := float64(time.Since(t)) / float64(time.Millisecond) + attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(statusCode))) + cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...) + }(time.Now()) + err := handler(srv, wrapServerStream(ctx, ss)) if err != nil { s, _ := status.FromError(err)