Skip to content

Commit

Permalink
feat(decide): add NEW_ANALYTICS_CAPTURE_SAMPLING_RATE option for slow…
Browse files Browse the repository at this point in the history
…er rollout (#18203)
  • Loading branch information
xvello authored Oct 26, 2023
1 parent f7fd536 commit eff6dc7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
3 changes: 2 additions & 1 deletion posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def get_decide(request: HttpRequest):
)

if settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS and str(team.id) in settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS:
response["analytics"] = {"endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT}
if random() < settings.NEW_ANALYTICS_CAPTURE_SAMPLING_RATE:
response["analytics"] = {"endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT}

if team.session_recording_opt_in and (
on_permitted_recording_domain(team, request) or not team.recording_domains
Expand Down
58 changes: 40 additions & 18 deletions posthog/api/test/test_decide.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import base64
import json
import random
from unittest.mock import patch
import time
from django.conf import settings

from unittest.mock import patch

import pytest
from django.conf import settings
from django.core.cache import cache
from django.db import connection, connections
from django.test import TransactionTestCase, TestCase
from django.test.client import Client
from rest_framework.test import APIClient
from freezegun import freeze_time
import pytest
from rest_framework import status
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride
from posthog.models.group.group import Group

from rest_framework.test import APIClient

from posthog.api.test.test_feature_flag import QueryTimeoutWrapper
from posthog import redis
from posthog.api.decide import label_for_team_id_to_track
from posthog.api.test.test_feature_flag import QueryTimeoutWrapper
from posthog.database_healthcheck import postgres_healthcheck
from posthog.models import (
FeatureFlag,
GroupTypeMapping,
Expand All @@ -30,21 +28,16 @@
PluginSourceFile,
)
from posthog.models.cohort.cohort import Cohort
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride
from posthog.models.group.group import Group
from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.person import PersonDistinctId
from posthog.models.personal_api_key import hash_key_value
from posthog.models.plugin import sync_team_inject_web_apps
from posthog.models.team.team import Team
from posthog.models.person import PersonDistinctId
from posthog.models.user import User
from posthog.models.utils import generate_random_token_personal
from posthog.test.base import (
BaseTest,
QueryMatchingTest,
snapshot_postgres_queries,
snapshot_postgres_queries_context,
)
from posthog.database_healthcheck import postgres_healthcheck
from posthog import redis
from posthog.test.base import BaseTest, QueryMatchingTest, snapshot_postgres_queries, snapshot_postgres_queries_context


@patch(
Expand Down Expand Up @@ -2943,6 +2936,35 @@ def test_decide_analytics_fire_for_survey_targeting_flags(self, *args):
{b"165192618": b"1"},
)

@patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_decide_new_capture_activation(self, *args):
self.client.logout()
with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue("analytics" in response.json())
self.assertEqual(response.json()["analytics"]["endpoint"], "/i/v0/e/")

with self.settings(
NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)},
NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0,
NEW_ANALYTICS_CAPTURE_ENDPOINT="/custom",
):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue("analytics" in response.json())
self.assertEqual(response.json()["analytics"]["endpoint"], "/custom")

with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={"0"}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertFalse("analytics" in response.json())

with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertFalse("analytics" in response.json())


class TestDatabaseCheckForDecide(BaseTest, QueryMatchingTest):
"""
Expand Down
1 change: 1 addition & 0 deletions posthog/settings/ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@

NEW_ANALYTICS_CAPTURE_ENDPOINT = os.getenv("NEW_CAPTURE_ENDPOINT", "/i/v0/e/")
NEW_ANALYTICS_CAPTURE_TEAM_IDS = get_set(os.getenv("NEW_ANALYTICS_CAPTURE_TEAM_IDS", ""))
NEW_ANALYTICS_CAPTURE_SAMPLING_RATE = get_from_env("NEW_ANALYTICS_CAPTURE_SAMPLING_RATE", type_cast=float, default=1.0)

0 comments on commit eff6dc7

Please sign in to comment.