8000 query-frontend: Fix panic in ResponseToSamples when resp.Data is nil by dimitarvdimitrov · Pull Request #11571 · grafana/mimir · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

query-frontend: Fix panic in ResponseToSamples when resp.Data is nil #11571

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

Conversation

dimitarvdimitrov
Copy link
Contributor
@dimitarvdimitrov dimitarvdimitrov commented May 28, 2025

Fixes a panic in ResponseToSamples when HTTP error responses (like 511 cluster validation errors) are incorrectly parsed as successful responses, resulting in PrometheusResponse objects with Data = nil.

The function was directly accessing resp.Data.ResultType without checking if resp.Data is nil, causing runtime panics when malformed responses are processed.

This could happen when the remote response was a JSON response which wasn't actually a valid prometheus response. For example this may happen when the cluster validation middleware responds with

HTTP 511
{
   "cluster_validation_error_message":"rejected request with wrong cluster validation label \"cluster-a\" - it should be \"cluster-b-secret\"",
   "route":"prometheus_api_v1_query"
}

dimitarvdimitrov and others added 5 commits May 28, 2025 13:02
Adds nil check to prevent panic when HTTP error responses (like 511
cluster validation errors) are incorrectly parsed as successful
responses, resulting in PrometheusResponse objects with Data = nil.

The fix converts the panic to a graceful error return, making the
function defensive and consistent with other patterns in the codebase.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Tests cover various scenarios where responses might not be valid
Prometheus responses:

- Arbitrary JSON objects that don't match the Prometheus schema
- Malformed JSON (arrays, strings, numbers instead of objects)
- Invalid JSON syntax (missing braces, trailing commas, unescaped quotes)
- Different content types (text/plain, text/html, application/xml)
- Large payloads and special characters (unicode, emojis)
- Error status codes with and without content-type headers

These tests ensure the codec handles edge cases gracefully and that
ResponseToSamples properly handles responses with nil data instead
of panicking.
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
- Consolidate all edge case tests into single TestPrometheusCodec_ResponseParsingEdgeCases function
- Add multiple status codes per test case to validate behavior across different HTTP response codes
- Add special characters test case using anonymous function for unicode, emoji, and special characters
- Add large payload test case using anonymous function to generate 10k item JSON array
- Remove separate test functions to reduce code duplication
- Ensure all tests validate that ResponseToSamples handles nil data gracefully

Tests now cover comprehensive edge cases with various status codes:
- 200 (success), 400/404 (client errors), 500/503/511 (server errors), 429/413 (rate limiting)
@dimitarvdimitrov dimitarvdimitrov requested a review from a team as a code owner May 28, 2025 11:24
Copy link
Contributor
@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
@aknuds1 aknuds1 enabled auto-merge (squash) May 28, 2025 11:49
@aknuds1 aknuds1 merged commit 2a65a97 into main May 28, 2025
29 checks passed
@aknuds1 aknuds1 deleted the dimitar/querymiddleware/fix-response-to-samples-nil-panic branch May 28, 2025 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0