Skip to content

Commit

Permalink
Supports same duration format in LogQL as Prometheus
Browse files Browse the repository at this point in the history
Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored and slim-bean committed Dec 3, 2019
1 parent df89859 commit f7ee1c7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ type logRange struct {
interval time.Duration
}

func mustNewRange(left LogSelectorExpr, interval time.Duration) *logRange {
func newLogRange(left LogSelectorExpr, interval time.Duration) *logRange {
return &logRange{
left: left,
interval: interval,
Expand Down
2 changes: 1 addition & 1 deletion pkg/logql/expr.y
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ logExpr:
;


logRangeExpr: logExpr DURATION { $$ = mustNewRange($1, $2) };
logRangeExpr: logExpr DURATION { $$ = newLogRange($1, $2) };


rangeAggregationExpr: rangeOp OPEN_PARENTHESIS logRangeExpr CLOSE_PARENTHESIS { $$ = newRangeAggregationExpr($3,$1) };
Expand Down
2 changes: 1 addition & 1 deletion pkg/logql/expr.y.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/logql/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strconv"
"text/scanner"
"time"

"github.com/prometheus/common/model"
)

var tokens = map[string]int{
Expand Down Expand Up @@ -64,12 +66,12 @@ func (l *lexer) Lex(lval *exprSymType) int {
d := ""
for r := l.Next(); r != scanner.EOF; r = l.Next() {
if string(r) == "]" {
i, err := time.ParseDuration(d)
i, err := model.ParseDuration(d)
if err != nil {
l.Error(err.Error())
return 0
}
lval.duration = i
lval.duration = time.Duration(i)
return DURATION
}
d += string(r)
Expand Down
32 changes: 31 additions & 1 deletion pkg/logql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ func TestParse(t *testing.T) {
operation: "rate",
},
},
{
in: `rate({ foo !~ "bar" }[5d])`,
exp: &rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 5 * 24 * time.Hour,
},
operation: "rate",
},
},
{
in: `count_over_time({ foo !~ "bar" }[1w])`,
exp: &rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 7 * 24 * time.Hour,
},
operation: "count_over_time",
},
},
{
in: `sum(rate({ foo !~ "bar" }[5h]))`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
Expand All @@ -69,6 +89,16 @@ func TestParse(t *testing.T) {
operation: "rate",
}, "sum", nil, nil),
},
{
in: `sum(rate({ foo !~ "bar" }[1y]))`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 365 * 24 * time.Hour,
},
operation: "rate",
}, "sum", nil, nil),
},
{
in: `avg(count_over_time({ foo !~ "bar" }[5h])) by (bar,foo)`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
Expand Down Expand Up @@ -149,7 +179,7 @@ func TestParse(t *testing.T) {
{
in: `rate({ foo !~ "bar" }[5minutes])`,
err: ParseError{
msg: "time: unknown unit minutes in duration 5minutes",
msg: `not a valid duration string: "5minutes"`,
line: 0,
col: 22,
},
Expand Down

0 comments on commit f7ee1c7

Please sign in to comment.