From 4fcfde6dd16d3ce2d32e6e7a7f372aac7da1f211 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Fri, 20 Sep 2024 15:11:33 +0300 Subject: [PATCH 1/3] #348 add check method in should be skipped --- go.sum | 15 +-------------- grpc.go | 50 +++++++++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/go.sum b/go.sum index c7c005c..d44355e 100644 --- a/go.sum +++ b/go.sum @@ -1,33 +1,20 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= -go.unistack.org/micro/v3 v3.10.66 h1:tiG8HnyTC71IZWSC2qT/DmLhJinZJL9qvw+4Fvpm3d4= -go.unistack.org/micro/v3 v3.10.66/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U= go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ= google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= -google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= -google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= diff --git a/grpc.go b/grpc.go index 5ddd9ba..c1c1aee 100644 --- a/grpc.go +++ b/grpc.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "reflect" + "slices" "sort" "strconv" "strings" @@ -268,33 +269,40 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { ctx = metadata.NewIncomingContext(ctx, md) var sp tracer.Span - ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server", - tracer.WithSpanKind(tracer.SpanKindServer), - tracer.WithSpanLabels( - "endpoint", fullMethod, - ), - ) + 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() - g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc() + if !slices.Contains(meter.DefaultSkipEndpoints, fullMethod) { + g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc() + defer func() { + te := time.Since(ts) + g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds()) + g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds()) + g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec() - defer func() { - te := time.Since(ts) - g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds()) - g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds()) - g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec() - - 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 { - sp.SetStatus(tracer.SpanStatusError, err.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() + } g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc() - } - sp.Finish() - }() + }() + } if g.opts.Wait != nil { g.opts.Wait.Add(1) -- 2.45.2 From 661e80331a3c9568abdd1739ce6a8e51849cc363 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Fri, 20 Sep 2024 15:44:10 +0300 Subject: [PATCH 2/3] #348 move trace, time.Now and add else --- grpc.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) 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() }() } -- 2.45.2 From 1f033505f69e0348735003d018601496156164c2 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Fri, 20 Sep 2024 17:42:01 +0300 Subject: [PATCH 3/3] #348 move time.Now --- grpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc.go b/grpc.go index 932b349..9a367f2 100644 --- a/grpc.go +++ b/grpc.go @@ -200,7 +200,6 @@ 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() @@ -209,6 +208,7 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { return status.Errorf(codes.Internal, "method does not exist in context") } + ts := time.Now() var sp tracer.Span if !slices.Contains(tracer.DefaultSkipEndpoints, fullMethod) { ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server", -- 2.45.2