Skip to content

Commit

Permalink
feat: promql 函数参数支持表达式 #1010158081115685124 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamcleren authored Dec 27, 2023
1 parent 516a320 commit 62b84f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/unify-query/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/unify-query/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 11 additions & 1 deletion pkg/unify-query/query/structured/query_promql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"math"
"time"

"github.com/Knetic/govaluate"
"github.com/prometheus/prometheus/promql/parser"
)

Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/unify-query/service/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
Expand All @@ -1456,7 +1456,7 @@ func TestStructAndPromQLConvert(t *testing.T) {
Operator: "contains",
},
{
DimensionName: "dim-req",
DimensionName: "dim_req",
Value: []string{
"val-1",
"val-2",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -1611,7 +1611,7 @@ func TestStructAndPromQLConvert(t *testing.T) {
Function: "predict_linear",
Window: "1h0m0s",
NodeIndex: 2,
VargsList: []interface{}{"4 * 3600"},
VargsList: []interface{}{4 * 3600},
},
},
},
Expand Down

0 comments on commit 62b84f7

Please sign in to comment.