upgrade prometheus client-go
Signed-off-by: zackzhangkai <zackzhang@yunify.com>
This commit is contained in:
@@ -17,10 +17,9 @@ limitations under the License.
|
||||
package expressions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/metric"
|
||||
"github.com/prometheus-community/prom-label-proxy/injectproxy"
|
||||
"github.com/prometheus/prometheus/pkg/labels"
|
||||
"github.com/prometheus/prometheus/promql/parser"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -28,87 +27,19 @@ func init() {
|
||||
}
|
||||
|
||||
func labelReplace(input, ns string) (string, error) {
|
||||
root, err := promql.ParseExpr(input)
|
||||
root, err := parser.ParseExpr(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
setRecursive(root, ns)
|
||||
err = injectproxy.NewEnforcer(&labels.Matcher{
|
||||
Type: labels.MatchEqual,
|
||||
Name: "namespace",
|
||||
Value: ns,
|
||||
}).EnforceNode(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return root.String(), nil
|
||||
}
|
||||
|
||||
// Inspired by https://github.com/prometheus-community/prom-label-proxy
|
||||
func setRecursive(node promql.Node, namespace string) (err error) {
|
||||
switch n := node.(type) {
|
||||
case *promql.EvalStmt:
|
||||
if err := setRecursive(n.Expr, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case promql.Expressions:
|
||||
for _, e := range n {
|
||||
if err := setRecursive(e, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *promql.AggregateExpr:
|
||||
if err := setRecursive(n.Expr, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case *promql.BinaryExpr:
|
||||
if err := setRecursive(n.LHS, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := setRecursive(n.RHS, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case *promql.Call:
|
||||
if err := setRecursive(n.Args, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case *promql.ParenExpr:
|
||||
if err := setRecursive(n.Expr, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case *promql.UnaryExpr:
|
||||
if err := setRecursive(n.Expr, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
case *promql.NumberLiteral, *promql.StringLiteral:
|
||||
// nothing to do
|
||||
case *promql.MatrixSelector:
|
||||
n.LabelMatchers = enforceLabelMatchers(n.LabelMatchers, namespace)
|
||||
case *promql.VectorSelector:
|
||||
n.LabelMatchers = enforceLabelMatchers(n.LabelMatchers, namespace)
|
||||
default:
|
||||
return fmt.Errorf("promql.Walk: unhandled node type %T", node)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func enforceLabelMatchers(matchers metric.LabelMatchers, namespace string) metric.LabelMatchers {
|
||||
var found bool
|
||||
for i, m := range matchers {
|
||||
if m.Name == "namespace" {
|
||||
matchers[i] = &metric.LabelMatcher{
|
||||
Name: "namespace",
|
||||
Type: metric.Equal,
|
||||
Value: model.LabelValue(namespace),
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
matchers = append(matchers, &metric.LabelMatcher{
|
||||
Name: "namespace",
|
||||
Type: metric.Equal,
|
||||
Value: model.LabelValue(namespace),
|
||||
})
|
||||
}
|
||||
return matchers
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func NewPrometheus(options *Options) (monitoring.Interface, error) {
|
||||
func (p prometheus) GetMetric(expr string, ts time.Time) monitoring.Metric {
|
||||
var parsedResp monitoring.Metric
|
||||
|
||||
value, err := p.client.Query(context.Background(), expr, ts)
|
||||
value, _, err := p.client.Query(context.Background(), expr, ts)
|
||||
if err != nil {
|
||||
parsedResp.Error = err.Error()
|
||||
} else {
|
||||
@@ -62,7 +62,7 @@ func (p prometheus) GetMetricOverTime(expr string, start, end time.Time, step ti
|
||||
Step: step,
|
||||
}
|
||||
|
||||
value, err := p.client.QueryRange(context.Background(), expr, timeRange)
|
||||
value, _, err := p.client.QueryRange(context.Background(), expr, timeRange)
|
||||
|
||||
var parsedResp monitoring.Metric
|
||||
if err != nil {
|
||||
@@ -86,7 +86,7 @@ func (p prometheus) GetNamedMetrics(metrics []string, ts time.Time, o monitoring
|
||||
go func(metric string) {
|
||||
parsedResp := monitoring.Metric{MetricName: metric}
|
||||
|
||||
value, err := p.client.Query(context.Background(), makeExpr(metric, *opts), ts)
|
||||
value, _, err := p.client.Query(context.Background(), makeExpr(metric, *opts), ts)
|
||||
if err != nil {
|
||||
parsedResp.Error = err.Error()
|
||||
} else {
|
||||
@@ -125,7 +125,7 @@ func (p prometheus) GetNamedMetricsOverTime(metrics []string, start, end time.Ti
|
||||
go func(metric string) {
|
||||
parsedResp := monitoring.Metric{MetricName: metric}
|
||||
|
||||
value, err := p.client.QueryRange(context.Background(), makeExpr(metric, *opts), timeRange)
|
||||
value, _, err := p.client.QueryRange(context.Background(), makeExpr(metric, *opts), timeRange)
|
||||
if err != nil {
|
||||
parsedResp.Error = err.Error()
|
||||
} else {
|
||||
@@ -176,7 +176,7 @@ func (p prometheus) GetMetadata(namespace string) []monitoring.Metadata {
|
||||
func (p prometheus) GetMetricLabelSet(expr string, start, end time.Time) []map[string]string {
|
||||
var res []map[string]string
|
||||
|
||||
labelSet, err := p.client.Series(context.Background(), []string{expr}, start, end)
|
||||
labelSet, _, err := p.client.Series(context.Background(), []string{expr}, start, end)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return []map[string]string{}
|
||||
|
||||
Reference in New Issue
Block a user