|
@@ -2,6 +2,8 @@ package service
|
|
|
|
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
|
|
+ "context"
|
|
|
|
+ "esproxy/internal/consts"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
@@ -23,6 +25,23 @@ func Middleware(r *ghttp.Request) {
|
|
r.Request.Header.Set("Content-Length", fmt.Sprintf("%d", len(finalBytes)))
|
|
r.Request.Header.Set("Content-Length", fmt.Sprintf("%d", len(finalBytes)))
|
|
r.Request.Body = io.NopCloser(bytes.NewReader(finalBytes))
|
|
r.Request.Body = io.NopCloser(bytes.NewReader(finalBytes))
|
|
}
|
|
}
|
|
|
|
+ queryLevel := getQueryLevel(bodyBytes)
|
|
|
|
+ r.SetCtxVar(consts.QueryLevelKey, queryLevel)
|
|
r.Middleware.Next()
|
|
r.Middleware.Next()
|
|
- g.Log().Infof(r.Context(), "status:%d time:%fs from:%s query:%s", r.Response.Status, time.Since(now).Seconds(), r.GetClientIp(), string(bodyBytes))
|
|
|
|
|
|
+ g.Log().Infof(r.Context(), "status:%d time:%fs level:%d from:%s query:%s", r.Response.Status, time.Since(now).Seconds(), queryLevel, r.GetClientIp(), string(bodyBytes))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ aggsFlag = []byte("aggs")
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// getQueryLevel 获取sql查询复杂度
|
|
|
|
+func getQueryLevel(detail []byte) consts.QueryLevel {
|
|
|
|
+ if isAggs := bytes.Contains(detail, aggsFlag); !isAggs {
|
|
|
|
+ return consts.QueryLevelSimple
|
|
|
|
+ }
|
|
|
|
+ if queryLen := len(string(detail)); queryLen < g.Cfg().MustGet(context.Background(), "elasticSearch.complexQueryLen", 200).Int() {
|
|
|
|
+ return consts.QueryLevelAggs
|
|
|
|
+ }
|
|
|
|
+ return consts.QueryLevelComplex
|
|
}
|
|
}
|