#348 move trace, time.Now and add else
Some checks failed
prbuild / test (pull_request) Failing after 11s
codeql / analyze (go) (pull_request) Failing after 8s
prbuild / lint (pull_request) Failing after 11s
automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 11s
dependabot-automerge / automerge (pull_request) Failing after 14m33s

This commit is contained in:
Gorbunov Kirill Andreevich 2024-09-20 15:44:10 +03:00
parent 4fcfde6dd1
commit 661e80331a

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 {
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()
}()
}