mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 05:18:52 +08:00
fca2b71e28
Signed-off-by: aoiasd <zhicheng.yue@zilliz.com> Signed-off-by: aoiasd <zhicheng.yue@zilliz.com>
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
// Licensed to the LF AI & Data foundation under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package accesslog
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/peer"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type BaseResponse interface {
|
|
GetStatus() *commonpb.Status
|
|
}
|
|
|
|
func UnaryAccessLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
starttime := time.Now()
|
|
resp, err := handler(ctx, req)
|
|
PrintAccessInfo(ctx, resp, err, info, time.Since(starttime).Milliseconds())
|
|
return resp, err
|
|
}
|
|
|
|
func getAccessAddr(ctx context.Context) string {
|
|
ip, ok := peer.FromContext(ctx)
|
|
if !ok {
|
|
return "Unknown"
|
|
}
|
|
return fmt.Sprintf("%s-%s", ip.Addr.Network(), ip.Addr.String())
|
|
}
|
|
|
|
func getTraceID(ctx context.Context) (id string, ok bool) {
|
|
meta, ok := metadata.FromOutgoingContext(ctx)
|
|
if ok {
|
|
return meta.Get(clientRequestIDKey)[0], true
|
|
}
|
|
|
|
traceID, _, ok := trace.InfoFromContext(ctx)
|
|
if ok {
|
|
return traceID, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func getResponseSize(resq interface{}) (int, bool) {
|
|
message, ok := resq.(proto.Message)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
|
|
return proto.Size(message), true
|
|
}
|
|
|
|
func getErrCode(resp interface{}) (int, bool) {
|
|
baseResp, ok := resp.(BaseResponse)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
|
|
status := baseResp.GetStatus()
|
|
return int(status.ErrorCode), true
|
|
}
|
|
|
|
func getGrpcStatus(err error) string {
|
|
code := status.Code(err)
|
|
if code != codes.OK {
|
|
return fmt.Sprintf("Grpc%s", code.String())
|
|
}
|
|
return code.String()
|
|
}
|