: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): report a missing source module only from a deploy (#97250)
Tom Owers3969d3505369
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/__init__.py b/products/warehouse_sources/backend/temporal/data_imports/sources/__init__.pyindex 69d5f73ea090..87b481fd060c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/__init__.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/__init__.py@@ -3,9 +3,12 @@ import importlibfrom pathlib import Pathfrom typing import Any+from django.conf import settings+import structlogfrom posthog.exceptions_capture import capture_exception+from posthog.run_mode import derive_run_modefrom products.warehouse_sources.backend.types import ExternalDataSourceType@@ -83,7 +86,22 @@ def _load_source(module_path: str) -> None:importlib.import_module(module_path)except Exception as e:logger.exception("load_all_sources: source module failed to import", module=module_path)- capture_exception(e)+ if _should_capture_import_failure(e):+ capture_exception(e)+++def _should_capture_import_failure(error: Exception) -> bool:+ """Whether an import failure is a broken source or an incomplete checkout.++ A missing module means the tree is incomplete: an interpreter without the source SDKs,+ or a source directory added before its generated config was written. A deploy has every+ SDK locked and every generated config committed, so only there does a missing module+ mean a source dropped out of the catalog. Any other error is module-level breakage,+ which counts wherever it happens.+ """+ if isinstance(error, ImportError): # ModuleNotFoundError is a subclass+ return derive_run_mode(settings.CLOUD_DEPLOYMENT, settings.DEBUG).is_deployed_cloud+ return Truedef __getattr__(name: str) -> Any:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tests/test_source_loading.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tests/test_source_loading.pyindex 58924505d857..4885c72d60e8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tests/test_source_loading.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tests/test_source_loading.py@@ -5,7 +5,10 @@ from typing import castimport pytestfrom unittest.mock import patch+from django.test import override_settings+from products.warehouse_sources.backend.temporal.data_imports.sources import (+ _should_capture_import_failure,_source_module_paths,load_all_sources,source_module_path,@@ -25,6 +28,7 @@ def test_broken_source_module_does_not_block_the_rest_of_the_catalog():return_value=[f"{_SOURCES}.gone.source", f"{_SOURCES}.pypi.source"],),patch(f"{_SOURCES}.capture_exception") as capture_exception,+ override_settings(CLOUD_DEPLOYMENT="US"),):load_all_sources()@@ -34,6 +38,25 @@ def test_broken_source_module_does_not_block_the_rest_of_the_catalog():capture_exception.assert_called_once()+ "error,cloud_deployment,expected",+ [+ (ModuleNotFoundError("No module named 'vendor_sdk'"), None, False),+ (ModuleNotFoundError("No module named 'vendor_sdk'"), "US", True),+ (ValueError("module-level code raised"), None, True),+ (ValueError("module-level code raised"), "US", True),+ ],+)+def test_a_missing_module_is_reported_only_from_a_deploy(+ error: Exception, cloud_deployment: str | None, expected: bool+):+ # An unprovisioned or mid-edit checkout cannot import every source, and each failure files+ # its own error-tracking issue. Only a deploy, where the SDKs and generated configs are+ # guaranteed present, can tell that apart from a source that really went missing.+ with override_settings(CLOUD_DEPLOYMENT=cloud_deployment):+ assert _should_capture_import_failure(error) is expected++def test_per_source_fallback_covers_every_source_the_bulk_import_loads():bulk_import_list = ast.parse((Path(__file__).parent.parent / "_load_all.py").read_text())