-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
web/api add limit param #13396
web/api add limit param #13396
Conversation
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
Hey @GiedriusS! Can you please take a look at this pull request? |
web/api/v1/api.go
Outdated
|
||
func parseLimitParam(limitStr string) (limit int, err error) { | ||
limit = math.MaxInt | ||
if limitStr != "" { |
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.
Probably better to return early:
if limitStr == "" {
return limit, 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.
Done.
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.
So now you don't need to check != ""
. This is a Go style point: the code moves further to the left (is less indented) so is a little easier to read.
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.
Thanks for this PR. The code is nice and simple.
As an enhancement we could pass in the limit to the code that computes results, so we can stop early rather than creating the full result then truncating it. However I think it would be fine to merge this and then make it more efficient.
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
It just occurred to me: the caller might like to know if the results were truncated. |
Any plan to move this forward? |
You mean should I add a warning to the response if the results were truncated because of the limit? |
Sounds reasonable. |
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
It now shows the warnings in the response when the results are truncated |
web/api/v1/api.go
Outdated
@@ -783,6 +792,14 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) { | |||
|
|||
slices.Sort(vals) | |||
|
|||
limit, err := parseLimitParam(r.FormValue("limit")) |
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.
Can we move this before we actually fetch data? So that we don't throw a 400 error after we finish fetching label values
web/api/v1/api.go
Outdated
@@ -860,8 +877,19 @@ func (api *API) series(r *http.Request) (result apiFuncResult) { | |||
} | |||
|
|||
metrics := []labels.Labels{} | |||
|
|||
limit, err := parseLimitParam(r.FormValue("limit")) |
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.
ditto
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
It might be out of scope of this pr. For |
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
@darshanime I think you are correct. However, that requires the underlying implementation to support streaming. Having limit as part of the hints might help such usecases. |
In some code paths the results are sorted before that loop. Sorting is O(N log N). And that is not the only place where the limit could be applied. |
I think we may move this forward and then further iterate on it, at least for a start. |
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.
Thanks, the code seems nearly done.
Since there was a mistake in the handling of warnings
, you should add a test for that case.
Also, ideally, a test for an invalid limit
parameter.
web/api/v1/api.go
Outdated
|
||
func parseLimitParam(limitStr string) (limit int, err error) { | ||
limit = math.MaxInt | ||
if limitStr != "" { |
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.
So now you don't need to check != ""
. This is a Go style point: the code moves further to the left (is less indented) so is a little easier to read.
web/api/v1/api.go
Outdated
if len(vals) >= limit { | ||
vals = vals[:limit] | ||
warnings = annotations.New().Add(errors.New("results truncated due to limit")) | ||
return apiFuncResult{vals, nil, warnings, closer} |
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 line seems unnecessary.
web/api/v1/api.go
Outdated
if len(names) >= limit { | ||
names = names[:limit] | ||
warnings := annotations.New().Add(errors.New("results truncated due to limit")) | ||
return apiFuncResult{names, nil, warnings, 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.
This line seems unnecessary, since it is identical to the next line.
Thanks. Yeah I think it is a good start. |
eefa8f2
to
c16f2d5
Compare
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
c16f2d5
to
140a6cb
Compare
Hey @bboreham, sorry for the delay. I was busy with some academic engagements at my university, so I have made some changes; can you please take a look at them? Thanks for your guidance. |
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 this is very close now. Couple of comments remain from my previous review.
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
Was busy due to university minors, suggestions are implemented now, PTAL @bboreham |
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.
Thanks, I think we can merge this now and iterate based on experience.
@@ -703,6 +708,11 @@ func (api *API) labelNames(r *http.Request) apiFuncResult { | |||
if names == nil { | |||
names = []string{} | |||
} | |||
|
|||
if len(names) >= limit { |
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.
shouldn't this check be len(names) == limit
instead of >=
? In this case the returned elements can be equal to the limit and also contain all elements in the database, but the response will still contain a warning. The same is used on the other two endpoints
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.
you mean len(names) > limit
?
I opened #14116 to fix that.
Thanks and don't hesitate to open an issue for such "issues":)
Add warnings count check to TestEndpoints The limit param was added in prometheus#13396 Signed-off-by: machine424 <ayoubmrini424@gmail.com>
Add warnings count check to TestEndpoints The limit param was added in prometheus#13396 Signed-off-by: machine424 <ayoubmrini424@gmail.com>
for the the series, label names and label values APIs Add warnings count check to TestEndpoints The limit param was added in prometheus#13396 Signed-off-by: machine424 <ayoubmrini424@gmail.com>
for the the series, label names and label values APIs Add warnings count check to TestEndpoints The limit param was added in #13396 Signed-off-by: machine424 <ayoubmrini424@gmail.com>
In continuation to the PR #12823