-
Notifications
You must be signed in to change notification settings - Fork 852
Convert heavy queries from 5xx to 4xx #7374
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1d9b4ef
Convert heavy queries from 5xx to 4xx
eeldaly e5fe31a
Fix 504 default return, disable retry on querier timeout
eeldaly 792e6c3
Add tests
eeldaly 627b92e
make check doc
eeldaly 101df71
make check doc
eeldaly 67bc01f
fix QueryStats new max stats
eeldaly d547ee0
docs config schema
eeldaly fb1f312
fix non-constant format string call
eeldaly f9ddb7b
fix pb.go correct version
eeldaly 1c37195
check protos
eeldaly 14fee3c
lint
eeldaly a1227fd
Fix scheduler time track
eeldaly 3aa35ae
Add verification to check query stats enabled when this is enabled
eeldaly eebccb2
fix query end time qfe querier inconsistency
eeldaly 2b9066a
Improve query shard logs on timeout
eeldaly 4033acd
Update default times to match cortex's 2min timeout
eeldaly 341227f
Update default times to match cortex's 2min timeout
eeldaly 315196f
Add docs/guide
eeldaly f71c9ea
Merge branch 'master' into query-4xx
eeldaly d5ed4c6
gofmt
eeldaly d0e4acb
update changelog
eeldaly 61663b6
Merge branch 'master' into query-4xx
friedrichg 61fa202
Clarify only works on instant/ranged queries
eeldaly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Timeout Classification | ||
|
|
||
| Timeout classification lets Cortex distinguish between query timeouts caused by expensive user queries (4XX) and those caused by system issues (5XX). When enabled, queries that spend most of their time in PromQL evaluation are returned as `422 Unprocessable Entity` instead of `503 Service Unavailable`, giving callers a clear signal to simplify the query rather than retry. | ||
|
|
||
| ## How It Works | ||
|
|
||
| When a query (instant/ranged, other apis are unchanged) arrives at the querier, the feature: | ||
|
|
||
| 1. Subtracts any time the query spent waiting in the scheduler queue from the configured deadline. | ||
| 2. Sets a proactive context timeout using the remaining budget, so the querier cancels the query slightly before the PromQL engine's own timeout fires. | ||
| 3. On timeout, inspects phase timings (storage fetch time vs. total time) to compute eval time. | ||
| 4. If eval time exceeds the configured threshold, the timeout is classified as a user error (4XX). Otherwise it remains a system error (5XX). | ||
|
|
||
| This means expensive queries that burn their budget in PromQL evaluation get a `422`, while other queries remain a `5XX`. | ||
|
|
||
| * Note that due to different query shards not returning at the same time, the first returned timed out shard gets to decide whether the query will be converted to 4XX. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Enable the feature and set the three related flags: | ||
|
|
||
| ```yaml | ||
| querier: | ||
| timeout_classification_enabled: true | ||
| timeout_classification_deadline: 1m59s | ||
| timeout_classification_eval_threshold: 1m30s | ||
| ``` | ||
|
|
||
| | Flag | Default | Description | | ||
| |---|---|---| | ||
| | `timeout_classification_enabled` | `false` | Enable 5XX-to-4XX conversion based on phase timing. | | ||
| | `timeout_classification_deadline` | `1m59s` | Proactive cancellation deadline. Set this a few seconds less than the querier timeout. | | ||
| | `timeout_classification_eval_threshold` | `1m30s` | Eval time above which a timeout is classified as user error (4XX). Must be ≤ the deadline. | | ||
|
|
||
| ### Constraints | ||
|
|
||
| - `timeout_classification_deadline` must be positive and strictly less than `querier.timeout`. | ||
| - `timeout_classification_eval_threshold` must be positive and ≤ `timeout_classification_deadline`. | ||
| - Query stats must be enabled (`query_stats_enabled: true` on the frontend handler) for classification to work. | ||
|
|
||
| ## Tuning | ||
|
|
||
| - The deadline should be close to but below the querier timeout so the proactive cancellation fires first. A gap of 1–2 seconds is typical. | ||
| - The eval threshold controls sensitivity. A lower threshold classifies more timeouts as user errors; a higher threshold is more conservative. Start with the default and adjust based on your workload. | ||
| - Monitor the `decision` field in the timeout classification log line (`query shard timed out with classification`) to see how queries are being classified before enabling the conversion. | ||
|
|
||
| ## Observability | ||
|
|
||
| When a query times out and query stats is active, the querier emits a warning-level log line containing: | ||
|
|
||
| - `queue_wait_time` — time spent in the scheduler queue | ||
| - `query_storage_wall_time` — time spent fetching data from storage | ||
| - `eval_time` — computed as `total_time - query_storage_wall_time` | ||
| - `decision` — `0` for 5XX (system), `1` for 4XX (user) | ||
| - `conversion_enabled` — whether the status code conversion is active | ||
|
|
||
| These fields are logged regardless of whether conversion is enabled, so you can observe classification behavior in dry-run mode by setting `timeout_classification_enabled: false` and reviewing the logs. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.