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(data-imports): classify non-json who gho dimension discovery responses (#113030)

Tom Owers0dfb1c787be1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/tests/test_who_gho.py b/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/tests/test_who_gho.py
index 2a65a199465c..dcf8c75da512 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/tests/test_who_gho.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/tests/test_who_gho.py
@@ -7,6 +7,9 @@ from unittest.mock import MagicMock, patch
from parameterized import parameterized
from requests import Response
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client import (
+ RESTClientNonRetryableError,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.who_gho.who_gho import (
MAX_INDICATOR_CODES,
@@ -80,7 +83,7 @@ class TestWhoGhoSourceTransport:
) -> tuple[list[dict[str, Any]], list[str], list[list[dict[str, Any]]]]:
sent_params: list[dict[str, Any]] = []
sent_urls: list[str] = []
- response_iter = iter(responses)
+ response_iter = iter([*(dimension_catalog_responses or []), *responses])
def fake_send(request: Any, *_args: Any, **_kwargs: Any) -> Response:
sent_params.append(dict(request.params or {}))
@@ -93,21 +96,17 @@ class TestWhoGhoSourceTransport:
resource_session.prepare_request.side_effect = lambda request: request
resource_session.send.side_effect = fake_send
- with patch(WHO_GHO_SESSION_PATH) as MockOwnSession:
- if dimension_catalog_responses is not None:
- MockOwnSession.return_value.get.side_effect = dimension_catalog_responses
-
- pages = list(
- who_gho_source(
- endpoint=endpoint,
- indicator_codes=indicator_codes or [],
- team_id=123,
- job_id="job-id",
- resumable_source_manager=manager,
- should_use_incremental_field=should_use_incremental_field,
- since=since,
- )
+ pages = list(
+ who_gho_source(
+ endpoint=endpoint,
+ indicator_codes=indicator_codes or [],
+ team_id=123,
+ job_id="job-id",
+ resumable_source_manager=manager,
+ should_use_incremental_field=should_use_incremental_field,
+ since=since,
)
+ )
return sent_params, sent_urls, pages
@@ -280,6 +279,7 @@ class TestWhoGhoSourceTransport:
)
assert sent_urls == [
+ "https://ghoapi.azureedge.net/api/DIMENSION",
"https://ghoapi.azureedge.net/api/DIMENSION/COUNTRY/DimensionValues",
"https://ghoapi.azureedge.net/api/DIMENSION/SEX/DimensionValues",
]
@@ -292,17 +292,31 @@ class TestWhoGhoSourceTransport:
catalog_page_one = _http_response({"value": [{"Code": f"DIM{index}"} for index in range(1000)]})
catalog_page_two = _http_response({"value": [{"Code": "LASTDIM"}]})
- _, sent_urls, _ = self._drive(
+ sent_params, sent_urls, _ = self._drive(
"dimension_values",
manager,
[_http_response({"value": []}) for _ in range(1001)],
dimension_catalog_responses=[catalog_page_one, catalog_page_two],
)
+ assert [params["$skip"] for params in sent_params[:2]] == [0, 1000]
# One DimensionValues request per discovered code, including the one from the second
# discovery page, confirms discovery did not stop after the first (full) page.
assert sent_urls[-1] == "https://ghoapi.azureedge.net/api/DIMENSION/LASTDIM/DimensionValues"
+ def test_dimension_values_fails_cleanly_when_discovery_returns_an_html_page(self) -> None:
+ manager = MagicMock(spec=ResumableSourceManager)
+ manager.can_resume.return_value = False
+
+ maintenance_page = Response()
+ maintenance_page.status_code = 200
+ maintenance_page._content = b" <!DOCTYPE html><html><head><title>Sorry</title></head></html>"
+ maintenance_page.headers["Content-Type"] = "text/html; charset=utf-8"
+ maintenance_page.url = "https://www.example.com/sorry/"
+
+ with pytest.raises(RESTClientNonRetryableError, match="Non-JSON response from"):
+ self._drive("dimension_values", manager, [], dimension_catalog_responses=[maintenance_page])
+
class TestValidateCredentials:
def _validate(self, codes: list[str], responses: list[Response]) -> tuple[bool, Optional[str]]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/who_gho.py b/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/who_gho.py
index 4641c1804a57..8727b3c316a9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/who_gho.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/who_gho/who_gho.py
@@ -13,6 +13,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.res
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.paginators import (
OffsetPaginator,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client import RESTClient
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.typing import EndpointResource
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.who_gho.settings import (
@@ -27,6 +28,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.who_gho.se
)
BASE_URL = "https://ghoapi.azureedge.net/api"
+# (connect, read) seconds, so a connection the API accepts but never answers can't hold the worker.
+REQUEST_TIMEOUT = (10, 120)
# Codes may be pasted one per line, comma separated, or semicolon separated.
_CODE_SEPARATORS = re.compile(r"[,;\s]+")
@@ -92,6 +95,7 @@ def _rest_config(resource: EndpointResource) -> RESTAPIConfig:
"client": {
"base_url": BASE_URL,
"paginator": _paginator(),
+ "request_timeout": REQUEST_TIMEOUT,
},
"resource_defaults": {"write_disposition": "replace"},
"resources": [resource],
@@ -124,19 +128,10 @@ def _catalog_pages(
def _fetch_all_dimension_codes() -> list[str]:
"""One-off, unpaginated-by-default catalog: fetched fresh at the start of every
dimension_values sync rather than hardcoded, since WHO adds dimensions over time."""
- session = make_tracked_session()
+ client = RESTClient(base_url=BASE_URL, paginator=_paginator(), request_timeout=REQUEST_TIMEOUT)
codes: list[str] = []
- skip = 0
- while True:
- response = session.get(
- f"{BASE_URL}{CATALOG_ENDPOINTS[DIMENSIONS_ENDPOINT].path}", params={"$top": PAGE_SIZE, "$skip": skip}
- )
- response.raise_for_status()
- rows = response.json().get("value", [])
+ for rows in client.paginate(CATALOG_ENDPOINTS[DIMENSIONS_ENDPOINT].path, data_selector="value"):
codes.extend(row["Code"] for row in rows if row.get("Code"))
- if len(rows) < PAGE_SIZE:
- break
- skip += PAGE_SIZE
return codes