From 24c565e37beeb4bd86e57373d2a59ead83bf6b31 Mon Sep 17 00:00:00 2001 From: aoiasd <45024769+aoiasd@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:30:38 +0800 Subject: [PATCH] fix: accesslog method status not return failed when error in response (#28824) relate: https://github.com/milvus-io/milvus/issues/28086 Signed-off-by: aoiasd --- internal/proxy/accesslog/info.go | 40 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/internal/proxy/accesslog/info.go b/internal/proxy/accesslog/info.go index 98ee14ee9f..72cb5b3a56 100644 --- a/internal/proxy/accesslog/info.go +++ b/internal/proxy/accesslog/info.go @@ -39,11 +39,12 @@ type AccessInfo interface { } type GrpcAccessInfo struct { - ctx context.Context - info *grpc.UnaryServerInfo - req interface{} - resp interface{} - err error + ctx context.Context + info *grpc.UnaryServerInfo + status *commonpb.Status + req interface{} + resp interface{} + err error start time.Time end time.Time @@ -69,6 +70,19 @@ func (i *GrpcAccessInfo) SetResult(resp interface{}, err error) { i.resp = resp i.err = err i.end = time.Now() + + // extract status from response + baseResp, ok := i.resp.(BaseResponse) + if ok { + i.status = baseResp.GetStatus() + return + } + + status, ok := i.resp.(*commonpb.Status) + if ok { + i.status = status + return + } } func (i *GrpcAccessInfo) Get(keys ...string) []string { @@ -150,6 +164,11 @@ func getMethodStatus(i *GrpcAccessInfo) string { if code != codes.OK && code != codes.Unknown { return fmt.Sprintf("Grpc%s", code.String()) } + + if i.status.GetCode() != 0 { + return "Failed" + } + return code.String() } @@ -179,15 +198,8 @@ type BaseResponse interface { } func getErrorCode(i *GrpcAccessInfo) string { - baseResp, ok := i.resp.(BaseResponse) - if ok { - status := baseResp.GetStatus() - return fmt.Sprint(int(status.GetErrorCode())) - } - - status, ok := i.resp.(*commonpb.Status) - if ok { - return fmt.Sprint(int(status.GetErrorCode())) + if i.status != nil { + return fmt.Sprint(i.status.GetCode()) } return fmt.Sprint(merr.Code(i.err))