@@ -22,6 +22,7 @@ import (
2222 "slices"
2323 "strings"
2424
25+ simplejson "github.com/bitly/go-simplejson"
2526 ctlcommon "github.com/deepflowio/deepflow/server/controller/common"
2627 "github.com/deepflowio/deepflow/server/querier/config"
2728 "github.com/deepflowio/deepflow/server/querier/engine/clickhouse/client"
@@ -32,7 +33,7 @@ var EXT_METRICS = map[string]*Metrics{}
3233
3334func GetExtMetrics (db , table , where , queryCacheTTL , orgID string , useQueryCache bool , ctx context.Context ) (map [string ]* Metrics , error ) {
3435 loadMetrics := make (map [string ]* Metrics )
35- if slices .Contains ([]string {common .DB_NAME_DEEPFLOW_ADMIN , common .DB_NAME_DEEPFLOW_TENANT , common .DB_NAME_APPLICATION_LOG , common .DB_NAME_EXT_METRICS }, db ) || slices .Contains ([]string {common .TABLE_NAME_L7_FLOW_LOG , common . TABLE_NAME_EVENT , common . TABLE_NAME_FILE_EVENT }, table ) {
36+ if slices .Contains ([]string {common .DB_NAME_DEEPFLOW_ADMIN , common .DB_NAME_DEEPFLOW_TENANT , common .DB_NAME_APPLICATION_LOG , common .DB_NAME_EXT_METRICS }, db ) || slices .Contains ([]string {common .TABLE_NAME_L7_FLOW_LOG }, table ) {
3637 externalChClient := client.Client {
3738 Host : config .Cfg .Clickhouse .Host ,
3839 Port : config .Cfg .Clickhouse .Port ,
@@ -59,27 +60,42 @@ func GetExtMetrics(db, table, where, queryCacheTTL, orgID string, useQueryCache
5960 log .Error (err )
6061 return nil , err
6162 }
63+ var customMetrics map [string ]* simplejson.Json
64+ if config .ControllerCfg .DFWebService .Enabled {
65+ customMetrics , err = common .GetCustomMetrics (orgID )
66+ if err != nil {
67+ log .Error (err .Error ())
68+ }
69+ }
6270 for i , value := range externalMetricFloatRst .Values {
6371 tagName := value .([]interface {})[0 ]
6472 tableName := value .([]interface {})[1 ].(string )
6573 externalTag := tagName .(string )
6674 metrics_names_field , metrics_values_field := METRICS_ARRAY_NAME_MAP [db ][0 ], METRICS_ARRAY_NAME_MAP [db ][1 ]
6775 dbField := fmt .Sprintf ("if(indexOf(%s, '%s')=0, null, %s[indexOf(%s, '%s')])" , metrics_names_field , externalTag , metrics_values_field , metrics_names_field , externalTag )
76+ var mUnit , description string
77+ mType := METRICS_TYPE_COUNTER
6878 metricName := fmt .Sprintf ("metrics.%s" , externalTag )
79+ displayName := metricName
80+ customMetric , ok := customMetrics [fmt .Sprintf ("%s.%s.%s" , db , tableName , metricName )]
81+ if ok {
82+ mType = customMetric .Get ("TYPE" ).MustInt ()
83+ mUnit = customMetric .Get ("UNIT" ).MustString ()
84+ displayName = customMetric .Get ("DISPLAY_NAME" ).MustString ()
85+ description = customMetric .Get ("DESCRIPTION" ).MustString ()
86+ }
6987 lm := NewMetrics (
70- i , dbField , metricName , metricName , metricName , "" , "" , "" , METRICS_TYPE_COUNTER ,
71- common .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , tableName , "" , "" , "" , "" , "" ,
88+ i , dbField , displayName , displayName , displayName , mUnit , mUnit , mUnit , mType ,
89+ common .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , tableName , description , description , description , "" , "" ,
7290 )
7391 loadMetrics [fmt .Sprintf ("%s-%s" , metricName , tableName )] = lm
7492 }
75- if ! slices .Contains ([]string {common .TABLE_NAME_EVENT , common .TABLE_NAME_FILE_EVENT }, table ) {
76- lm := NewMetrics (
77- len (loadMetrics ), "metrics" ,
78- "metrics" , "metrics" , "metrics" , "" , "" , "" , METRICS_TYPE_ARRAY ,
79- common .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , "" , "" , "" , "" , "" ,
80- )
81- loadMetrics [fmt .Sprintf ("%s-%s" , "metrics" , table )] = lm
82- }
93+ lm := NewMetrics (
94+ len (loadMetrics ), "metrics" ,
95+ "metrics" , "metrics" , "metrics" , "" , "" , "" , METRICS_TYPE_ARRAY ,
96+ common .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , "" , "" , "" , "" , "" ,
97+ )
98+ loadMetrics [fmt .Sprintf ("%s-%s" , "metrics" , table )] = lm
8399
84100 // native metrics
85101 if config .ControllerCfg .DFWebService .Enabled {
@@ -101,8 +117,17 @@ func GetExtMetrics(db, table, where, queryCacheTTL, orgID string, useQueryCache
101117 if fieldType != common .NATIVE_FIELD_TYPE_METRIC {
102118 continue
103119 }
120+ var mUnit string
121+ mType := METRICS_TYPE_COUNTER
122+ customMetric , ok := customMetrics [fmt .Sprintf ("%s.%s.%s" , db , table , nativeMetric )]
123+ if ok {
124+ mType = customMetric .Get ("TYPE" ).MustInt ()
125+ mUnit = customMetric .Get ("UNIT" ).MustString ()
126+ displayName = customMetric .Get ("DISPLAY_NAME" ).MustString ()
127+ description = customMetric .Get ("DESCRIPTION" ).MustString ()
128+ }
104129 lm := NewMetrics (
105- len (loadMetrics ), nativeMetric , displayName , displayName , displayName , "" , "" , "" , METRICS_TYPE_COUNTER ,
130+ len (loadMetrics ), nativeMetric , displayName , displayName , displayName , mUnit , mUnit , mUnit , mType ,
106131 common .NATIVE_FIELD_CATEGORY_METRICS , []bool {true , true , true }, "" , table , description , description , description , "" , "" ,
107132 )
108133 loadMetrics [fmt .Sprintf ("%s-%s" , nativeMetric , table )] = lm
0 commit comments