Skip to content

Commit

Permalink
Fix logQL metric with grouping losing opMetric (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak authored Oct 15, 2024
1 parent 9a6dbb7 commit 0c59e41
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 325 deletions.
48 changes: 48 additions & 0 deletions logql/v2/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,30 @@ func (l *LogRangeQueryExpr) Walk(fn WalkFn) {
l.left.Walk(fn)
}

type ParenthesisLogRangeQueryExpr struct {
defaultLogQLExpr
inner LogSelectorExpr
}

func (e *ParenthesisLogRangeQueryExpr) Matchers() []*labels.Matcher {
return e.inner.Matchers()
}

func (e *ParenthesisLogRangeQueryExpr) Walk(fn WalkFn) {
fn(e)
e.inner.Walk(fn)
}

func (e *ParenthesisLogRangeQueryExpr) String() string {
return fmt.Sprintf("(%s)", e.inner)
}

func newParenthesisLogRangeQueryExpr(expr LogSelectorExpr) *ParenthesisLogRangeQueryExpr {
return &ParenthesisLogRangeQueryExpr{
inner: expr,
}
}

type LogMetricExpr struct {
defaultLogQLExpr // nolint:unused
left LogSelectorExpr
Expand Down Expand Up @@ -794,6 +818,30 @@ func (l *LogMetricExpr) Walk(fn WalkFn) {
}
}

type ParenthesisLogMetricExpr struct {
defaultLogQLExpr
inner LogMetricSampleExpr
}

func (e *ParenthesisLogMetricExpr) Selector() LogSelectorExpr {
return e.inner.Selector()
}

func (e *ParenthesisLogMetricExpr) Walk(fn WalkFn) {
fn(e)
e.inner.Walk(fn)
}

func (e *ParenthesisLogMetricExpr) String() string {
return fmt.Sprintf("(%s)", e.inner)
}

func newParenthesisLogMetricExpr(expr LogMetricSampleExpr) *ParenthesisLogMetricExpr {
return &ParenthesisLogMetricExpr{
inner: expr,
}
}

type grouping struct {
without bool
groups []string
Expand Down
16 changes: 16 additions & 0 deletions logql/v2/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func Test_AstWalker_AppendMatcher(t *testing.T) {
) by(second,first)`,
output: `avg(label_replace(count_over_time({first="value", second="next"}[5h]),"value","$1$2","first","(.*).(.*)")) by(second,first)`,
},
{
input: `quantile_over_time(0.5,{app="foo"}[15s]) by (app)`,
output: `quantile_over_time(0.5,{app="foo", second="next"}[15s]) by(app)`,
},
{
input: `avg_over_time({app="foo"}[15s]) by (app)`,
output: `avg_over_time({app="foo", second="next"}[15s]) by(app)`,
},
{
input: `(count_over_time({first="value"}[10h]))`,
output: `(count_over_time({first="value", second="next"}[10h]))`,
},
{
input: `topk(25,(count_over_time({first="value"}[10h])))`,
output: `topk(25,(count_over_time({first="value", second="next"}[10h])))`,
},
}
for _, tc := range tc {
expr, err := ParseExpr(tc.input)
Expand Down
6 changes: 4 additions & 2 deletions logql/v2/expr.y
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ logRangeQueryExpr:
| OPEN_PARENTHESIS selector logPipelineExpr CLOSE_PARENTHESIS RANGE { $$ = newLogRangeQueryExpr(newLogQueryExpr(newStreamMatcherExpr($2), $3), $5, nil, true) }
| OPEN_PARENTHESIS selector RANGE logPipelineExpr CLOSE_PARENTHESIS grouping { $$ = newLogRangeQueryExpr(newLogQueryExpr(newStreamMatcherExpr($2), $4), $3, $6, false) }
| logRangeQueryExpr error
| OPEN_PARENTHESIS logRangeQueryExpr CLOSE_PARENTHESIS { $$ = newParenthesisLogRangeQueryExpr($2) }
;

logMetricExpr:
Expand All @@ -176,8 +177,8 @@ logMetricExpr:
| metricOp OPEN_PARENTHESIS NUMBER COMMA logRangeQueryExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $5, $1, $3, $7, false, nil, nil) }
| metricOp OPEN_PARENTHESIS NUMBER COMMA logRangeQueryExpr logOffsetExpr CLOSE_PARENTHESIS { $$ = newLogMetricExpr(nil, $5, $1, $3, nil, false, nil, $6) }
| metricOp OPEN_PARENTHESIS NUMBER COMMA logRangeQueryExpr logOffsetExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $5, $1, $3, $8, false, nil, $6) }
| metricOp OPEN_PARENTHESIS logRangeQueryExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $3, "", "", $5, false, nil, nil) }
| metricOp OPEN_PARENTHESIS logRangeQueryExpr logOffsetExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $3, "", "", $6, false, nil, $4) }
| metricOp OPEN_PARENTHESIS logRangeQueryExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $3, $1, "", $5, false, nil, nil) }
| metricOp OPEN_PARENTHESIS logRangeQueryExpr logOffsetExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr(nil, $3, $1, "", $6, false, nil, $4) }
| metricOp OPEN_PARENTHESIS logMetricExpr CLOSE_PARENTHESIS grouping { $$ = newLogMetricExpr($3, nil, $1, "", $5, false, nil, nil) }
| metricOp OPEN_PARENTHESIS logMetricExpr CLOSE_PARENTHESIS { $$ = newLogMetricExpr($3, nil, $1, "", nil, false, nil, nil) }
| metricOp OPEN_PARENTHESIS NUMBER COMMA logMetricExpr CLOSE_PARENTHESIS { $$ = newLogMetricExpr($5, nil, $1, $3, nil, false, nil, nil) }
Expand All @@ -195,6 +196,7 @@ logMetricExpr:
| metricOp grouping OPEN_PARENTHESIS logBinaryOpExpr CLOSE_PARENTHESIS { $$ = newLogMetricExpr($4, nil, $1, "", $2, true, nil, nil) }
| LABEL_REPLACE OPEN_PARENTHESIS logBinaryOpExpr COMMA STRING COMMA STRING COMMA STRING COMMA STRING CLOSE_PARENTHESIS { $$ = newLogMetricExpr($3, nil, OpLabelReplace, "", nil, false, []string{$5,$7,$9,$11}, nil) }
| metricOp OPEN_PARENTHESIS NUMBER CLOSE_PARENTHESIS { $$ = newLogMetricExpr(newVectorExpr($3), nil, OpTypeVector, "", nil, false, nil, nil) }
| OPEN_PARENTHESIS logMetricExpr CLOSE_PARENTHESIS { $$ = newParenthesisLogMetricExpr($2) }
;

logBinaryOpExpr:
Expand Down
Loading

0 comments on commit 0c59e41

Please sign in to comment.