Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(decide): add NEW_ANALYTICS_CAPTURE_SAMPLING_RATE option for slower rollout #18203

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
xvello marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading