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

[query] Fix step parsing #1617

Merged
merged 9 commits into from
May 9, 2019
Merged

[query] Fix step parsing #1617

merged 9 commits into from
May 9, 2019

Conversation

benraskin92
Copy link
Collaborator

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

"error":"error parsing param: step, error: time: missing unit in duration 15.00"

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?:

Maybe we can add something in the docs, but I don't think it's necessary. 

@codecov
Copy link

codecov bot commented May 8, 2019

Codecov Report

Merging #1617 into master will increase coverage by <.1%.
The diff coverage is 100%.

Impacted file tree graph

@@           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
Flag Coverage Δ
#aggregator 82.3% <ø> (-0.1%) ⬇️
#cluster 85.7% <ø> (ø) ⬆️
#collector 63.7% <ø> (ø) ⬆️
#dbnode 79.8% <ø> (ø) ⬆️
#m3em 73.2% <ø> (ø) ⬆️
#m3ninx 73.9% <ø> (ø) ⬆️
#m3nsch 51.1% <ø> (ø) ⬆️
#metrics 17.6% <ø> (ø) ⬆️
#msg 74.7% <ø> (ø) ⬆️
#query 67.1% <100%> (ø) ⬆️
#x 87.1% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2e0830d...2632fce. Read the comment docs.

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Update comment.

Copy link
Collaborator Author

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) {
Copy link
Collaborator

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?

Copy link
Collaborator

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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)
Copy link
Collaborator

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.

Copy link
Collaborator

@mway mway left a 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)
Copy link
Collaborator

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)

Copy link
Collaborator

@mway mway left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Collaborator

@robskillington robskillington left a 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 {
Copy link
Collaborator

@arnikola arnikola May 9, 2019

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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) {
Copy link
Collaborator

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"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is one already.

@benraskin92 benraskin92 merged commit 582dc07 into master May 9, 2019
@benraskin92 benraskin92 deleted the braskin/investigate_1610 branch May 9, 2019 15:53
@arnikola arnikola restored the braskin/investigate_1610 branch May 9, 2019 16:08
@robskillington robskillington deleted the braskin/investigate_1610 branch May 13, 2019 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

M3 can't parse query_range with decimal step parameter (without unit)
5 participants