Skip to content

Commit

Permalink
add trace
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Nov 23, 2023
1 parent 25a1ee4 commit 3d3cdfa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
84 changes: 43 additions & 41 deletions posthog/api/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json

from django.db import DatabaseError
from sentry_sdk import start_span
import structlog

from posthog.models.feature_flag.flag_matching import (
Expand Down Expand Up @@ -625,48 +626,49 @@ def get_cohort_actors_for_feature_flag(cohort_id: int, flag: str, team_id: int,
if len(all_persons) == 0:
break

for person in all_persons:
# ignore almost-deleted persons / persons with no distinct ids
if len(person.distinct_ids) == 0:
continue

distinct_id = person.distinct_ids[0]
person_overrides = {}
if feature_flag.ensure_experience_continuity:
# :TRICKY: This is inefficient because it tries to get the hashkey overrides one by one.
# But reusing functions is better for maintainability. Revisit optimising if this becomes a bottleneck.
person_overrides = get_feature_flag_hash_key_overrides(
team_id, [distinct_id], person_id_to_distinct_id_mapping={person.id: distinct_id}
)
with start_span(op="batch_flag_matching_with_overrides"):
for person in all_persons:
# ignore almost-deleted persons / persons with no distinct ids
if len(person.distinct_ids) == 0:
continue

distinct_id = person.distinct_ids[0]
person_overrides = {}
if feature_flag.ensure_experience_continuity:
# :TRICKY: This is inefficient because it tries to get the hashkey overrides one by one.
# But reusing functions is better for maintainability. Revisit optimising if this becomes a bottleneck.
person_overrides = get_feature_flag_hash_key_overrides(
team_id, [distinct_id], person_id_to_distinct_id_mapping={person.id: distinct_id}
)

try:
match = FeatureFlagMatcher(
[feature_flag],
distinct_id,
groups={},
cache=matcher_cache,
hash_key_overrides=person_overrides,
property_value_overrides={**default_person_properties, **person.properties},
group_property_value_overrides={},
cohorts_cache=cohorts_cache,
).get_match(feature_flag)
if match.match:
uuids_to_add_to_cohort.append(str(person.uuid))
except (DatabaseError, ValueError, ValidationError):
logger.exception(
"Error evaluating feature flag for person", person_uuid=str(person.uuid), team_id=team_id
)
except Exception as err:
# matching errors are not fatal, so we just log them and move on.
# Capturing in sentry for now just in case there are some unexpected errors
# we did not account for.
capture_exception(err)

if len(uuids_to_add_to_cohort) >= batchsize:
cohort.insert_users_list_by_uuid(
uuids_to_add_to_cohort, insert_in_clickhouse=True, batchsize=batchsize
)
uuids_to_add_to_cohort = []
try:
match = FeatureFlagMatcher(
[feature_flag],
distinct_id,
groups={},
cache=matcher_cache,
hash_key_overrides=person_overrides,
property_value_overrides={**default_person_properties, **person.properties},
group_property_value_overrides={},
cohorts_cache=cohorts_cache,
).get_match(feature_flag)
if match.match:
uuids_to_add_to_cohort.append(str(person.uuid))
except (DatabaseError, ValueError, ValidationError):
logger.exception(
"Error evaluating feature flag for person", person_uuid=str(person.uuid), team_id=team_id
)
except Exception as err:
# matching errors are not fatal, so we just log them and move on.
# Capturing in sentry for now just in case there are some unexpected errors
# we did not account for.
capture_exception(err)

if len(uuids_to_add_to_cohort) >= batchsize:
cohort.insert_users_list_by_uuid(
uuids_to_add_to_cohort, insert_in_clickhouse=True, batchsize=batchsize
)
uuids_to_add_to_cohort = []

start += batchsize
batch_of_persons = queryset[start : start + batchsize]
Expand Down
8 changes: 8 additions & 0 deletions posthog/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def traces_sampler(sampling_context: dict) -> float:
else:
# Default sample rate for Celery tasks
return 0.001 # 0.1%
elif op == "queue.task.celery":
task = sampling_context.get("celery_job", {}).get("task")
if task == "posthog.tasks.calculate_cohort.insert_cohort_from_feature_flag":
# sample all cohort calculations via feature flag
return 1
# Default sample rate
return 0.01

else:
# Default sample rate for everything else
return 0.01 # 1%
Expand Down

0 comments on commit 3d3cdfa

Please sign in to comment.