Skip to content

Commit

Permalink
fix for comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Sep 8, 2022
1 parent 4cb7bdc commit 5ef181d
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,16 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
requestStartTime := time.Now()
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,
Expand Down Expand Up @@ -352,19 +361,16 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
messageReceived.Event(ctx, 1, req)

resp, err := handler(ctx, req)
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)
if err != nil {
s, _ := status.FromError(err)
statusCode = s.Code()
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
messageSent.Event(ctx, 1, s.Proto())
attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(s.Code())))
cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...)
} else {
statusCode = grpc_codes.OK
span.SetAttributes(statusCodeAttr(grpc_codes.OK))
messageSent.Event(ctx, 1, resp)
attr = append(attr, semconv.RPCGRPCStatusCodeOk)
cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...)
}

return resp, err
Expand Down Expand Up @@ -422,8 +428,17 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
requestStartTime := time.Now()
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,
Expand Down Expand Up @@ -453,17 +468,14 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
defer span.End()

err := handler(srv, wrapServerStream(ctx, ss))
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)
if err != nil {
s, _ := status.FromError(err)
statusCode = s.Code()
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(s.Code())))
cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...)
} else {
statusCode = grpc_codes.OK
span.SetAttributes(statusCodeAttr(grpc_codes.OK))
attr = append(attr, semconv.RPCGRPCStatusCodeOk)
cfg.rpcServerDuration.Record(ctx, elapsedTime, attr...)
}

return err
Expand Down

0 comments on commit 5ef181d

Please sign in to comment.