Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] fix metrics bug #1800

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions internal/net/grpc/interceptor/server/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,32 @@ func MetricInterceptors() (grpc.UnaryServerInterceptor, grpc.StreamServerInterce
return nil, nil, errors.Wrap(err, "failed to create completedRPCs metric")
}

record := func(ctx context.Context, method string, err error, latency float64) {
code := codes.OK // default error is success when error is nil
if err != nil {
st, _ := status.FromError(err)
if st != nil {
code = st.Code()
}
kpango marked this conversation as resolved.
Show resolved Hide resolved
}
attrs := []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, method),
attribute.String(gRPCStatus, code.String()),
}
latencyHistgram.Record(ctx, latency, attrs...)
completedRPCCnt.Add(ctx, 1, attrs...)
}
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
now := time.Now()

resp, err = handler(ctx, req)

elapsedTime := time.Since(now)

code := codes.Unknown.String()
st, _ := status.FromError(err)
if st != nil {
code = st.Code().String()
}

latency := float64(elapsedTime) / float64(time.Millisecond)

attrs := []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, info.FullMethod),
attribute.String(gRPCStatus, code),
}
latencyHistgram.Record(ctx, latency, attrs...)
completedRPCCnt.Add(ctx, 1, attrs...)

record(ctx, info.FullMethod, err, float64(elapsedTime)/float64(time.Millisecond))
return resp, err
}, func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
now := time.Now()

err = handler(srv, ss)

elapsedTime := time.Since(now)

code := codes.Unknown.String()
st, _ := status.FromError(err)
if st != nil {
code = st.Code().String()
}

latency := float64(elapsedTime) / float64(time.Millisecond)

attrs := []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, info.FullMethod),
attribute.String(gRPCStatus, code),
}
latencyHistgram.Record(ss.Context(), latency, attrs...)
completedRPCCnt.Add(ss.Context(), 1, attrs...)

record(ss.Context(), info.FullMethod, err, float64(elapsedTime)/float64(time.Millisecond))
return err
}, nil
}