: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(slo): only send SLO events from cloud deployments (#93723)
Andy Zhao03b3920a64fa
diff --git a/posthog/slo/events.py b/posthog/slo/events.pyindex b81332c8231e..6afb1cbd379c 100644--- a/posthog/slo/events.py+++ b/posthog/slo/events.py@@ -1,8 +1,12 @@from collections.abc import Callable+from typing import Any++from django.conf import settingsimport posthoganalyticsfrom prometheus_client import Counter, Histogram+from posthog.cloud_utils import is_hobbyfrom posthog.exceptions_capture import capture_exceptionfrom posthog.git import get_git_commit_shortfrom posthog.slo.types import SloCompletedProperties, SloStartedProperties@@ -70,6 +74,15 @@ def _capture_emit_exception(pass+def _send(capture: Callable | None, *, distinct_id: str, event: str, properties: dict[str, Any]) -> None:+ # The module-level client carries PostHog's own project token on self-hosted installs too, so without this+ # guard they report SLO events into PostHog's internal project, with no region. Tests run in hobby mode with+ # that client patched, so they keep observing emission.+ if capture is None and is_hobby() and not settings.TEST:+ return+ (capture or posthoganalytics.capture)(distinct_id=distinct_id, event=event, properties=properties)++def emit_slo_started(distinct_id: str,properties: SloStartedProperties,@@ -86,12 +99,7 @@ def emit_slo_started(if sample_rate < 1.0:all_properties["sample_rate"] = sample_rate- _capture = capture or posthoganalytics.capture- _capture(- distinct_id=distinct_id,- event="slo_operation_started",- properties=all_properties,- )+ _send(capture, distinct_id=distinct_id, event="slo_operation_started", properties=all_properties)OPERATION_STARTED_COUNTER.labels(area=properties.area,operation=properties.operation,@@ -122,12 +130,7 @@ def emit_slo_completed(if sample_rate < 1.0:all_properties["sample_rate"] = sample_rate- _capture = capture or posthoganalytics.capture- _capture(- distinct_id=distinct_id,- event="slo_operation_completed",- properties=all_properties,- )+ _send(capture, distinct_id=distinct_id, event="slo_operation_completed", properties=all_properties)OPERATION_COMPLETED_COUNTER.labels(area=properties.area,operation=properties.operation,diff --git a/posthog/slo/test/test_slo_events.py b/posthog/slo/test/test_slo_events.pynew file mode 100644index 000000000000..d6428edef0c8--- /dev/null+++ b/posthog/slo/test/test_slo_events.py@@ -0,0 +1,45 @@+from collections.abc import Callable++from unittest.mock import patch++from django.test import override_settings++from parameterized import parameterized++from posthog.slo.events import emit_slo_completed, emit_slo_started+from posthog.slo.types import SloArea, SloCompletedProperties, SloOperation, SloOutcome, SloStartedProperties++STARTED = SloStartedProperties(area=SloArea.ANALYTIC_PLATFORM, operation=SloOperation.EXPORT, team_id=1)+COMPLETED = SloCompletedProperties(+ area=SloArea.ANALYTIC_PLATFORM, operation=SloOperation.EXPORT, team_id=1, outcome=SloOutcome.SUCCESS+)+++CLOUD = {"CLOUD_DEPLOYMENT": "US", "DEBUG": False, "TEST": False}+LOCAL_DEV = {"CLOUD_DEPLOYMENT": None, "DEBUG": True, "TEST": False}+SELF_HOSTED = {"CLOUD_DEPLOYMENT": None, "DEBUG": False, "TEST": False}+++ [+ ("started_on_cloud", emit_slo_started, STARTED, CLOUD, True),+ ("started_in_local_dev", emit_slo_started, STARTED, LOCAL_DEV, True),+ ("started_self_hosted", emit_slo_started, STARTED, SELF_HOSTED, False),+ ("completed_on_cloud", emit_slo_completed, COMPLETED, CLOUD, True),+ ("completed_self_hosted", emit_slo_completed, COMPLETED, SELF_HOSTED, False),+ ]+)+def test_slo_events_never_reach_posthog_from_self_hosted(+ _name: str,+ emit: Callable[..., None],+ properties: SloStartedProperties | SloCompletedProperties,+ run_mode_settings: dict[str, object],+ expected_sent: bool,+) -> None:+ with (+ override_settings(**run_mode_settings),+ patch("posthog.slo.events.posthoganalytics.capture") as capture,+ ):+ emit("distinct-id", properties)++ assert capture.called is expected_sent