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): skip appsignal trace windows outside tracing retention (#126475)

Tom Owers94bf3928cbe8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/appsignal.py b/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/appsignal.py
index 3d27e68e5d17..0d1c40b1f58f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/appsignal.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/appsignal.py
@@ -876,23 +876,31 @@ def _iter_action_traces(
windows: deque[tuple[int, int]] = deque([(since, before)])
while windows:
window_since, window_before = windows.popleft()
- rows = (
- _fetch_v2(
- session,
- api_token,
- "/tracing/traces/performance",
- logger,
- body={
- "site_ids": [app_id],
- "namespace": namespace,
- "action_name": action_name,
- "from": _to_iso(window_since),
- "to": _to_iso(window_before),
- "pagination": {"per_page": V2_PAGE_SIZE, "order": "ASC", "cursor": "Time"},
- },
+ try:
+ rows = (
+ _fetch_v2(
+ session,
+ api_token,
+ "/tracing/traces/performance",
+ logger,
+ body={
+ "site_ids": [app_id],
+ "namespace": namespace,
+ "action_name": action_name,
+ "from": _to_iso(window_since),
+ "to": _to_iso(window_before),
+ "pagination": {"per_page": V2_PAGE_SIZE, "order": "ASC", "cursor": "Time"},
+ },
+ )
+ or []
)
- or []
- )
+ except requests.HTTPError as error:
+ # Same documented terminal statuses as `/deploys/stats`: an action whose trace data
+ # aged out of retention between the actions listing and this query has nothing to
+ # report, and AppSignal answers 422 for a query it cannot run rather than an empty list.
+ if error.response is None or error.response.status_code not in (404, 422):
+ raise
+ rows = []
rows = [row for row in rows if isinstance(row, dict)]
if len(rows) >= V2_PAGE_SIZE and (window_before - window_since) >= 2 * MIN_WINDOW_SECONDS:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/tests/test_appsignal.py b/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/tests/test_appsignal.py
index 81a0036ed37b..661f90b76cf7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/tests/test_appsignal.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/tests/test_appsignal.py
@@ -510,6 +510,25 @@ class TestTraceRows:
("s-1", traces[1]["time"]),
]
+ @pytest.mark.parametrize("status_code", [404, 422])
+ @mock.patch(f"{MODULE}.TRACES_INITIAL_LOOKBACK_SECONDS", 3600)
+ def test_an_action_without_trace_data_is_skipped(self, status_code: int) -> None:
+ # Same documented terminal statuses as `/deploys/stats`: an action whose trace data aged
+ # out of retention between the actions listing and this query has nothing to report, and
+ # AppSignal answers 422 for a query it cannot run rather than an empty list.
+ result = _driver().run(
+ "performance_traces",
+ _v2_script(
+ post={
+ "/tracing/actions": [{"namespace": "web", "action": "UsersController#show"}],
+ "/tracing/traces/performance": status_code,
+ }
+ ),
+ )
+
+ assert result.raised is None
+ assert result.rows == []
+
@mock.patch(f"{MODULE}.MAX_SPAN_TRACES_PER_SYNC", 1)
@mock.patch(f"{MODULE}.TRACES_INITIAL_LOOKBACK_SECONDS", 3600)
def test_span_cap_stops_the_sweep_and_checkpoints_the_last_trace(self) -> None: