Skip to content

fix(analytics): catch InfluxDBError in influx_query_manager (fixes #8510) - #8577

Open
siddiqueirshad wants to merge 1 commit into
Flagsmith:mainfrom
siddiqueirshad:fix/influx-query-manager-catch-api-exception
Open

siddiqueirshad wants to merge 1 commit into
Flagsmith:mainfrom
siddiqueirshad:fix/influx-query-manager-catch-api-exception

Conversation

@siddiqueirshad

Copy link
Copy Markdown

Fixes #8510

Cause

influx_query_manager caught only urllib3.exceptions.HTTPError, which covers network/socket connection errors but not Influx HTTP error responses (influxdb_client.client.exceptions.InfluxDBError / ApiException). As a result, non-200 responses from InfluxDB propagated uncaught exceptions and resulted in 500 responses on caller endpoints.

Solution

  1. Catch (HTTPError, InfluxDBError) in influx_query_manager (matching write()).
  2. Add a unit test verifying influx_query_manager handles InfluxDBError by capturing the exception and returning an empty list.

@siddiqueirshad
siddiqueirshad requested a review from a team as a code owner September 23, 2026 08:10
@siddiqueirshad
siddiqueirshad requested review from emyller and removed request for a team September 23, 2026 08:10
@vercel

vercel Bot commented Sep 23, 2026

Copy link
Copy Markdown

@siddiqueirshad is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the api Issue related to the REST API label Sep 23, 2026
@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

📝 Walkthrough

Walkthrough

influx_query_manager now catches InfluxDBError as well as HTTPError. It captures the exception and returns an empty list. A unit test verifies this behaviour.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Severity of issue fixed: Medium

Merge Risk: 🟠 High · up to 5ce97

InfluxDB HTTP failures can still reach analytics endpoints as 500 responses; handle ApiException before merging.

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 23414984-53c8-47ad-b70b-954fa7ff3e49

📥 Commits

Reviewing files that changed from the base of the PR and between abcb31c and 5ce9709.

📒 Files selected for processing (2)
  • api/app_analytics/influxdb_wrapper.py
  • api/tests/unit/app_analytics/test_unit_app_analytics_influxdb_wrapper.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment on lines 150 to 154
try:
return query_api.query(org=settings.INFLUXDB_ORG, query=query)
except HTTPError as e:
except (HTTPError, InfluxDBError) as e:
capture_exception(e)
return []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1,220p' api/app_analytics/influxdb_wrapper.py | cat -n
rg -n -C 3 'influx_query_manager|InfluxDBWrapper' api --glob '*.py'
sed -n '1,160p' api/tests/unit/app_analytics/test_unit_app_analytics_influxdb_wrapper.py | cat -n
git diff --unified=12 49d957aa3228d8733b43951c099840577ff78338 5ce97097baba97504ae0e062cbfd3ab7ffe88d00 -- api/app_analytics/influxdb_wrapper.py api/tests/unit/app_analytics/test_unit_app_analytics_influxdb_wrapper.py
rg -n -C 2 'influxdb-client|influxdb_client' api/pyproject.toml api/uv.lock | head -50

Repository: Flagsmith/flagsmith

Length of output: 41159


🏁 Script executed:

sed -n '60,110p' api/app_analytics/services.py | cat -n
rg -n -C 4 'get_feature_usage|get_multiple_event_list_for_organisation|get_current_api_usage|get_top_organisations|get_usage_data|get_feature_evaluation_data' api --glob '*.py' | head -240

Repository: Flagsmith/flagsmith

Length of output: 24800


Handle ApiException in the query handler.

influxdb-client 1.50.0 raises influxdb_client.rest.ApiException for HTTP response failures. The handler catches only HTTPError and InfluxDBError, so 400, 401, 429, and 5xx failures can escape InfluxDBWrapper.influx_query_manager instead of returning []. Analytics callers can therefore fail instead of receiving the existing empty-result fallback.

Add ApiException to the handler and make the added test raise ApiException. Keep InfluxDBError if this boundary must also handle that separate exception type.

Suggested fix
 from influxdb_client.client.exceptions import InfluxDBError
 from influxdb_client.client.flux_table import FluxTable
 from influxdb_client.client.write_api import SYNCHRONOUS
+from influxdb_client.rest import ApiException
 ...
-        except (HTTPError, InfluxDBError) as e:
+        except (HTTPError, InfluxDBError, ApiException) as e:
-    expected_exception = InfluxDBError(message="InfluxDB error occurred")
+    expected_exception = ApiException(status=400, reason="Bad Request")

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

influx_query_manager doesn't catch ApiException, so Influx error responses 500

1 participant