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.

feat(logs): dry run for the logs alert cutover command (#121530)

Jimmy Zhu7305098fb23c
diff --git a/products/logs/backend/management/commands/cut_over_logs_alerts_to_platform.py b/products/logs/backend/management/commands/cut_over_logs_alerts_to_platform.py
index 2fc8e5ec2457..b572ee120ee3 100644
--- a/products/logs/backend/management/commands/cut_over_logs_alerts_to_platform.py
+++ b/products/logs/backend/management/commands/cut_over_logs_alerts_to_platform.py
@@ -12,6 +12,11 @@ class Command(BaseCommand):
def add_arguments(self, parser: Any) -> None:
parser.add_argument("--team-id", type=int, required=True, help="The team to move.")
+ parser.add_argument(
+ "--dry-run",
+ action="store_true",
+ help="Report what a real run would do, and move nothing.",
+ )
def handle(self, *args: Any, **options: Any) -> None:
team_id = options["team_id"]
@@ -21,10 +26,12 @@ class Command(BaseCommand):
for alert in paths.alerts:
if alert.choice.reason is not None:
self.stdout.write(f"alert={alert.alert_id} path={alert.choice.path} reason={alert.choice.reason}")
- results = cut_over_team(team_id)
+ dry_run = options["dry_run"]
+ results = cut_over_team(team_id, dry_run=dry_run)
for result in results:
if result.skipped is not None:
self.stdout.write(f"alert={result.alert_id} skipped={result.skipped}")
delivery = Counter(str(alert.choice.path) for alert in paths.alerts)
- outcomes = Counter(str(result.skipped or "moved") for result in results)
+ moved = "would_move" if dry_run else "moved"
+ outcomes = Counter(str(result.skipped or moved) for result in results)
self.stdout.write(f"team={team_id} {dict(outcomes) or 'no alerts'} paths={dict(delivery)}")
diff --git a/products/logs/backend/platform_cutover.py b/products/logs/backend/platform_cutover.py
index 9cb7d43806ff..33d305b239af 100644
--- a/products/logs/backend/platform_cutover.py
+++ b/products/logs/backend/platform_cutover.py
@@ -68,32 +68,38 @@ def _logs_check_owed(alert_id: UUID, at: datetime) -> bool:
)
-def cut_over_team(team_id: int, *, now: datetime | None = None) -> tuple[AlertCutover, ...]:
+def cut_over_team(team_id: int, *, now: datetime | None = None, dry_run: bool = False) -> tuple[AlertCutover, ...]:
"""Hands every logs alert of one team to the platform, or says why one stays.
No destination holds an alert back: delivery sends a customized one through its HogFunction.
+ A dry run decides each alert exactly as a real run would, then rolls its transaction back.
"""
return tuple(
- AlertCutover(alert_id=alert_id, skipped=_cut_over_alert(alert_id, now)) for alert_id in _team_alert_ids(team_id)
+ AlertCutover(alert_id=alert_id, skipped=_cut_over_alert(alert_id, now, dry_run=dry_run))
+ for alert_id in _team_alert_ids(team_id)
)
-def _cut_over_alert(alert_id: UUID, now: datetime | None) -> CutoverSkip | None:
+def _cut_over_alert(alert_id: UUID, now: datetime | None, *, dry_run: bool) -> CutoverSkip | None:
with transaction.atomic():
- alert = LogsAlertConfiguration.objects.select_for_update().get(id=alert_id)
- # Read after the lock, so a check that became due while earlier alerts moved is seen as due.
- at = now or timezone.now()
- if alert.platform_owned:
- return CutoverSkip.ALREADY_MOVED
- if in_an_environment(alert):
- return CutoverSkip.ENVIRONMENT
- if alert.state in FIRING_STATES:
- return CutoverSkip.FIRING
- if _logs_check_owed(alert.id, at):
- return CutoverSkip.CHECK_DUE
-
- if not hand_over(alert, at):
- return CutoverSkip.PLATFORM_FIRING
+ try:
+ alert = LogsAlertConfiguration.objects.select_for_update().get(id=alert_id)
+ # Read after the lock, so a check that became due while earlier alerts moved is seen as due.
+ at = now or timezone.now()
+ if alert.platform_owned:
+ return CutoverSkip.ALREADY_MOVED
+ if in_an_environment(alert):
+ return CutoverSkip.ENVIRONMENT
+ if alert.state in FIRING_STATES:
+ return CutoverSkip.FIRING
+ if _logs_check_owed(alert.id, at):
+ return CutoverSkip.CHECK_DUE
+ if not hand_over(alert, at):
+ return CutoverSkip.PLATFORM_FIRING
+ finally:
+ # After the last query, because a block marked for rollback refuses queries.
+ if dry_run:
+ transaction.set_rollback(True)
return None
diff --git a/products/logs/backend/test/test_platform_cutover.py b/products/logs/backend/test/test_platform_cutover.py
index 656de659d611..978a16d91a7c 100644
--- a/products/logs/backend/test/test_platform_cutover.py
+++ b/products/logs/backend/test/test_platform_cutover.py
@@ -12,7 +12,7 @@ from posthog.models.team import Team
from products.alerts.backend.facade.contracts import HogFunctionPathReason
from products.alerts_platform.backend.facade import testing as platform_testing
-from products.alerts_platform.backend.facade.api import get_configuration, upsert_configuration
+from products.alerts_platform.backend.facade.api import get_configuration, list_configurations, upsert_configuration
from products.alerts_platform.backend.facade.contracts import DeliveryPath, SourceKind
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
from products.cdp.backend.models.hog_functions.hog_function_revision import HogFunctionRevision
@@ -139,6 +139,22 @@ class TestPlatformCutover(APIBaseTest):
with team_scope(self.team.id):
assert platform_testing.copy_of(alert.id).owner == "source"
+ def test_a_dry_run_decides_as_a_real_run_and_changes_nothing(self) -> None:
+ alert = self._alert()
+
+ # Kafka is not transactional, so an event produced inside the dry run would outlive its rollback.
+ with patch("products.cdp.backend.internal_events.produce_internal_event") as produced:
+ (dry,) = cut_over_team(self.team.id, now=NOW, dry_run=True)
+
+ produced.assert_not_called()
+ assert dry.skipped is None
+ alert.refresh_from_db()
+ assert (alert.owner, alert.owner_changed_at) == ("source", None)
+ copies = list_configurations(team_id=self.team.id, source_kinds=[SourceKind.LOGS], limit=10, offset=0)
+ assert copies.configurations == ()
+ (real,) = cut_over_team(self.team.id, now=NOW)
+ assert real.skipped == dry.skipped
+
def test_a_second_run_leaves_a_moved_alert_alone(self) -> None:
self._alert()
cut_over_team(self.team.id, now=NOW)