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

HTTP API: Support float values for default query step #1359

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions pkg/loghttp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func bounds(r *http.Request) (time.Time, time.Time, error) {
func step(r *http.Request, start, end time.Time) (time.Duration, error) {
value := r.URL.Query().Get("step")
if value == "" {
return time.Duration(defaultQueryRangeStep(start, end)) * time.Second, nil
return defaultQueryRangeStep(start, end), nil
}

if d, err := strconv.ParseFloat(value, 64); err == nil {
Expand All @@ -73,8 +73,8 @@ func step(r *http.Request, start, end time.Time) (time.Duration, error) {

// defaultQueryRangeStep returns the default step used in the query range API,
// which is dinamically calculated based on the time range
func defaultQueryRangeStep(start time.Time, end time.Time) int {
return int(math.Max(math.Floor(end.Sub(start).Seconds()/250), 1))
func defaultQueryRangeStep(start time.Time, end time.Time) time.Duration {
return end.Sub(start) / 250
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the / 250 was applied to seconds. Now it's applied to nanoseconds. Doesn't that changes the behaviour ?

/cc @pracucci WDYT ? You did that piece.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the current behaviour changes as can be seen on the tests. Now the range is divided in 250 precise steps. For example, in the previous implementation if you had a range of 1h you would get steps of 14s, and now you would get steps of 14,4s.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ha that makes sense ! since you returns nanoseconds now.

Copy link
Contributor Author

@danieldabate danieldabate Dec 3, 2019

Choose a reason for hiding this comment

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

Also the value is not bounded to 1s anymore. So if you have a range of, let's say, 200seconds you will get steps of 0.8s. I think it makes sense, but maybe someone else with more knowledge on this part will know better.

Copy link
Contributor

Choose a reason for hiding this comment

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

The change itself doesn't look wrong, but I'm not sure this change brings any real benefit and I'm not getting the reason why we should support steps below 1s. Which use case do we want to cover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You've got a good point there. There's no real use case at the moment that supports this change. It was just an idea we had with Cyril after pushing this change: #1355

Actually, thinking out loud Prometheus doesn't seem to provide a default value for this parameter, so if we want Loki to have an API matching Prometheus' API, we should remove this feature.

}

func tailDelay(r *http.Request) (uint32, error) {
Expand Down
27 changes: 16 additions & 11 deletions pkg/loghttp/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ func TestHttp_defaultQueryRangeStep(t *testing.T) {
tests := map[string]struct {
start time.Time
end time.Time
expected int
expected time.Duration
}{
"should not be lower then 1s": {
"should return 0.8s if input time range is 200s": {
start: time.Unix(60, 0),
end: time.Unix(60, 0),
expected: 1,
end: time.Unix(260, 0),
expected: 800 * time.Millisecond,
},
"should return 1s if input time range is 5m": {
"should return 1s if input time range is 250s": {
start: time.Unix(60, 0),
end: time.Unix(310, 0),
expected: 1 * time.Second,
},
"should return 1.2s if input time range is 5m": {
start: time.Unix(60, 0),
end: time.Unix(360, 0),
expected: 1,
expected: 1200 * time.Millisecond,
},
"should return 14s if input time range is 1h": {
"should return 14.4s if input time range is 1h": {
start: time.Unix(60, 0),
end: time.Unix(3660, 0),
expected: 14,
expected: 14400 * time.Millisecond,
},
}

Expand All @@ -58,7 +63,7 @@ func TestHttp_ParseRangeQuery_Step(t *testing.T) {
Query: "{}",
Start: time.Unix(0, 0),
End: time.Unix(3600, 0),
Step: 14 * time.Second,
Step: 14400 * time.Millisecond,
Limit: 100,
Direction: logproto.BACKWARD,
},
Expand Down Expand Up @@ -91,7 +96,7 @@ func TestHttp_ParseRangeQuery_Step(t *testing.T) {
Query: "{}",
Start: time.Unix(0, 0),
End: time.Unix(3600, 0),
Step: 5.5 * 1e9,
Step: 5500 * time.Millisecond,
Limit: 100,
Direction: logproto.BACKWARD,
},
Expand All @@ -113,7 +118,7 @@ func TestHttp_ParseRangeQuery_Step(t *testing.T) {
Query: "{}",
Start: time.Unix(0, 0),
End: time.Unix(3600, 0),
Step: 5 * 24 * 3600 * time.Second,
Step: 5 * 24 * time.Hour,
Limit: 100,
Direction: logproto.BACKWARD,
},
Expand Down