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(exports): stop reporting user query errors to error tracking (#115024)

posthog[bot]cc9e2ee01140
diff --git a/posthog/tasks/exporter.py b/posthog/tasks/exporter.py
index e67d4f890a06..9e14090167cd 100644
--- a/posthog/tasks/exporter.py
+++ b/posthog/tasks/exporter.py
@@ -126,10 +126,10 @@ def export_asset_direct(
csv_exporter.export_tabular(exported_asset, limit=limit, source=export_source)
elif exported_asset.export_format == ExportedAsset.ExportFormat.JSONL:
if not exported_asset.is_dataset_export:
- raise InvalidExportContext("JSONL exports require a dataset export context.")
+ raise InvalidExportContext("JSONL exports require a dataset export context.", reportable=True)
export_handler = get_export_format_handler(exported_asset.export_format)
if export_handler is None:
- raise InvalidExportContext("No handler is registered for this export format.")
+ raise InvalidExportContext("No handler is registered for this export format.", reportable=True)
export_handler(exported_asset)
else:
image_exporter.export_image(exported_asset, max_height_pixels=max_height_pixels, source=export_source)
diff --git a/posthog/temporal/exports/activities.py b/posthog/temporal/exports/activities.py
index 3b7487961dc8..54905425c93f 100644
--- a/posthog/temporal/exports/activities.py
+++ b/posthog/temporal/exports/activities.py
@@ -7,7 +7,12 @@ from temporalio.exceptions import ApplicationError
from posthog.event_usage import EventSource
from posthog.sync import database_sync_to_async
from posthog.tasks import exporter
-from posthog.temporal.common.errors import MAX_ERROR_MESSAGE_CHARS, MAX_ERROR_TRACE_CHARS, truncate_for_temporal_payload
+from posthog.temporal.common.errors import (
+ MAX_ERROR_MESSAGE_CHARS,
+ MAX_ERROR_TRACE_CHARS,
+ NonReportableApplicationError,
+ truncate_for_temporal_payload,
+)
from posthog.temporal.common.heartbeat import Heartbeater
from posthog.temporal.exports.types import ExportAssetActivityInputs, ExportAssetResult, export_failure_metadata
@@ -17,6 +22,7 @@ from products.exports.backend.tasks.failure_handler import (
TIMEOUT_ERROR_NAMES,
ExportCancelled,
export_slo_failure_details,
+ is_reportable_export_failure,
)
logger = structlog.get_logger(__name__)
@@ -75,7 +81,9 @@ async def export_asset_activity(inputs: ExportAssetActivityInputs) -> ExportAsse
# errors retry; programming errors and Chrome crashes fail fast). See
# posthog.temporal.exports.types.extract_error_details. Strings are truncated so
# an upstream exception can't blow out the 2 MiB payload envelope.
- raise ApplicationError(
+ # A user query error is not a defect, so it stays out of error tracking.
+ error_class = ApplicationError if is_reportable_export_failure(e) else NonReportableApplicationError
+ raise error_class(
truncate_for_temporal_payload(str(e), MAX_ERROR_MESSAGE_CHARS),
truncate_for_temporal_payload(error_trace, MAX_ERROR_TRACE_CHARS),
export_failure_metadata(export_slo_failure_details(e)),
diff --git a/posthog/temporal/tests/test_export_activities.py b/posthog/temporal/tests/test_export_activities.py
index 4675d015b5bc..9c2be7c88bdd 100644
--- a/posthog/temporal/tests/test_export_activities.py
+++ b/posthog/temporal/tests/test_export_activities.py
@@ -16,14 +16,21 @@ from temporalio.exceptions import ActivityError, ApplicationError
from temporalio.testing import ActivityEnvironment, WorkflowEnvironment
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
+from posthog.hogql.errors import QueryError
+
from posthog.models import Team
from posthog.temporal.common.base import PostHogWorkflow
+from posthog.temporal.common.posthog_client import is_expected_activity_failure
from posthog.temporal.exports.activities import export_asset_activity
from posthog.temporal.exports.retry_policy import EXPORT_RETRY_POLICY
from posthog.temporal.exports.types import ExportAssetActivityInputs, ExportAssetResult
from products.exports.backend.models.exported_asset import ExportedAsset
-from products.exports.backend.tasks.failure_handler import ExcelColumnLimitExceeded, ExportCancelled
+from products.exports.backend.tasks.failure_handler import (
+ ExcelColumnLimitExceeded,
+ ExportCancelled,
+ InvalidExportContext,
+)
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db(transaction=True)]
@@ -148,6 +155,41 @@ async def test_export_asset_activity_timeout_errors_are_retryable(
assert exc_info.value.non_retryable is expected_non_retryable
+ "exception,expected_reported",
+ [
+ (QueryError("Unknown table"), False),
+ (ExcelColumnLimitExceeded(), False),
+ (InvalidExportContext("Invalid query: unknown table"), False),
+ (InvalidExportContext("Export is missing a render target", reportable=True), True),
+ (ValueError("render failed"), True),
+ ],
+)
+@patch("posthog.temporal.exports.activities.exporter")
+async def test_export_asset_activity_reports_only_non_user_errors(
+ mock_exporter: MagicMock,
+ activity_environment: ActivityEnvironment,
+ team: Team,
+ exception: Exception,
+ expected_reported: bool,
+) -> None:
+ asset = await sync_to_async(ExportedAsset.objects.create)(
+ team=team,
+ export_format=ExportedAsset.ExportFormat.PNG,
+ )
+
+ def fake_export(_exported_asset: ExportedAsset, **_kwargs: object) -> None:
+ raise exception
+
+ mock_exporter.export_asset_direct = fake_export
+
+ with pytest.raises(ApplicationError) as exc_info:
+ await activity_environment.run(export_asset_activity, ExportAssetActivityInputs(exported_asset_id=asset.id))
+
+ assert exc_info.value.type == type(exception).__name__
+ assert is_expected_activity_failure(exc_info.value) is not expected_reported
+
+
@patch("posthog.temporal.exports.activities.exporter")
async def test_export_asset_activity_refresh_failure_preserves_export_error(
mock_exporter: MagicMock, activity_environment: ActivityEnvironment, team: Team
diff --git a/products/ai_observability/backend/dataset_export.py b/products/ai_observability/backend/dataset_export.py
index 2f85359882a7..f10d3d8e4c83 100644
--- a/products/ai_observability/backend/dataset_export.py
+++ b/products/ai_observability/backend/dataset_export.py
@@ -81,10 +81,10 @@ def export_dataset_jsonl(asset: ExportedAsset) -> None:
dataset_id = UUID(str(context["dataset_id"]))
revision = int(context["dataset_revision"])
except (KeyError, TypeError, ValueError) as error:
- raise DatasetExportError("The dataset export configuration is invalid.") from error
+ raise DatasetExportError("The dataset export configuration is invalid.", reportable=True) from error
if not asset.is_dataset_export or revision < 1 or asset.created_by is None:
- raise DatasetExportError("The dataset export configuration is invalid.")
+ raise DatasetExportError("The dataset export configuration is invalid.", reportable=True)
dataset = Dataset.objects.for_team(asset.team_id, canonical=True).filter(id=dataset_id).first()
if dataset is None:
diff --git a/products/exports/backend/tasks/csv_exporter.py b/products/exports/backend/tasks/csv_exporter.py
index 1861b93d60b6..a0907f374a2d 100644
--- a/products/exports/backend/tasks/csv_exporter.py
+++ b/products/exports/backend/tasks/csv_exporter.py
@@ -48,7 +48,7 @@ from posthog.utils import absolute_uri
from products.exports.backend.models.exported_asset import ExportedAsset, save_content_from_file
-from .failure_handler import ExcelColumnLimitExceeded
+from .failure_handler import ExcelColumnLimitExceeded, is_reportable_export_failure
logger = structlog.get_logger(__name__)
@@ -793,6 +793,7 @@ def export_tabular(
"dashboard_id": exported_asset.dashboard_id,
},
)
- else:
+ # A broken saved query is the user's to fix, and the asset already records it.
+ elif is_reportable_export_failure(e):
capture_exception(e, additional_properties={"task": "csv_export", "team_id": team_id})
raise
diff --git a/products/exports/backend/tasks/failure_handler.py b/products/exports/backend/tasks/failure_handler.py
index 0b72a7a2ccd6..d1c558fea1dd 100644
--- a/products/exports/backend/tasks/failure_handler.py
+++ b/products/exports/backend/tasks/failure_handler.py
@@ -183,7 +183,11 @@ class ExcelColumnLimitExceeded(Exception):
class InvalidExportContext(Exception):
"""Raised when an export's export_context lacks a renderable target."""
- pass
+ def __init__(self, message: str, *, reportable: bool = False):
+ super().__init__(message)
+ # Set when PostHog built the context wrong (no target, no handler), not the customer. The SLO
+ # still counts it as a user failure, but error tracking must see it.
+ self.reportable = reportable
class RetryableExportError(Exception):
@@ -294,6 +298,12 @@ def classify_failure_type(exception: Exception | str) -> str:
return FAILURE_TYPE_UNKNOWN
+def is_reportable_export_failure(exception: Exception) -> bool:
+ if isinstance(exception, InvalidExportContext) and exception.reportable:
+ return True
+ return classify_failure_type(exception) != FAILURE_TYPE_USER
+
+
def is_user_query_error_type(exception_type: str | None) -> bool:
if exception_type is None:
return False
diff --git a/products/exports/backend/tasks/image_exporter.py b/products/exports/backend/tasks/image_exporter.py
index 45f22df1c5a1..1fdba81f37a5 100644
--- a/products/exports/backend/tasks/image_exporter.py
+++ b/products/exports/backend/tasks/image_exporter.py
@@ -41,6 +41,7 @@ from products.exports.backend.tasks.failure_handler import (
BrowserlessUnavailable,
InvalidExportContext,
classify_failure_type,
+ is_reportable_export_failure,
)
from products.exports.backend.url_security import is_heatmap_url_allowed
from products.product_analytics.backend.facade.api import insight_variables_for_team, map_stale_to_latest
@@ -306,7 +307,8 @@ def _export_to_png(
page_load_timeout = 100
else:
raise InvalidExportContext(
- "Export is missing required dashboard, insight ID, session_recording_id, or query source in export_context"
+ "Export is missing required dashboard, insight ID, session_recording_id, or query source in export_context",
+ reportable=True,
)
logger.info("exporting_asset", asset_id=exported_asset.id, render_url=url_to_render)
@@ -707,6 +709,7 @@ def export_image(
"dashboard_id": exported_asset.dashboard_id,
},
)
- else:
+ # A broken saved query is the user's to fix, and the asset already records it.
+ elif is_reportable_export_failure(e):
capture_exception(e, additional_properties={"task": "image_export", "team_id": team_id})
raise
diff --git a/products/exports/backend/tasks/test/test_image_exporter.py b/products/exports/backend/tasks/test/test_image_exporter.py
index 606fa4f04b26..a41d231c6210 100644
--- a/products/exports/backend/tasks/test/test_image_exporter.py
+++ b/products/exports/backend/tasks/test/test_image_exporter.py
@@ -152,6 +152,32 @@ class TestImageExporter(APIBaseTest):
# The browser must never be reached — the point is failing before the render.
mock_screenshot_asset.assert_not_called()
+ @parameterized.expand(
+ [
+ ("user_query_error", QueryError("Unknown table"), False),
+ ("unexpected_error", ValueError("render failed"), True),
+ ]
+ )
+ def test_export_failure_reaches_error_tracking_only_when_not_a_user_error(
+ self,
+ mock_remove: Any,
+ mock_open_file: Any,
+ mock_screenshot_asset: Any,
+ _name: str,
+ error: Exception,
+ expect_captured: bool,
+ ) -> None:
+ mock_screenshot_asset.side_effect = error
+
+ with (
+ self.settings(OBJECT_STORAGE_ENABLED=False),
+ patch("products.exports.backend.tasks.image_exporter.capture_exception") as mock_capture,
+ self.assertRaises(type(error)),
+ ):
+ image_exporter.export_image(self.exported_asset)
+
+ assert mock_capture.called is expect_captured
+
def test_image_exporter_writes_to_asset_when_object_storage_is_disabled(self, *args: Any) -> None:
with self.settings(OBJECT_STORAGE_ENABLED=False):
image_exporter.export_image(self.exported_asset)