diff --git a/grpc.go b/grpc.go index c1c1aee..932b349 100644 --- a/grpc.go +++ b/grpc.go @@ -200,6 +200,7 @@ func (g *Server) getGrpcOptions() []grpc.ServerOption { func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { var err error + ts := time.Now() ctx := stream.Context() @@ -208,6 +209,23 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { return status.Errorf(codes.Internal, "method does not exist in context") } + var sp tracer.Span + if !slices.Contains(tracer.DefaultSkipEndpoints, fullMethod) { + ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server", + tracer.WithSpanKind(tracer.SpanKindServer), + tracer.WithSpanLabels( + "endpoint", fullMethod, + ), + ) + defer func() { + st := status.Convert(err) + if st != nil || st.Code() != codes.OK { + sp.SetStatus(tracer.SpanStatusError, err.Error()) + } + sp.Finish() + }() + } + // get grpc metadata gmd, ok := gmetadata.FromIncomingContext(ctx) if !ok { @@ -268,26 +286,8 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { // create new context ctx = metadata.NewIncomingContext(ctx, md) - var sp tracer.Span - if !slices.Contains(tracer.DefaultSkipEndpoints, fullMethod) { - ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server", - tracer.WithSpanKind(tracer.SpanKindServer), - tracer.WithSpanLabels( - "endpoint", fullMethod, - ), - ) - defer func() { - st := status.Convert(err) - if st != nil || st.Code() != codes.OK { - sp.SetStatus(tracer.SpanStatusError, err.Error()) - } - sp.Finish() - }() - } - stream = &streamWrapper{ctx, stream} - ts := time.Now() if !slices.Contains(meter.DefaultSkipEndpoints, fullMethod) { g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc() defer func() { @@ -299,8 +299,9 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { st := status.Convert(err) if st == nil || st.Code() == codes.OK { g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc() + } else { + g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc() } - g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc() }() }