Files
grafana/pkg/tsdb/prometheus/models/scope.go
T
Kyle Brandt 43d0664340 Prometheus: (Experimental) Inject label matchers into queries (also change drone to fix ARM rpm build and Update Swagger) (#81396)
- Feature Toggle is `promQLScope`.
 - Query property is:

"scope": {
  "matchers": "{job=~\".*\"}"
 }

Misc:
 - Also updates drone GO version to address ARM bug https://github.com/golang/go/issues/58425
 - Also updates Swagger defs that were causing builds to fail

---------

Co-authored-by: Kevin Minehart <kmineh0151@gmail.com>
2024-01-29 22:22:17 +02:00

53 lines
1.2 KiB
Go

package models
import (
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
)
func ApplyQueryScope(rawExpr string, matchers []*labels.Matcher) (string, error) {
expr, err := parser.ParseExpr(rawExpr)
if err != nil {
return "", err
}
matcherNamesToIdx := make(map[string]int, len(matchers))
for i, matcher := range matchers {
if matcher == nil {
continue
}
matcherNamesToIdx[matcher.Name] = i
}
parser.Inspect(expr, func(node parser.Node, nodes []parser.Node) error {
switch v := node.(type) {
case *parser.VectorSelector:
found := make([]bool, len(matchers))
for _, matcher := range v.LabelMatchers {
if matcher == nil || matcher.Name == "__name__" { // const prob
continue
}
if _, ok := matcherNamesToIdx[matcher.Name]; ok {
found[matcherNamesToIdx[matcher.Name]] = true
newM := matchers[matcherNamesToIdx[matcher.Name]]
matcher.Name = newM.Name
matcher.Type = newM.Type
matcher.Value = newM.Value
}
}
for i, f := range found {
if f {
continue
}
v.LabelMatchers = append(v.LabelMatchers, matchers[i])
}
return nil
default:
return nil
}
})
return expr.String(), nil
}