Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(new_relic): retry a truncated NerdGraph response instead of failing the sync (#92096)

Tom Owersa3c4a5dc63f7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/new_relic.py b/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/new_relic.py
index 487ee6d0d75d..7013880c6f2e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/new_relic.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/new_relic.py
@@ -191,7 +191,14 @@ def _execute_graphql(
logger.error(f"New Relic API error: status={response.status_code}, body={response.text}, url={url}")
response.raise_for_status()
- body = response.json()
+ try:
+ body = response.json()
+ except requests.exceptions.JSONDecodeError as exc:
+ # A truncated or malformed body after an otherwise-OK response means the transfer was
+ # cut short (dropped connection, an intermediary returning a partial payload). It's
+ # transient, so retry rather than fail the sync.
+ raise NewRelicRetryableError(f"New Relic API error (retryable): malformed JSON response, url={url}") from exc
+
errors = body.get("errors")
if errors:
messages = "; ".join(str(error.get("message", error)) for error in errors)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/tests/test_new_relic.py b/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/tests/test_new_relic.py
index fc2d73f9fce1..27578e4d416d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/tests/test_new_relic.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/new_relic/tests/test_new_relic.py
@@ -152,6 +152,12 @@ class TestExecuteGraphql:
with pytest.raises(NewRelicRetryableError):
self._execute(self._response(body=body))
+ def test_malformed_json_body_is_retryable(self) -> None:
+ response = self._response()
+ response.json.side_effect = requests.exceptions.JSONDecodeError("Unterminated string", "", 0)
+ with pytest.raises(NewRelicRetryableError):
+ self._execute(response)
+
def test_returns_data_on_success(self) -> None:
body = {"data": {"actor": {"account": {"id": ACCOUNT_ID}}}}
assert self._execute(self._response(body=body)) == body["data"]