From e6e64ff0701221c1cc4c5f581b2d861f1e88602a Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 6 Apr 2024 22:48:21 +0300 Subject: [PATCH] improve meter Signed-off-by: Vasiliy Tolstov --- grpc.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/grpc.go b/grpc.go index 41b70d5..9276c1f 100644 --- a/grpc.go +++ b/grpc.go @@ -206,6 +206,7 @@ func (g *Server) getGrpcOptions() []grpc.ServerOption { } func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { + var err error fullMethod, ok := grpc.MethodFromServerStream(stream) if !ok { return status.Errorf(codes.Internal, "method does not exist in context") @@ -218,12 +219,20 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { 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 { + g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc() + } }() - serviceName, methodName, err := serviceMethod(fullMethod) + var serviceName, methodName string + serviceName, methodName, err = serviceMethod(fullMethod) if err != nil { - g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(codes.InvalidArgument))).Inc() - return status.New(codes.InvalidArgument, err.Error()).Err() + err = status.New(codes.InvalidArgument, err.Error()).Err() + return err } if g.opts.Wait != nil { @@ -306,19 +315,21 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { if svc == nil { if g.unknownHandler != nil { - return g.unknownHandler(srv, stream) + err = g.unknownHandler(srv, stream) + return err } - g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(codes.Unimplemented))).Inc() - return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err() + err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err() + return err } mtype := svc.method[methodName] if mtype == nil { if g.unknownHandler != nil { - return g.unknownHandler(srv, stream) + err = g.unknownHandler(srv, stream) + return err } - g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(codes.Unimplemented))).Inc() - return status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err() + err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err() + return err } // process unary @@ -329,14 +340,6 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { err = g.processStream(ctx, stream, svc, mtype, ct) } - 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() - } - return err }