-
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1617 +/- ##
========================================
+ Coverage 71.9% 71.9% +<.1%
========================================
Files 954 954
Lines 79170 79176 +6
========================================
+ Hits 56966 56985 +19
+ Misses 18478 18471 -7
+ Partials 3726 3720 -6
Continue to review full report at Codecov.
|
@@ -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 |
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 :)
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 comment
The 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 comment
The 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 comment
The 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 comment
The 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.
Sounds good.
@@ -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 comment
The 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.
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.
lgtm modulo martin's comments
if seconds, floatErr := strconv.ParseFloat(str, 64); floatErr == nil { | ||
ts := seconds * float64(time.Second) | ||
if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) { | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
nit: cannot parse %q to a valid duration: int64 overflow
or similar (errors aren't sentences)
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.
lgtm
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.
LGTM
if seconds, intErr := strconv.ParseInt(str, 10, 64); intErr == nil { | ||
return time.Duration(seconds) * time.Second, nil | ||
// Try parsing as a float value specifying seconds, the Prometheus default | ||
if seconds, floatErr := strconv.ParseFloat(str, 64); floatErr == nil { |
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.
I think there's a bug here, you're not returning floatErr
here if it's not nil
also, nit: when you have a decent size codeblock I think it might read a little nicer to flip the conditional and then split this up, i.e.
seconds, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, err
}
// logic
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.
Eh i mean it only gets to here if this returns an error:
value, err := time.ParseDuration(str)
So either way it'll return an error - just might not be the float conversion error.
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.
That's wrong. It needs to return this error
@@ -120,13 +146,28 @@ 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a case for "/foo?step=30"
?
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.
There is one already.
What this PR does / why we need it:
Fixes #1610
Previously, if you included a float step size with no units (e.g. 15.00), the query would fail with
This PR allows you to add a float step size without units (and assumes it is in seconds -- equivalent to what Prometheus does)
Special notes for your reviewer:
Does this PR introduce a user-facing and/or backwards incompatible change?:
Does this PR require updating code package or user-facing documentation?: