-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.go
75 lines (68 loc) · 1.88 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/prometheus/prometheus/model/labels"
promql "github.com/prometheus/prometheus/promql/parser"
log "github.com/sirupsen/logrus"
)
func (v *visitor) addFilter(m []*labels.Matcher) []*labels.Matcher {
m = append(m, v.filter...)
log.WithFields(log.Fields{"matchers": m}).Debug("addFilter")
return m
}
type visitor struct {
filter []*labels.Matcher
}
func newVisitor(filter string) (*visitor, error) {
f, err := promql.ParseMetricSelector(filter)
if err != nil {
return nil, err
}
v := &visitor{
filter: f,
}
return v, nil
}
func (v *visitor) Visit(node promql.Node, path []promql.Node) (promql.Visitor, error) {
if node == nil {
return v, nil
}
log.WithFields(log.Fields{"node": node}).Debug("Visit")
switch n := node.(type) {
case *promql.VectorSelector:
n.LabelMatchers = v.addFilter(n.LabelMatchers)
case *promql.MatrixSelector:
// VectorSelector is an Expr and is treated as a child, therefore
// we do not have to explicitly handle it here.
case *promql.AggregateExpr:
case *promql.BinaryExpr:
case *promql.Call:
case promql.Expressions:
case *promql.NumberLiteral:
case *promql.StringLiteral:
case *promql.SubqueryExpr:
case *promql.ParenExpr:
case *promql.UnaryExpr:
default:
log.Warnf("Unknown type %T", n)
}
return v, nil
}
func addQueryFilter(filter, query string) (string, error) {
expr, err := promql.ParseExpr(query)
if err != nil {
log.WithFields(log.Fields{"err": err}).Debug("ParseExpr")
return "", fmt.Errorf("promql parse error: %s", err)
}
log.Debug(promql.Tree(expr))
v, err := newVisitor(filter)
if err != nil {
log.WithFields(log.Fields{"err": err}).Warn("failed to create visitor")
return "", err
}
var path []promql.Node
promql.Walk(v, expr, path)
newQuery := expr.String()
log.WithFields(log.Fields{"origQuery": query, "newQuery": newQuery}).Debug("rewrote query")
return newQuery, nil
}