fix(monitor): support or filter of query (#21170)

This commit is contained in:
Zexi Li
2024-09-05 20:10:24 +08:00
committed by GitHub
parent e3598bceab
commit 5c70df90d7
5 changed files with 46 additions and 26 deletions

4
go.mod
View File

@@ -70,7 +70,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
@@ -304,3 +304,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

8
go.sum
View File

@@ -471,8 +471,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=
@@ -837,8 +835,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=

View File

@@ -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)
}

View File

@@ -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 {
l.err = errors.Errorf("%#v: OR is not suported yet.", expr)
return l
if expr.Op != influxql.OR && expr.Op != influxql.AND {
l.curOp = expr.Op
}
l.curOp = expr.Op
l.Visit(expr.LHS)
if expr.Op == influxql.OR {
curLabel := l.labels[len(l.labels)-1]
key := fmt.Sprintf("%s,", curLabel.String())
l.replaceLabels[key] = fmt.Sprintf("%s or ", curLabel.String())
}
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()
}

5
vendor/modules.txt vendored
View File

@@ -668,7 +668,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
@@ -1174,7 +1174,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
@@ -1853,3 +1853,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