Skip to content

Commit 61b08be

Browse files
committedFeb 7, 2025·
Add support for MetricsRelabel for BinaryExpr
In #676 support was added for pushing down BinaryExpr, but this support wasn't added to the metrics_relabel feature. This commit adds support thereby fixing #707 entirely.
1 parent bd44b8b commit 61b08be

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed
 

‎pkg/promclient/metric_relabel.go

+4
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ func (v *MetricsRelabelVisitor) Visit(node parser.Node, path []parser.Node) (w p
405405
case *parser.AggregateExpr:
406406
nodeTyped.Grouping = RewriteLabels(v.MetricsRelabelConfigs, nodeTyped.Grouping)
407407
case *parser.BinaryExpr:
408+
// If one is a literal; then it is safe to traverse
409+
if ExprIsLiteral(nodeTyped.LHS) || ExprIsLiteral(nodeTyped.RHS) {
410+
return v, nil
411+
}
408412
return nil, fmt.Errorf("metricsrelabelvisitor does not support BinaryExprs")
409413
}
410414

‎pkg/proxystorage/util.go ‎pkg/promclient/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package proxystorage
1+
package promclient
22

33
import (
44
"fmt"

‎pkg/proxystorage/proxy.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@ func (p *ProxyStorage) NodeReplacer(ctx context.Context, s *parser.EvalStmt, nod
347347

348348
// If there is a child that is an aggregator we cannot do anything (as they have their own
349349
// rules around combining). We'll skip this node and let a lower layer take this on
350-
aggFinder := &BooleanFinder{Func: isAgg}
351-
offsetFinder := &OffsetFinder{}
352-
vecFinder := &BooleanFinder{Func: isVectorSelector}
353-
timestampFinder := &BooleanFinder{Func: hasTimestamp}
350+
aggFinder := &promclient.BooleanFinder{Func: isAgg}
351+
offsetFinder := &promclient.OffsetFinder{}
352+
vecFinder := &promclient.BooleanFinder{Func: isVectorSelector}
353+
timestampFinder := &promclient.BooleanFinder{Func: hasTimestamp}
354354

355-
visitor := NewMultiVisitor([]parser.Visitor{aggFinder, offsetFinder, vecFinder, timestampFinder})
355+
visitor := promclient.NewMultiVisitor([]parser.Visitor{aggFinder, offsetFinder, vecFinder, timestampFinder})
356356

357357
if _, err := parser.Walk(ctx, visitor, s, node, nil, nil); err != nil {
358358
return nil, err
@@ -395,7 +395,7 @@ func (p *ProxyStorage) NodeReplacer(ctx context.Context, s *parser.EvalStmt, nod
395395
// that the time be the absolute time, whereas the API returns them based on the
396396
// range you ask for (with the offset being implicit)
397397
removeOffsetFn := func() error {
398-
_, err := parser.Walk(ctx, &OffsetRemover{}, s, node, nil, nil)
398+
_, err := parser.Walk(ctx, &promclient.OffsetRemover{}, s, node, nil, nil)
399399
return err
400400
}
401401

@@ -459,19 +459,19 @@ func (p *ProxyStorage) NodeReplacer(ctx context.Context, s *parser.EvalStmt, nod
459459

460460
return &parser.AggregateExpr{
461461
Op: parser.MAX,
462-
Expr: PreserveLabel(&parser.BinaryExpr{
462+
Expr: promclient.PreserveLabel(&parser.BinaryExpr{
463463
Op: parser.DIV,
464464
LHS: &parser.AggregateExpr{
465465
Op: parser.SUM,
466-
Expr: PreserveLabel(CloneExpr(n.Expr), model.MetricNameLabel, metricNameWorkaroundLabel),
466+
Expr: promclient.PreserveLabel(promclient.CloneExpr(n.Expr), model.MetricNameLabel, metricNameWorkaroundLabel),
467467
Param: n.Param,
468468
Grouping: replacedGrouping,
469469
Without: n.Without,
470470
},
471471

472472
RHS: &parser.AggregateExpr{
473473
Op: parser.COUNT,
474-
Expr: PreserveLabel(CloneExpr(n.Expr), model.MetricNameLabel, metricNameWorkaroundLabel),
474+
Expr: promclient.PreserveLabel(promclient.CloneExpr(n.Expr), model.MetricNameLabel, metricNameWorkaroundLabel),
475475
Param: n.Param,
476476
Grouping: replacedGrouping,
477477
Without: n.Without,
@@ -489,15 +489,15 @@ func (p *ProxyStorage) NodeReplacer(ctx context.Context, s *parser.EvalStmt, nod
489489
Op: parser.DIV,
490490
LHS: &parser.AggregateExpr{
491491
Op: parser.SUM,
492-
Expr: CloneExpr(n.Expr),
492+
Expr: promclient.CloneExpr(n.Expr),
493493
Param: n.Param,
494494
Grouping: n.Grouping,
495495
Without: n.Without,
496496
},
497497

498498
RHS: &parser.AggregateExpr{
499499
Op: parser.COUNT,
500-
Expr: CloneExpr(n.Expr),
500+
Expr: promclient.CloneExpr(n.Expr),
501501
Param: n.Param,
502502
Grouping: n.Grouping,
503503
Without: n.Without,
@@ -839,11 +839,11 @@ func (p *ProxyStorage) NodeReplacer(ctx context.Context, s *parser.EvalStmt, nod
839839
// Only valid if the other side is either `NumberLiteral` or `StringLiteral`
840840
this := n.LHS
841841
other := n.RHS
842-
literal := ExprIsLiteral(UnwrapExpr(this))
842+
literal := promclient.ExprIsLiteral(promclient.UnwrapExpr(this))
843843
if !literal {
844844
this = n.RHS
845845
other = n.LHS
846-
literal = ExprIsLiteral(UnwrapExpr(this))
846+
literal = promclient.ExprIsLiteral(promclient.UnwrapExpr(this))
847847
}
848848
// If one side is a literal lets check
849849
if literal {

0 commit comments

Comments
 (0)
Please sign in to comment.