Skip to content

Commit

Permalink
feat: querier optimize dynamic metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochaoren1 committed Jan 16, 2025
1 parent af62899 commit a1edfca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
12 changes: 2 additions & 10 deletions server/querier/engine/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ type CHEngine struct {
DerivativeGroupBy []string
ORGID string
Language string
DynamicTag *common.Result
}

func init() {
Expand Down Expand Up @@ -1236,13 +1235,6 @@ func (e *CHEngine) TransFrom(froms sqlparser.TableExprs) error {
table = strings.ReplaceAll(table, "vtap_acl", "traffic_policy")
}
e.Table = table
// get dynamic tag
dynamicTag, err := tag.GetDynamicTagDescriptions(e.DB, e.Table, "", config.Cfg.Clickhouse.QueryCacheTTL, e.ORGID, config.Cfg.Clickhouse.UseQueryCache, context.Background(), nil)
if err != nil {
log.Error("Failed to get tag type dynamic metrics")
return err
}
e.DynamicTag = dynamicTag
// ext_metrics只有metrics表,使用virtual_table_name做过滤区分
if e.DB == "ext_metrics" {
table = "metrics"
Expand Down Expand Up @@ -1718,7 +1710,7 @@ func (e *CHEngine) parseSelectBinaryExpr(node sqlparser.Expr) (binary Function,
if fieldFunc != nil {
return fieldFunc, nil
}
metricStruct, ok := metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok := metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID)
if ok {
return &Field{Value: metricStruct.DBField}, nil
}
Expand Down Expand Up @@ -1831,7 +1823,7 @@ func (e *CHEngine) parseWhere(node sqlparser.Expr, w *Where, isCheck bool) (view
switch comparExpr.(type) {
case *sqlparser.ColName, *sqlparser.SQLVal:
whereTag := chCommon.ParseAlias(node.Left)
metricStruct, ok := metrics.GetMetrics(whereTag, e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok := metrics.GetMetrics(whereTag, e.DB, e.Table, e.ORGID)
if ok && metricStruct.Type != metrics.METRICS_TYPE_TAG {
whereTag = metricStruct.DBField
}
Expand Down
6 changes: 3 additions & 3 deletions server/querier/engine/clickhouse/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func GetAggFunc(name string, args []string, alias string, derivativeArgs []strin
if !ok {
return nil, 0, "", nil
}
metricStruct, ok := metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok := metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID)
if !ok {
return nil, 0, "", nil
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func GetTopKTrans(name string, args []string, alias string, e *CHEngine) (Statem
for i, field := range fields {

field = strings.Trim(field, "`")
metricStruct, ok = metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok = metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID)
if !ok || metricStruct.Type == metrics.METRICS_TYPE_ARRAY {
return nil, 0, "", nil
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func GetUniqTrans(name string, args []string, alias string, e *CHEngine) (Statem
var metricStruct *metrics.Metrics
for i, field := range fields {
field = strings.Trim(field, "`")
metricStruct, ok = metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok = metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID)
if !ok || metricStruct.Type == metrics.METRICS_TYPE_ARRAY {
return nil, 0, "", nil
}
Expand Down
7 changes: 4 additions & 3 deletions server/querier/engine/clickhouse/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewReplaceMetrics(dbField string, condition string) *Metrics {
}
}

func GetAggMetrics(field, db, table, orgID string, dynamicTag *common.Result) (*Metrics, bool) {
func GetAggMetrics(field, db, table, orgID string) (*Metrics, bool) {
field = strings.Trim(field, "`")
if field == COUNT_METRICS_NAME {
return &Metrics{
Expand All @@ -125,7 +125,7 @@ func GetAggMetrics(field, db, table, orgID string, dynamicTag *common.Result) (*
Table: table,
}, true
}
return GetMetrics(field, db, table, orgID, dynamicTag)
return GetMetrics(field, db, table, orgID)
}

func GetTagTypeMetrics(tagDescriptions *common.Result, newAllMetrics map[string]*Metrics, db, table, orgID string) error {
Expand Down Expand Up @@ -221,7 +221,7 @@ func GetTagTypeMetrics(tagDescriptions *common.Result, newAllMetrics map[string]
return nil
}

func GetMetrics(field, db, table, orgID string, dynamicTag *common.Result) (*Metrics, bool) {
func GetMetrics(field, db, table, orgID string) (*Metrics, bool) {
newAllMetrics := map[string]*Metrics{}
field = strings.Trim(field, "`")
// flow_tag database has no metrics
Expand Down Expand Up @@ -296,6 +296,7 @@ func GetMetrics(field, db, table, orgID string, dynamicTag *common.Result) (*Met
}
}
// Dynamic tag metrics
dynamicTag := tag.GetDynamicMetric(db, table, field)
GetTagTypeMetrics(dynamicTag, newAllMetrics, db, table, orgID)
metric, ok := newAllMetrics[field]
return metric, ok
Expand Down
2 changes: 1 addition & 1 deletion server/querier/engine/clickhouse/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func GetPrometheusAllTagTranslator(e *CHEngine) (string, string, error) {
}

func GetMetricsTag(name string, alias string, e *CHEngine) (Statement, error) {
metricStruct, ok := metrics.GetMetrics(strings.Trim(name, "`"), e.DB, e.Table, e.ORGID, e.DynamicTag)
metricStruct, ok := metrics.GetMetrics(strings.Trim(name, "`"), e.DB, e.Table, e.ORGID)
if !ok {
return nil, nil
}
Expand Down
39 changes: 36 additions & 3 deletions server/querier/engine/clickhouse/tag/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,39 @@ func GetDynamicTagDescriptions(db, table, rawSql, queryCacheTTL, orgID string, u
return
}

// Get dynamic metric
func GetDynamicMetric(db, table, metric string) (response *common.Result) {
response = &common.Result{
Columns: []interface{}{
"name", "client_name", "server_name", "display_name", "display_name_zh", "display_name_en", "type", "category",
"operators", "permissions", "description", "description_zh", "description_en", "related_tag", "deprecated", "not_supported_operators", "table",
},
Values: []interface{}{},
}
if table == "alert_event" {
return
}

for preffix, _ := range common.TRANS_MAP_ITEM_TAG {
if strings.HasPrefix(metric, preffix) {
response.Values = append(response.Values, []interface{}{
metric, metric, metric, metric, metric, metric, "map_item",
"Custom Tag", tagTypeToOperators["string"], []bool{true, true, true}, "", "", "", "", false, []string{}, "",
})
return
}
}

if strings.HasPrefix(metric, "tag.") || strings.HasPrefix(metric, "attribute.") {
response.Values = append(response.Values, []interface{}{
metric, metric, metric, metric, metric, metric, "map_item",
"Native Tag", tagTypeToOperators["string"], []bool{true, true, true}, "", "", "", "", false, []string{}, "",
})
return
}
return
}

func GetAlertEventTagDescriptions(staticTag, dynamicTag *common.Result) (response *common.Result, err error) {
response = &common.Result{
Columns: []interface{}{
Expand Down Expand Up @@ -1092,15 +1125,15 @@ func GetTagDescriptions(db, table, rawSql, queryCacheTTL, orgID string, useQuery
if err != nil {
return
}
DynamicResponse, err := GetDynamicTagDescriptions(db, table, rawSql, queryCacheTTL, orgID, useQueryCache, ctx, DebugInfo)
dynamicResponse, err := GetDynamicTagDescriptions(db, table, rawSql, queryCacheTTL, orgID, useQueryCache, ctx, DebugInfo)
if err != nil {
return
}
if table == "alert_event" {
return GetAlertEventTagDescriptions(staticResponse, DynamicResponse)
return GetAlertEventTagDescriptions(staticResponse, dynamicResponse)
}
response.Values = append(response.Values, staticResponse.Values...)
response.Values = append(response.Values, DynamicResponse.Values...)
response.Values = append(response.Values, dynamicResponse.Values...)
return
}

Expand Down

0 comments on commit a1edfca

Please sign in to comment.