-
Notifications
You must be signed in to change notification settings - Fork 10.3k
web/api add limit param #13396
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
Changes from all commits
7a021bd
cda3049
071af9d
8ea623d
66ab452
641d665
a5f9b52
140a6cb
2c88e61
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 |
|---|---|---|
|
|
@@ -644,6 +644,11 @@ func returnAPIError(err error) *apiError { | |
| } | ||
|
|
||
| func (api *API) labelNames(r *http.Request) apiFuncResult { | ||
| limit, err := parseLimitParam(r.FormValue("limit")) | ||
| if err != nil { | ||
| return invalidParamError(err, "limit") | ||
| } | ||
|
|
||
| start, err := parseTimeParam(r, "start", MinTime) | ||
| if err != nil { | ||
| return invalidParamError(err, "start") | ||
|
|
@@ -703,6 +708,11 @@ func (api *API) labelNames(r *http.Request) apiFuncResult { | |
| if names == nil { | ||
| names = []string{} | ||
| } | ||
|
|
||
| if len(names) >= limit { | ||
|
Contributor
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. shouldn't this check be
Member
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. you mean |
||
| names = names[:limit] | ||
| warnings = warnings.Add(errors.New("results truncated due to limit")) | ||
| } | ||
| return apiFuncResult{names, nil, warnings, nil} | ||
|
bboreham marked this conversation as resolved.
|
||
| } | ||
|
|
||
|
|
@@ -714,6 +724,11 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) { | |
| return apiFuncResult{nil, &apiError{errorBadData, fmt.Errorf("invalid label name: %q", name)}, nil, nil} | ||
| } | ||
|
|
||
| limit, err := parseLimitParam(r.FormValue("limit")) | ||
| if err != nil { | ||
| return invalidParamError(err, "limit") | ||
| } | ||
|
|
||
| start, err := parseTimeParam(r, "start", MinTime) | ||
| if err != nil { | ||
| return invalidParamError(err, "start") | ||
|
|
@@ -783,6 +798,11 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) { | |
|
|
||
| slices.Sort(vals) | ||
|
|
||
| if len(vals) >= limit { | ||
| vals = vals[:limit] | ||
| warnings = warnings.Add(errors.New("results truncated due to limit")) | ||
| } | ||
|
|
||
| return apiFuncResult{vals, nil, warnings, closer} | ||
| } | ||
|
|
||
|
|
@@ -809,6 +829,11 @@ func (api *API) series(r *http.Request) (result apiFuncResult) { | |
| return apiFuncResult{nil, &apiError{errorBadData, errors.New("no match[] parameter provided")}, nil, nil} | ||
| } | ||
|
|
||
| limit, err := parseLimitParam(r.FormValue("limit")) | ||
| if err != nil { | ||
| return invalidParamError(err, "limit") | ||
| } | ||
|
|
||
| start, err := parseTimeParam(r, "start", MinTime) | ||
| if err != nil { | ||
| return invalidParamError(err, "start") | ||
|
|
@@ -860,11 +885,17 @@ func (api *API) series(r *http.Request) (result apiFuncResult) { | |
| } | ||
|
|
||
| metrics := []labels.Labels{} | ||
|
|
||
| warnings := set.Warnings() | ||
|
|
||
| for set.Next() { | ||
| metrics = append(metrics, set.At().Labels()) | ||
| } | ||
|
|
||
| warnings := set.Warnings() | ||
| if len(metrics) >= limit { | ||
| warnings.Add(errors.New("results truncated due to limit")) | ||
| return apiFuncResult{metrics, nil, warnings, closer} | ||
| } | ||
| } | ||
| if set.Err() != nil { | ||
| return apiFuncResult{nil, returnAPIError(set.Err()), warnings, closer} | ||
| } | ||
|
|
@@ -1868,3 +1899,20 @@ OUTER: | |
| } | ||
| return matcherSets, nil | ||
| } | ||
|
|
||
| func parseLimitParam(limitStr string) (limit int, err error) { | ||
| limit = math.MaxInt | ||
| if limitStr == "" { | ||
| return limit, nil | ||
| } | ||
|
|
||
| limit, err = strconv.Atoi(limitStr) | ||
| if err != nil { | ||
| return limit, err | ||
| } | ||
| if limit <= 0 { | ||
| return limit, errors.New("limit must be positive") | ||
| } | ||
|
|
||
| return limit, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.