Prometheus: Introduce prometheus backend library (#83952)
* Move files to prometheus-library * refactor core prometheus to use prometheus-library * modify client transport options * mock * have a type * import aliases * rename * call the right method * remove unrelated test from the library * update codeowners * go work sync * update go.work.sum * make swagger-clean && make openapi3-gen * add promlib to makefile * remove clilogger * Export the function * update unit test * add prometheus_test.go * fix mock type * use mapUtil from grafana-plugin-sdk-go
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user