Skip to content

Commit

Permalink
return vector selector from expressionWithNoMetricName
Browse files Browse the repository at this point in the history
  • Loading branch information
tizki committed Dec 6, 2023
1 parent 800a3af commit 083429c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pkg/validator/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ func (e expressionWithNoMetricName) String() string {

func (e expressionWithNoMetricName) Validate(rule rulefmt.Rule, _ *prometheus.Client) []error {
var errs []error
names, err := getExpressionMetricsNames(rule.Expr)
vectorsWithNames, err := getExpressionMetricsNames(rule.Expr)
if err != nil {
return []error{err}
}
for _, s := range names {
if s == "" {
errs = append(errs, fmt.Errorf("missing metric name for expression `%s`", rule.Expr))
for _, v := range vectorsWithNames {
if v.MetricName == "" {
errs = append(errs, fmt.Errorf("missing metric name for vector `%s`", v.Vector.String()))
}
}
return errs
Expand Down
16 changes: 11 additions & 5 deletions pkg/validator/expression_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,26 @@ func getExpressionSelectors(expr string) ([]string, error) {
return selectors, nil
}

func getExpressionMetricsNames(expr string) ([]string, error) {
type VectorSelectorWithMetricName struct {
Vector *parser.VectorSelector
MetricName string
}

func getExpressionMetricsNames(expr string) ([]VectorSelectorWithMetricName, error) {
promQl, err := parser.ParseExpr(expr)
if err != nil {
return []string{}, fmt.Errorf("failed to parse expression `%s`: %s", expr, err)
return []VectorSelectorWithMetricName{}, fmt.Errorf("failed to parse expression `%s`: %s", expr, err)
}
var names []string
var vectors []VectorSelectorWithMetricName
parser.Inspect(promQl, func(n parser.Node, ns []parser.Node) error {
switch v := n.(type) {
case *parser.VectorSelector:
names = append(names, getMetricNameFromLabels(v.LabelMatchers))
metricName := getMetricNameFromLabels(v.LabelMatchers)
vectors = append(vectors, VectorSelectorWithMetricName{Vector: v, MetricName: metricName})
}
return nil
})
return names, nil
return vectors, nil
}

func getMetricNameFromLabels(labels []*labels.Matcher) string {
Expand Down

0 comments on commit 083429c

Please sign in to comment.