improve meter
Some checks failed
build / test (push) Failing after 1m48s
build / lint (push) Successful in 9m15s
codeql / analyze (go) (push) Failing after 5m10s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-04-06 22:48:21 +03:00
parent 5ec59f0989
commit e6e64ff070

37
grpc.go
View File

@ -206,6 +206,7 @@ func (g *Server) getGrpcOptions() []grpc.ServerOption {
} }
func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error { func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
var err error
fullMethod, ok := grpc.MethodFromServerStream(stream) fullMethod, ok := grpc.MethodFromServerStream(stream)
if !ok { if !ok {
return status.Errorf(codes.Internal, "method does not exist in context") 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.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds())
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "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() 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 { if err != nil {
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(codes.InvalidArgument))).Inc() err = status.New(codes.InvalidArgument, err.Error()).Err()
return status.New(codes.InvalidArgument, err.Error()).Err() return err
} }
if g.opts.Wait != nil { if g.opts.Wait != nil {
@ -306,19 +315,21 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
if svc == nil { if svc == nil {
if g.unknownHandler != 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() err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err()
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err() return err
} }
mtype := svc.method[methodName] mtype := svc.method[methodName]
if mtype == nil { if mtype == nil {
if g.unknownHandler != 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() err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err()
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err() return err
} }
// process unary // process unary
@ -329,14 +340,6 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
err = g.processStream(ctx, stream, svc, mtype, ct) 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 return err
} }