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(tasks): keep budget steer capture on a supported redis set (#105952)

posthog[bot]9d6b1eff1fd0
diff --git a/products/tasks/backend/logic/stream/budget_steer.py b/products/tasks/backend/logic/stream/budget_steer.py
index 6f6bdb232962..2f904e0827d5 100644
--- a/products/tasks/backend/logic/stream/budget_steer.py
+++ b/products/tasks/backend/logic/stream/budget_steer.py
@@ -45,15 +45,12 @@ class BudgetSteerCapture:
or cls._parse_timestamp(properties.get("threshold_at"))
or timezone.now()
).isoformat()
- original_timestamp = redis.set(
- f"task-run-budget-steer:{event_uuid}:timestamp",
- timestamp,
- nx=True,
- get=True,
- ex=BUDGET_STEER_RETENTION_SECONDS,
- )
- if original_timestamp is not None:
- timestamp = original_timestamp.decode() if isinstance(original_timestamp, bytes) else original_timestamp
+ timestamp_key = f"task-run-budget-steer:{event_uuid}:timestamp"
+ # Redis before 7.0 rejects SET with NX and GET together, so read the first value back separately.
+ if not redis.set(timestamp_key, timestamp, nx=True, ex=BUDGET_STEER_RETENTION_SECONDS):
+ original_timestamp = redis.get(timestamp_key)
+ if original_timestamp is not None:
+ timestamp = original_timestamp.decode() if isinstance(original_timestamp, bytes) else original_timestamp
current_app.send_task(
BUDGET_STEER_CAPTURE_TASK,
kwargs={"team_id": team_id, "event_uuid": event_uuid, "timestamp": timestamp, "properties": properties},
diff --git a/products/tasks/backend/tests/test_budget_steer.py b/products/tasks/backend/tests/test_budget_steer.py
index fc74ffe4ddfa..1c62690d9c64 100644
--- a/products/tasks/backend/tests/test_budget_steer.py
+++ b/products/tasks/backend/tests/test_budget_steer.py
@@ -5,6 +5,7 @@ from unittest.mock import patch
from django.test import SimpleTestCase, override_settings
+import fakeredis
from parameterized import parameterized
from posthog.ph_client import get_client
@@ -31,12 +32,24 @@ class TestBudgetSteerCapture(SimpleTestCase):
@parameterized.expand(
[
- (None, "2026-01-01T23:59:59+00:00"),
- ("2026-01-01T23:59:58.000Z", "2026-01-01T23:59:58+00:00"),
+ ((6, 2), None, "2026-01-01T23:59:59+00:00"),
+ ((6, 2), "2026-01-01T23:59:58.000Z", "2026-01-01T23:59:58+00:00"),
+ ((7, 4), None, "2026-01-01T23:59:59+00:00"),
+ ((7, 4), "2026-01-01T23:59:58.000Z", "2026-01-01T23:59:58+00:00"),
]
)
- def test_replays_preserve_timestamp_across_midnight(self, event_timestamp: str | None, expected: str) -> None:
- with patch("products.tasks.backend.logic.stream.budget_steer.current_app.send_task") as dispatch:
+ def test_replays_preserve_timestamp_across_midnight(
+ self, redis_version: tuple[int, int], event_timestamp: str | None, expected: str
+ ) -> None:
+ # The shared fake client speaks the newest command set, which hides commands older servers reject.
+ redis = fakeredis.FakeRedis(server=fakeredis.FakeServer(version=redis_version))
+ with (
+ patch(
+ "products.tasks.backend.logic.stream.budget_steer.get_tasks_stream_redis_sync",
+ return_value=redis,
+ ),
+ patch("products.tasks.backend.logic.stream.budget_steer.current_app.send_task") as dispatch,
+ ):
with time_machine.travel("2026-01-01T23:59:59Z", tick=False):
BudgetSteerCapture.enqueue(1, self.run_id, 7, self.properties, event_timestamp)
with time_machine.travel("2026-01-02T00:00:01Z", tick=False):