-
Notifications
You must be signed in to change notification settings - Fork 454
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
[query] Fix step parsing #1617
[query] Fix step parsing #1617
Changes from 1 commit
8c646c6
748f104
1f76b76
15316b3
ef0e3fb
2bb2954
87b4d3d
e86a4b7
2632fce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,8 +76,13 @@ func parseDuration(r *http.Request, key string) (time.Duration, error) { | |
} | ||
|
||
// Try parsing as an integer value specifying seconds, the Prometheus default | ||
if seconds, intErr := strconv.ParseInt(str, 10, 64); intErr == nil { | ||
return time.Duration(seconds) * time.Second, nil | ||
if seconds, intErr := strconv.ParseFloat(str, 64); intErr == nil { | ||
ts := seconds * float64(time.Second) | ||
if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it valid for duration to be negative in the prom context? If not, maybe check that it's positive instead of smaller than minInt64? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it might be worth storing the float version of maxInt64 as a const so you don't need to cast each time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what Prometheus checks, so tempted to just leave it. https://github.com/prometheus/prometheus/blob/master/web/api/v1/api.go#L1171 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good. |
||
return 0, fmt.Errorf("cannot parse %q to a valid duration. It overflows int64", str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
} | ||
|
||
return time.Duration(ts), nil | ||
} | ||
|
||
return 0, err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,14 @@ func TestParseDuration(t *testing.T) { | |
assert.Equal(t, 10*time.Second, v) | ||
} | ||
|
||
func TestParseFloatDuration(t *testing.T) { | ||
r, err := http.NewRequest(http.MethodGet, "/foo?step=10.00m", nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prob want a test where the step is not just a round number? eg Test to ensure that /foo?step=10.85m return the expected output, whether that's the precise duration, a rounded duration or an error. |
||
require.NoError(t, err) | ||
v, err := parseDuration(r, stepParam) | ||
require.NoError(t, err) | ||
assert.Equal(t, 10*time.Minute, v) | ||
} | ||
|
||
func TestParseDurationParsesIntAsSeconds(t *testing.T) { | ||
r, err := http.NewRequest(http.MethodGet, "/foo?step=30", nil) | ||
require.NoError(t, err) | ||
|
@@ -120,6 +128,14 @@ func TestParseDurationParsesIntAsSeconds(t *testing.T) { | |
assert.Equal(t, 30*time.Second, v) | ||
} | ||
|
||
func TestParseDurationParsesFloatAsSeconds(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a case for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one already. |
||
r, err := http.NewRequest(http.MethodGet, "/foo?step=30.00", nil) | ||
require.NoError(t, err) | ||
v, err := parseDuration(r, stepParam) | ||
require.NoError(t, err) | ||
assert.Equal(t, 30*time.Second, v) | ||
} | ||
|
||
func TestParseDurationError(t *testing.T) { | ||
r, err := http.NewRequest(http.MethodGet, "/foo?step=bar10", nil) | ||
require.NoError(t, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Update comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha beat you to it :)