From d29c1110dc1a9aa9518545ca94da332e3d471885 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 5 Sep 2024 18:56:20 +0800 Subject: [PATCH] fix(monitor): support or filter of query --- go.mod | 4 +- go.sum | 8 +-- .../influxdata/promql/v2/printer.go | 3 +- .../converter/translator/promql.go | 50 +++++++++++++------ vendor/modules.txt | 5 +- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index cc5c5f9f71b..034737cfc8e 100644 --- a/go.mod +++ b/go.mod @@ -66,7 +66,7 @@ require ( github.com/vishvananda/netns v0.0.5-0.20240412164733-9469873f4601 github.com/vmihailenco/msgpack v4.0.4+incompatible github.com/xuri/excelize/v2 v2.7.1 - github.com/zexi/influxql-to-metricsql v0.0.8 + github.com/zexi/influxql-to-metricsql v0.0.9 go.etcd.io/etcd/api/v3 v3.5.0 go.etcd.io/etcd/client/v3 v3.5.0 golang.org/x/crypto v0.8.0 @@ -273,3 +273,5 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.0.1 // indirect sigs.k8s.io/yaml v1.2.0 // indirect ) + +replace github.com/influxdata/promql/v2 => github.com/zexi/promql/v2 v2.12.1 diff --git a/go.sum b/go.sum index b3a722169b6..1e20abad3f0 100644 --- a/go.sum +++ b/go.sum @@ -431,8 +431,6 @@ github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28= github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/influxdata/influxql v1.1.0 h1:sPsaumLFRPMwR5QtD3Up54HXpNND8Eu7G1vQFmi3quQ= github.com/influxdata/influxql v1.1.0/go.mod h1:KpVI7okXjK6PRi3Z5B+mtKZli+R1DnZgb3N+tzevNgo= -github.com/influxdata/promql/v2 v2.12.0 h1:kXn3p0D7zPw16rOtfDR+wo6aaiH8tSMfhPwONTxrlEc= -github.com/influxdata/promql/v2 v2.12.0/go.mod h1:fxOPu+DY0bqCTCECchSRtWfc+0X19ybifQhZoQNF5D8= github.com/ipandtcp/godingtalk v0.0.0-20180410032244-ca3d6ac197fb/go.mod h1:3umLciE1jBnh2LgrL15R2zbEe+HVPRZXnlSjjb/dYiw= github.com/jaypipes/ghw v0.11.0 h1:i0pKvAM7eZk0KvLm9vzpcpDKTRnfR6AQ5pFkPVnYJXU= github.com/jaypipes/ghw v0.11.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= @@ -757,8 +755,10 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -github.com/zexi/influxql-to-metricsql v0.0.8 h1:DIzXVffcpWGgW6AHdK8exHXKyh6dFNXuBrYdFStyFIQ= -github.com/zexi/influxql-to-metricsql v0.0.8/go.mod h1:PyRRM+3Zrzzig6J4okYLeSv+/d+5GaL5ccBaUKQABNs= +github.com/zexi/influxql-to-metricsql v0.0.9 h1:IVT4w4OvYvUJvj5c1TvH0G/ueM4ZZsfHGMoXtzC5rQE= +github.com/zexi/influxql-to-metricsql v0.0.9/go.mod h1:JlC5FY+6De9ZPxG47G5DOgva8P9X1VaKS4ExzCmhSCc= +github.com/zexi/promql/v2 v2.12.1 h1:crHKpULdLLsBZ9b78Rg6qQkugzlk6BHeCj93tw/F5RU= +github.com/zexi/promql/v2 v2.12.1/go.mod h1:2UtzWZGmth95n2qdIZWpJ5yQ0cE5hEvz0dGsajI1Sqg= go.etcd.io/etcd/api/v3 v3.5.0 h1:GsV3S+OfZEOCNXdtNkBSR7kgLobAa/SO6tCxRa0GAYw= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0 h1:2aQv6F436YnN7I4VbI8PPYrBhu+SmrTaADcf8Mi/6PU= diff --git a/vendor/github.com/influxdata/promql/v2/printer.go b/vendor/github.com/influxdata/promql/v2/printer.go index 72ed4ecf7de..806774a3011 100644 --- a/vendor/github.com/influxdata/promql/v2/printer.go +++ b/vendor/github.com/influxdata/promql/v2/printer.go @@ -15,7 +15,6 @@ package promql import ( "fmt" - "sort" "strings" "time" @@ -193,6 +192,6 @@ func (node *VectorSelector) String() string { if len(labelStrings) == 0 { return fmt.Sprintf("%s%s", node.Name, offset) } - sort.Strings(labelStrings) + // sort.Strings(labelStrings) return fmt.Sprintf("%s{%s}%s", node.Name, strings.Join(labelStrings, ","), offset) } diff --git a/vendor/github.com/zexi/influxql-to-metricsql/converter/translator/promql.go b/vendor/github.com/zexi/influxql-to-metricsql/converter/translator/promql.go index 4fd626395b3..4215eb1d173 100644 --- a/vendor/github.com/zexi/influxql-to-metricsql/converter/translator/promql.go +++ b/vendor/github.com/zexi/influxql-to-metricsql/converter/translator/promql.go @@ -40,10 +40,13 @@ type promQL struct { timeRange *influxql.TimeRange fieldIsWildcard bool measurement string + labelsVisitor *labelsVisitor } func NewPromQL() Translator { - return &promQL{} + return &promQL{ + labelsVisitor: newLabelsVisitor(), + } } func (m *promQL) Translate(s influxql.Statement) (string, error) { @@ -93,7 +96,7 @@ func (m *promQL) translateField(s *influxql.SelectStatement, field *influxql.Fie } m.timeRange = timeRange - matchers, err := m.getLabels(cond) + matchers, err := m.getLabels(m.labelsVisitor, cond) if err != nil { return nil, errors.Wrap(err, "get matchers") } @@ -295,7 +298,15 @@ func (m promQL) generateExpr( } func (m promQL) formatExpr(expr promql.Expr) string { - return expr.String() + initialExpr := expr.String() + //fmt.Printf("---replaceLabels: %#v\n", m.labelsVisitor.replaceLabels) + //fmt.Printf("--src: %s\n", initialExpr) + if len(m.labelsVisitor.replaceLabels) > 0 && len(m.labelsVisitor.labels) > 1 { + for src, replace := range m.labelsVisitor.replaceLabels { + initialExpr = strings.ReplaceAll(initialExpr, src, replace) + } + } + return initialExpr } func newAggrExpr(name string, argType promql.ValueType, returnType promql.ValueType, restExpr promql.Expr) promql.Expr { @@ -483,17 +494,19 @@ func getCallVariable(c *influxql.Call) (string, error) { } type labelsVisitor struct { - err error - labels []*labels.Matcher - curKey string - curOp influxql.Token - curVal string + err error + labels []*labels.Matcher + curKey string + curOp influxql.Token + curVal string + replaceLabels map[string]string } func newLabelsVisitor() *labelsVisitor { return &labelsVisitor{ - err: nil, - labels: make([]*labels.Matcher, 0), + err: nil, + labels: make([]*labels.Matcher, 0), + replaceLabels: map[string]string{}, } } @@ -536,18 +549,24 @@ func (l *labelsVisitor) commitLabel() error { } func (l *labelsVisitor) Visit(node influxql.Node) influxql.Visitor { - //fmt.Printf("-- visit: %#v\n", node) + //fmt.Printf("-- visit: %s, %#v\n", node, node) if l.err != nil { log.Printf("error happend: %v, visting skipped", l.err) return l } switch expr := node.(type) { case *influxql.BinaryExpr: + if expr.Op != influxql.OR && expr.Op != influxql.AND { + l.curOp = expr.Op + } + l.Visit(expr.LHS) if expr.Op == influxql.OR { - l.err = errors.Errorf("%#v: OR is not suported yet.", expr) - return l + curLabel := l.labels[len(l.labels)-1] + key := fmt.Sprintf("%s,", curLabel.String()) + l.replaceLabels[key] = fmt.Sprintf("%s or ", curLabel.String()) } - l.curOp = expr.Op + l.Visit(expr.RHS) + return nil case *influxql.VarRef: l.curKey = expr.Val case *influxql.StringLiteral: @@ -564,11 +583,10 @@ func (l *labelsVisitor) Visit(node influxql.Node) influxql.Visitor { return l } -func (m promQL) getLabels(cond influxql.Expr) ([]*labels.Matcher, error) { +func (m promQL) getLabels(v *labelsVisitor, cond influxql.Expr) ([]*labels.Matcher, error) { if cond == nil { return nil, nil } - v := newLabelsVisitor() influxql.Walk(v, cond) return v.Labels(), v.Error() } diff --git a/vendor/modules.txt b/vendor/modules.txt index 0fde34c60a9..f13aeb457e8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -542,7 +542,7 @@ github.com/imdario/mergo ## explicit github.com/influxdata/influxql github.com/influxdata/influxql/internal -# github.com/influxdata/promql/v2 v2.12.0 +# github.com/influxdata/promql/v2 v2.12.0 => github.com/zexi/promql/v2 v2.12.1 ## explicit; go 1.12 github.com/influxdata/promql/v2 github.com/influxdata/promql/v2/pkg/labels @@ -983,7 +983,7 @@ github.com/xuri/nfp # github.com/yusufpapurcu/wmi v1.2.2 ## explicit; go 1.16 github.com/yusufpapurcu/wmi -# github.com/zexi/influxql-to-metricsql v0.0.8 +# github.com/zexi/influxql-to-metricsql v0.0.9 ## explicit; go 1.18 github.com/zexi/influxql-to-metricsql/converter github.com/zexi/influxql-to-metricsql/converter/translator @@ -1652,3 +1652,4 @@ yunion.io/x/sqlchemy/backends/sqlite # yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c ## explicit; go 1.12 yunion.io/x/structarg +# github.com/influxdata/promql/v2 => github.com/zexi/promql/v2 v2.12.1