@@ -32,6 +32,7 @@ import (
32
32
"github.com/xwb1989/sqlparser"
33
33
"golang.org/x/exp/slices"
34
34
35
+ ctlcommon "github.com/deepflowio/deepflow/server/controller/common"
35
36
"github.com/deepflowio/deepflow/server/querier/common"
36
37
"github.com/deepflowio/deepflow/server/querier/config"
37
38
"github.com/deepflowio/deepflow/server/querier/engine/clickhouse/client"
@@ -104,6 +105,7 @@ type CHEngine struct {
104
105
DerivativeGroupBy []string
105
106
ORGID string
106
107
Language string
108
+ NativeField map [string ]* metrics.Metrics
107
109
}
108
110
109
111
func init () {
@@ -126,7 +128,6 @@ func (e *CHEngine) ExecuteQuery(args *common.QuerierParams) (*common.Result, map
126
128
e .ORGID = args .ORGID
127
129
}
128
130
query_uuid := args .QueryUUID // FIXME: should be queryUUID
129
- log .Debugf ("query_uuid: %s | raw sql: %s" , query_uuid , sql )
130
131
debug_info := & client.DebugInfo {}
131
132
// Parse withSql
132
133
withResult , withDebug , err := e .QueryWithSql (sql , args )
@@ -215,15 +216,13 @@ func (e *CHEngine) ExecuteQuery(args *common.QuerierParams) (*common.Result, map
215
216
usedEngine .View .NoPreWhere = usedEngine .NoPreWhere
216
217
}
217
218
chSql := usedEngine .ToSQLString ()
218
- log .Debug (chSql )
219
219
callbacks := usedEngine .View .GetCallbacks ()
220
220
debug .Sql = chSql
221
221
if ! isShow {
222
222
for _ , ColumnSchema := range usedEngine .ColumnSchemas {
223
223
ColumnSchemaMap [ColumnSchema .Name ] = ColumnSchema
224
224
}
225
225
}
226
- log .Debug (ColumnSchemaMap )
227
226
params := & client.QueryParams {
228
227
Sql : chSql ,
229
228
UseQueryCache : args .UseQueryCache ,
@@ -1232,6 +1231,38 @@ func (e *CHEngine) TransFrom(froms sqlparser.TableExprs) error {
1232
1231
table = strings .ReplaceAll (table , "vtap_acl" , "traffic_policy" )
1233
1232
}
1234
1233
e .Table = table
1234
+ // native field
1235
+ if config .ControllerCfg .DFWebService .Enabled && slices .Contains ([]string {chCommon .DB_NAME_DEEPFLOW_ADMIN , chCommon .DB_NAME_DEEPFLOW_TENANT , chCommon .DB_NAME_APPLICATION_LOG , chCommon .DB_NAME_EXT_METRICS }, e .DB ) || slices .Contains ([]string {chCommon .TABLE_NAME_L7_FLOW_LOG , chCommon .TABLE_NAME_EVENT , chCommon .TABLE_NAME_PERF_EVENT }, e .Table ) {
1236
+ e .NativeField = map [string ]* metrics.Metrics {}
1237
+ getNativeUrl := fmt .Sprintf ("http://localhost:%d/v1/native-fields/?db=%s&table_name=%s" , config .ControllerCfg .ListenPort , e .DB , e .Table )
1238
+ resp , err := ctlcommon .CURLPerform ("GET" , getNativeUrl , nil , ctlcommon .WithHeader (ctlcommon .HEADER_KEY_X_ORG_ID , e .ORGID ))
1239
+ if err != nil {
1240
+ log .Errorf ("request controller failed: %s, URL: %s" , resp , getNativeUrl )
1241
+ } else {
1242
+ resultArray := resp .Get ("DATA" ).MustArray ()
1243
+ for i := range resultArray {
1244
+ nativeMetric := resp .Get ("DATA" ).GetIndex (i ).Get ("NAME" ).MustString ()
1245
+ displayName := resp .Get ("DATA" ).GetIndex (i ).Get ("DISPLAY_NAME" ).MustString ()
1246
+ description := resp .Get ("DATA" ).GetIndex (i ).Get ("DESCRIPTION" ).MustString ()
1247
+ fieldType := resp .Get ("DATA" ).GetIndex (i ).Get ("FIELD_TYPE" ).MustInt ()
1248
+ if fieldType == chCommon .NATIVE_FIELD_TYPE_METRIC {
1249
+ metric := metrics .NewMetrics (
1250
+ 0 , nativeMetric ,
1251
+ displayName , displayName , displayName , "" , "" , "" , metrics .METRICS_TYPE_COUNTER ,
1252
+ chCommon .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , description , description , description , "" , "" ,
1253
+ )
1254
+ e .NativeField [nativeMetric ] = metric
1255
+ } else {
1256
+ metric := metrics .NewMetrics (
1257
+ 0 , nativeMetric ,
1258
+ displayName , displayName , displayName , "" , "" , "" , metrics .METRICS_TYPE_NAME_MAP ["tag" ],
1259
+ chCommon .NATIVE_FIELD_CATEGORY_CUSTOM_TAG , []bool {true , true , true }, "" , table , "" , "" , "" , "" , "" ,
1260
+ )
1261
+ e .NativeField [nativeMetric ] = metric
1262
+ }
1263
+ }
1264
+ }
1265
+ }
1235
1266
// ext_metrics只有metrics表,使用virtual_table_name做过滤区分
1236
1267
if e .DB == "ext_metrics" {
1237
1268
table = "metrics"
@@ -1705,7 +1736,7 @@ func (e *CHEngine) parseSelectBinaryExpr(node sqlparser.Expr) (binary Function,
1705
1736
if fieldFunc != nil {
1706
1737
return fieldFunc , nil
1707
1738
}
1708
- metricStruct , ok := metrics .GetAggMetrics (field , e .DB , e .Table , e .ORGID )
1739
+ metricStruct , ok := metrics .GetAggMetrics (field , e .DB , e .Table , e .ORGID , e . NativeField )
1709
1740
if ok {
1710
1741
return & Field {Value : metricStruct .DBField }, nil
1711
1742
}
@@ -1818,7 +1849,7 @@ func (e *CHEngine) parseWhere(node sqlparser.Expr, w *Where, isCheck bool) (view
1818
1849
switch comparExpr .(type ) {
1819
1850
case * sqlparser.ColName , * sqlparser.SQLVal :
1820
1851
whereTag := chCommon .ParseAlias (node .Left )
1821
- metricStruct , ok := metrics .GetMetrics (whereTag , e .DB , e .Table , e .ORGID )
1852
+ metricStruct , ok := metrics .GetMetrics (whereTag , e .DB , e .Table , e .ORGID , e . NativeField )
1822
1853
if ok && metricStruct .Type != metrics .METRICS_TYPE_TAG {
1823
1854
whereTag = metricStruct .DBField
1824
1855
}
0 commit comments