#348 add check method in should be skipped #175

Merged
vtolstov merged 3 commits from kgorbunov/micro-server-grpc:#348-v3 into v3 2024-09-20 17:43:08 +03:00
Showing only changes of commit 661e80331a - Show all commits

39
grpc.go
View File

@ -200,6 +200,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 var err error
ts := time.Now()
ctx := stream.Context() 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") 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 // get grpc metadata
gmd, ok := gmetadata.FromIncomingContext(ctx) gmd, ok := gmetadata.FromIncomingContext(ctx)
if !ok { if !ok {
@ -268,26 +286,8 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
// create new context // create new context
ctx = metadata.NewIncomingContext(ctx, md) 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} stream = &streamWrapper{ctx, stream}
ts := time.Now()
if !slices.Contains(meter.DefaultSkipEndpoints, fullMethod) { if !slices.Contains(meter.DefaultSkipEndpoints, fullMethod) {
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc() g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc()
defer func() { defer func() {
@ -299,8 +299,9 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
st := status.Convert(err) st := status.Convert(err)
if st == nil || st.Code() == codes.OK { 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", "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()
}
}() }()
} }