fix: [Cherry-pick] accesslog can not print search expression (#28931)

relate: https://github.com/milvus-io/milvus/issues/28893
pr: https://github.com/milvus-io/milvus/pull/28899

---------

Signed-off-by: aoiasd <zhicheng.yue@zilliz.com>
pull/29069/head
aoiasd 2023-12-08 02:00:36 +08:00 committed by GitHub
parent 5f4ac437b2
commit b639fd66b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 13 deletions

View File

@ -200,18 +200,18 @@ proxy:
ginLogging: true
maxTaskNum: 1024 # max task number of proxy task queue
accessLog:
enable: true
enable: false
# Log filename, set as "" to use stdout.
filename: ""
# filename: ""
# define formatters for access log by XXX:{format: XXX, method:[XXX,XXX]}
formatters:
# "base" formatter could not set methods
# all method will use "base" formatter default
base:
# will not print access log if set as ""
format: "[$time_now] [ACCESS] <$user_name: $user_addr> $method_name [status: $method_status] [code: $error_code] [msg: $error_msg] [traceID: $trace_id] [timeCost: $time_cost]"
format: "[$time_now] [ACCESS] <$user_name: $user_addr> $method_name [status: $method_status] [code: $error_code] [sdk: $sdk_version] [msg: $error_msg] [traceID: $trace_id] [timeCost: $time_cost]"
query:
format: "[$time_now] [ACCESS] <$user_name: $user_addr> $method_name [status: $method_status] [code: $error_code] [msg: $error_msg] [traceID: $trace_id] [timeCost: $time_cost] [database: $database_name] [collection: $collection_name] [partitions: $partition_name] [expr: $method_expr]"
format: "[$time_now] [ACCESS] <$user_name: $user_addr> $method_name [status: $method_status] [code: $error_code] [sdk: $sdk_version] [msg: $error_msg] [traceID: $trace_id] [timeCost: $time_cost] [database: $database_name] [collection: $collection_name] [partitions: $partition_name] [expr: $method_expr]"
# set formatter owners by method name(method was all milvus external interface)
# all method will use base formatter default
# one method only could use one formatter

View File

@ -88,11 +88,8 @@ func (i *GrpcAccessInfo) SetResult(resp interface{}, err error) {
func (i *GrpcAccessInfo) Get(keys ...string) []string {
result := []string{}
metricMap := map[string]string{}
for _, key := range keys {
if value, ok := metricMap[key]; ok {
result = append(result, value)
} else if getFunc, ok := metricFuncMap[key]; ok {
if getFunc, ok := metricFuncMap[key]; ok {
result = append(result, getFunc(i))
}
}
@ -108,6 +105,7 @@ func (i *GrpcAccessInfo) Write() bool {
if !ok {
return false
}
_, err := _globalW.Write([]byte(formatter.Format(i)))
return err == nil
}
@ -256,10 +254,15 @@ func getPartitionName(i *GrpcAccessInfo) string {
func getExpr(i *GrpcAccessInfo) string {
expr, ok := requestutil.GetExprFromRequest(i.req)
if !ok {
return unknownString
if ok {
return expr.(string)
}
return expr.(string)
dsl, ok := requestutil.GetDSLFromRequest(i.req)
if ok {
return dsl.(string)
}
return unknownString
}
func getSdkVersion(i *GrpcAccessInfo) string {

View File

@ -45,7 +45,7 @@ type GrpcAccessInfoSuite struct {
info *GrpcAccessInfo
}
func (s *GrpcAccessInfoSuite) SetupSuite() {
func (s *GrpcAccessInfoSuite) SetupTest() {
s.username = "test-user"
s.traceID = "test-trace"
@ -130,6 +130,24 @@ func (s *GrpcAccessInfoSuite) TestSdkInfo() {
s.Equal(info.SdkType+"-"+info.SdkVersion, result[0])
}
func (s *GrpcAccessInfoSuite) TestExpression() {
result := s.info.Get("$method_expr")
s.Equal(unknownString, result[0])
testExpr := "test"
s.info.req = &milvuspb.QueryRequest{
Expr: testExpr,
}
result = s.info.Get("$method_expr")
s.Equal(testExpr, result[0])
s.info.req = &milvuspb.SearchRequest{
Dsl: testExpr,
}
result = s.info.Get("$method_expr")
s.Equal(testExpr, result[0])
}
func TestGrpcAccssInfo(t *testing.T) {
suite.Run(t, new(GrpcAccessInfoSuite))
}

View File

@ -1062,7 +1062,7 @@ please adjust in embedded Milvus: false`,
p.AccessLog.Filename = ParamItem{
Key: "proxy.accessLog.filename",
Version: "2.2.0",
DefaultValue: "milvus_access_log.log",
DefaultValue: "",
Doc: "Log filename, leave empty to use stdout.",
Export: true,
}