@@ -24,6 +24,7 @@ import (
2424 "slices"
2525 "strings"
2626
27+ simplejson "github.com/bitly/go-simplejson"
2728 "github.com/deepflowio/deepflow/server/querier/common"
2829 "github.com/deepflowio/deepflow/server/querier/config"
2930 ckcommon "github.com/deepflowio/deepflow/server/querier/engine/clickhouse/common"
@@ -116,7 +117,7 @@ func NewReplaceMetrics(dbField string, condition string) *Metrics {
116117 }
117118}
118119
119- func GetAggMetrics (field , db , table , orgID string , nativeField map [string ]* Metrics ) (* Metrics , bool ) {
120+ func GetAggMetrics (field , db , table , orgID string , nativeField map [string ]* Metrics , customMetrics map [ string ] * simplejson. Json ) (* Metrics , bool ) {
120121 field = strings .Trim (field , "`" )
121122 if field == COUNT_METRICS_NAME {
122123 return & Metrics {
@@ -129,7 +130,7 @@ func GetAggMetrics(field, db, table, orgID string, nativeField map[string]*Metri
129130 Table : table ,
130131 }, true
131132 }
132- return GetMetrics (field , db , table , orgID , nativeField )
133+ return GetMetrics (field , db , table , orgID , nativeField , customMetrics )
133134}
134135
135136func GetTagTypeMetrics (tagDescriptions * common.Result , newAllMetrics map [string ]* Metrics , db , table , orgID string ) error {
@@ -225,7 +226,7 @@ func GetTagTypeMetrics(tagDescriptions *common.Result, newAllMetrics map[string]
225226 return nil
226227}
227228
228- func GetMetrics (field , db , table , orgID string , nativeField map [string ]* Metrics ) (* Metrics , bool ) {
229+ func GetMetrics (field , db , table , orgID string , nativeField map [string ]* Metrics , customMetrics map [ string ] * simplejson. Json ) (* Metrics , bool ) {
229230 newAllMetrics := map [string ]* Metrics {}
230231 field = strings .Trim (field , "`" )
231232 // flow_tag database has no metrics
@@ -241,17 +242,35 @@ func GetMetrics(field, db, table, orgID string, nativeField map[string]*Metrics)
241242 )
242243 return metric , true
243244 }
245+
244246 // dynamic metrics
245247 if slices .Contains ([]string {ckcommon .DB_NAME_DEEPFLOW_ADMIN , ckcommon .DB_NAME_DEEPFLOW_TENANT , ckcommon .DB_NAME_APPLICATION_LOG , ckcommon .DB_NAME_EXT_METRICS }, db ) || slices .Contains ([]string {ckcommon .TABLE_NAME_L7_FLOW_LOG , ckcommon .TABLE_NAME_EVENT , ckcommon .TABLE_NAME_FILE_EVENT }, table ) {
246248 fieldSplit := strings .Split (field , "." )
247249 if len (fieldSplit ) > 1 {
248250 if fieldSplit [0 ] == "metrics" {
249251 fieldName := strings .Replace (field , "metrics." , "" , 1 )
252+ var mUnit , description string
253+ mType := METRICS_TYPE_COUNTER
254+ if customMetrics == nil {
255+ var fetchErr error
256+ customMetrics , fetchErr = ckcommon .GetCustomMetrics (orgID )
257+ if fetchErr != nil {
258+ log .Error (fetchErr .Error ())
259+ customMetrics = map [string ]* simplejson.Json {}
260+ }
261+ }
262+ customMetric , ok := customMetrics [fmt .Sprintf ("%s.%s.%s" , db , table , fieldName )]
263+ if ok {
264+ mType = customMetric .Get ("TYPE" ).MustInt ()
265+ mUnit = customMetric .Get ("UNIT" ).MustString ()
266+ field = customMetric .Get ("DISPLAY_NAME" ).MustString ()
267+ description = customMetric .Get ("DESCRIPTION" ).MustString ()
268+ }
250269 metrics_names_field , metrics_values_field := METRICS_ARRAY_NAME_MAP [db ][0 ], METRICS_ARRAY_NAME_MAP [db ][1 ]
251270 metric := NewMetrics (
252271 0 , fmt .Sprintf ("if(indexOf(%s, '%s')=0,null,%s[indexOf(%s, '%s')])" , metrics_names_field , fieldName , metrics_values_field , metrics_names_field , fieldName ),
253- field , field , field , "" , "" , "" , METRICS_TYPE_COUNTER ,
254- ckcommon .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , "" , "" , "" , "" , "" ,
272+ field , field , field , mUnit , mUnit , mUnit , mType ,
273+ ckcommon .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , description , description , description , "" , "" ,
255274 )
256275 return metric , true
257276 } else if fieldSplit [0 ] == "tag" {
0 commit comments