Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse duration expressions in accordance with promql #5275

Merged
merged 6 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/logql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Test_SampleExpr_String(t *testing.T) {
for _, tc := range []string{
`rate( ( {job="mysql"} |="error" !="timeout" ) [10s] )`,
`absent_over_time( ( {job="mysql"} |="error" !="timeout" ) [10s] )`,
`absent_over_time( ( {job="mysql"} |="error" !="timeout" ) [10s] offset 10m )`,
`absent_over_time( ( {job="mysql"} |="error" !="timeout" ) [10s] offset 10d )`,
`sum without(a) ( rate ( ( {job="mysql"} |="error" !="timeout" ) [10s] ) )`,
`sum by(a) (rate( ( {job="mysql"} |="error" !="timeout" ) [10s] ) )`,
`sum(count_over_time({job="mysql"}[5m]))`,
Expand All @@ -78,7 +78,7 @@ func Test_SampleExpr_String(t *testing.T) {
`sum(count_over_time({job="mysql"} | pattern "<foo> bar <buzz>" | json [5m]))`,
`sum(count_over_time({job="mysql"} | unpack | json [5m]))`,
`sum(count_over_time({job="mysql"} | regexp "(?P<foo>foo|bar)" [5m]))`,
`sum(count_over_time({job="mysql"} | regexp "(?P<foo>foo|bar)" [5m] offset 10m))`,
`sum(count_over_time({job="mysql"} | regexp "(?P<foo>foo|bar)" [5m] offset 10y))`,
dannykopping marked this conversation as resolved.
Show resolved Hide resolved
`topk(10,sum(rate({region="us-east1"}[5m])) by (name))`,
`topk by (name)(10,sum(rate({region="us-east1"}[5m])))`,
`avg( rate( ( {job="nginx"} |= "GET" ) [10s] ) ) by (region)`,
Expand Down
28 changes: 25 additions & 3 deletions pkg/logql/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,43 @@ func tryScanDuration(number string, l *scanner.Scanner) (time.Duration, bool) {
return 0, false
}
// we've found more characters before a whitespace or the end
d, err := time.ParseDuration(sb.String())
durationString := sb.String()
duration, err := parseDuration(durationString)
if err != nil {
return 0, false
}

// we need to consume the scanner, now that we know this is a duration.
for i := 0; i < consumed; i++ {
_ = l.Next()
}
return d, true

return duration, true
}

func parseDuration(d string) (time.Duration, error) {
var duration time.Duration
// Try to parse promql style durations first, to ensure that we support the same duration
// units as promql
prometheusDuration, err := model.ParseDuration(d)
if err != nil {
// Fall back to standard library's time.ParseDuration if a promql style
// duration couldn't be parsed.
duration, err = time.ParseDuration(d)
if err != nil {
return 0, err
}
} else {
duration = time.Duration(prometheusDuration)
}

return duration, nil
}

func isDurationRune(r rune) bool {
// "ns", "us" (or "µs"), "ms", "s", "m", "h".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this comment in sync with what we have in the switch case. While we are at it, can you please also order it the same manner as the switch case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Agreed, and done.

switch r {
case 'n', 's', 'u', 'm', 'h', 'µ':
case 'n', 's', 'u', 'm', 'h', 'µ', 'y', 'w', 'd':
return true
default:
return false
Expand Down
29 changes: 29 additions & 0 deletions pkg/logql/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"
"text/scanner"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -126,3 +127,31 @@ func Test_isFunction(t *testing.T) {
})
}
}

func Test_parseDuration(t *testing.T) {
const MICROSECOND = 1000 * time.Nanosecond
const DAY = 24 * time.Hour
const WEEK = 7 * DAY
const YEAR = 365 * DAY

for _, tc := range []struct {
input string
expected time.Duration
}{
{"1ns", time.Nanosecond},
{"1s", time.Second},
{"1us", MICROSECOND},
{"1m", time.Minute},
{"1h", time.Hour},
{"1µs", MICROSECOND},
{"1y", YEAR},
{"1w", WEEK},
{"1d", DAY},
{"1h15m30.918273645s", time.Hour + 15*time.Minute + 30*time.Second + 918273645*time.Nanosecond},
} {
actual, err := parseDuration(tc.input)

require.Equal(t, err, nil)
require.Equal(t, tc.expected, actual)
}
}