implement suggestions

Signed-off-by: Vandit Singh <vanditsinghkv@gmail.com>
This commit is contained in:
Vandit Singh 2024-12-12 23:12:08 +05:30
parent d02c40c79e
commit beb198df50
2 changed files with 11 additions and 7 deletions

View file

@ -86,6 +86,7 @@ URL query parameters:
- `time=<rfc3339 | unix_timestamp>`: Evaluation timestamp. Optional.
- `timeout=<duration>`: Evaluation timeout. Optional. Defaults to and
is capped by the value of the `-query.timeout` flag.
- `limit=<number>`: Maximum number of returned series. doesnt affect scalars or strings but truncates series for matrices and vectors. Optional.
The current server time is used if the `time` parameter is omitted.
@ -154,6 +155,7 @@ URL query parameters:
- `step=<duration | float>`: Query resolution step width in `duration` format or float number of seconds.
- `timeout=<duration>`: Evaluation timeout. Optional. Defaults to and
is capped by the value of the `-query.timeout` flag.
- `limit=<number>`: Maximum number of returned series. Optional.
You can URL-encode these parameters directly in the request body by using the `POST` method and
`Content-Type: application/x-www-form-urlencoded` header. This is useful when specifying a large

View file

@ -480,8 +480,10 @@ func (api *API) query(r *http.Request) (result apiFuncResult) {
return apiFuncResult{nil, returnAPIError(res.Err), res.Warnings, qry.Close}
}
var warnings annotations.Annotations
if limit > 0 {
res = truncateResults(res, limit)
warnings = warnings.Add(errors.New("results truncated due to limit"))
}
// Optional stats field in response if parameter "stats" is not empty.
sr := api.statsRenderer
@ -494,7 +496,7 @@ func (api *API) query(r *http.Request) (result apiFuncResult) {
ResultType: res.Value.Type(),
Result: res.Value,
Stats: qs,
}, nil, res.Warnings, qry.Close}
}, nil, warnings, qry.Close}
}
func (api *API) formatQuery(r *http.Request) (result apiFuncResult) {
@ -598,8 +600,10 @@ func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
return apiFuncResult{nil, returnAPIError(res.Err), res.Warnings, qry.Close}
}
var warnings annotations.Annotations
if limit > 0 {
res = truncateResults(res, limit)
warnings = warnings.Add(errors.New("results truncated due to limit"))
}
// Optional stats field in response if parameter "stats" is not empty.
@ -613,7 +617,7 @@ func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
ResultType: res.Value.Type(),
Result: res.Value,
Stats: qs,
}, nil, res.Warnings, qry.Close}
}, nil, warnings, qry.Close}
}
func (api *API) queryExemplars(r *http.Request) apiFuncResult {
@ -2115,6 +2119,8 @@ func toHintLimit(limit int) int {
return limit
}
// truncateResults truncates result for queryRange() and query().
// No truncation for other types. (Scalars or Strings)
func truncateResults(result *promql.Result, limit int) *promql.Result {
switch v := result.Value.(type) {
case promql.Matrix:
@ -2125,11 +2131,7 @@ func truncateResults(result *promql.Result, limit int) *promql.Result {
if len(v) > limit {
result.Value = v[:limit]
}
default:
// No truncation for other types (Scalars or Strings)
return result
}
// Return the modified result
// Return the modified result. Unchanged for other types.
return result
}