diff --git a/pkg/unify-query/go.mod b/pkg/unify-query/go.mod index 6b2e6f7b5..49f0506da 100755 --- a/pkg/unify-query/go.mod +++ b/pkg/unify-query/go.mod @@ -3,6 +3,7 @@ module github.com/TencentBlueKing/bkmonitor-datalink/pkg/unify-query go 1.19 require ( + github.com/Knetic/govaluate v3.0.0+incompatible github.com/TencentBlueKing/bkmonitor-datalink/pkg/offline-data-archive v0.0.0-00010101000000-000000000000 github.com/TencentBlueKing/bkmonitor-datalink/pkg/utils v0.0.0-00010101000000-000000000000 github.com/VictoriaMetrics/metricsql v0.69.0 diff --git a/pkg/unify-query/go.sum b/pkg/unify-query/go.sum index c212f4113..b74bfa4f3 100644 --- a/pkg/unify-query/go.sum +++ b/pkg/unify-query/go.sum @@ -59,6 +59,8 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= +github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg= +github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= diff --git a/pkg/unify-query/query/structured/query_promql.go b/pkg/unify-query/query/structured/query_promql.go index 05be49990..a8dc791ae 100644 --- a/pkg/unify-query/query/structured/query_promql.go +++ b/pkg/unify-query/query/structured/query_promql.go @@ -14,6 +14,7 @@ import ( "math" "time" + "github.com/Knetic/govaluate" "github.com/prometheus/prometheus/promql/parser" ) @@ -248,7 +249,16 @@ func (sp *queryPromQLExpr) queryTs() (*QueryTs, error) { case *parser.StringLiteral: vargsList = append(vargsList, at.Val) case *parser.BinaryExpr: - vargsList = append(vargsList, at.String()) + expr, err := govaluate.NewEvaluableExpression(at.String()) + if err != nil { + return &QueryTs{}, err + } + result, err := expr.Evaluate(nil) + if err != nil { + return &QueryTs{}, err + } + + vargsList = append(vargsList, result) default: continue } diff --git a/pkg/unify-query/service/http/handler_test.go b/pkg/unify-query/service/http/handler_test.go index c5ff88818..ba61070cf 100644 --- a/pkg/unify-query/service/http/handler_test.go +++ b/pkg/unify-query/service/http/handler_test.go @@ -1436,7 +1436,7 @@ func TestStructAndPromQLConvert(t *testing.T) { "promq to struct with condition contains`": { queryStruct: true, promql: &structured.QueryPromQL{ - PromQL: `bkmonitor:metric{dim-contains=~"^(val-1|val-2|val-3)$",dim-req=~"val-1|val-2|val-3"} @ end()`, + PromQL: `bkmonitor:metric{dim_contains=~"^(val-1|val-2|val-3)$",dim_req=~"val-1|val-2|val-3"} @ end()`, }, query: &structured.QueryTs{ QueryList: []*structured.Query{ @@ -1447,7 +1447,7 @@ func TestStructAndPromQLConvert(t *testing.T) { Conditions: structured.Conditions{ FieldList: []structured.ConditionField{ { - DimensionName: "dim-contains", + DimensionName: "dim_contains", Value: []string{ "val-1", "val-2", @@ -1456,7 +1456,7 @@ func TestStructAndPromQLConvert(t *testing.T) { Operator: "contains", }, { - DimensionName: "dim-req", + DimensionName: "dim_req", Value: []string{ "val-1", "val-2", @@ -1515,7 +1515,7 @@ func TestStructAndPromQLConvert(t *testing.T) { "nodeIndex 3 with sum": { queryStruct: false, promql: &structured.QueryPromQL{ - PromQL: `increase(sum by (deployment_environment, result_table_id) (count_over_time(bkmonitor:5000575_bkapm_metric_tgf_server_gs_cn_idctest:__default__:trace_additional_duration_count{deployment_environment="g-5"})[1m])[2m:])`, + PromQL: `increase(sum by (deployment_environment, result_table_id) (bkmonitor:5000575_bkapm_metric_tgf_server_gs_cn_idctest:__default__:trace_additional_duration_count{deployment_environment="g-5"})[2m:])`, }, query: &structured.QueryTs{ QueryList: []*structured.Query{ @@ -1598,7 +1598,7 @@ func TestStructAndPromQLConvert(t *testing.T) { "predict_linear": { queryStruct: false, promql: &structured.QueryPromQL{ - PromQL: `predict_linear(metric[1h], 4*3600)`, + PromQL: `predict_linear(bkmonitor:metric[1h], 4*3600)`, }, query: &structured.QueryTs{ QueryList: []*structured.Query{ @@ -1611,7 +1611,7 @@ func TestStructAndPromQLConvert(t *testing.T) { Function: "predict_linear", Window: "1h0m0s", NodeIndex: 2, - VargsList: []interface{}{"4 * 3600"}, + VargsList: []interface{}{4 * 3600}, }, }, },