Skip to content

Commit b730e54

Browse files
dustinbyrneadamleithp
authored andcommitted
chore(cohorts): Pause cohort dependency metrics on cache warm (#39123)
1 parent b02a825 commit b730e54

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

posthog/models/cohort/dependencies.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def extract_cohort_dependencies(cohort: Cohort) -> set[int]:
4040
return dependencies
4141

4242

43-
def get_cohort_dependencies(cohort: Cohort) -> list[int]:
43+
def get_cohort_dependencies(cohort: Cohort, _warming: bool = False) -> list[int]:
4444
"""
4545
Get the list of cohort IDs that the given cohort depends on.
4646
"""
@@ -50,10 +50,11 @@ def get_cohort_dependencies(cohort: Cohort) -> list[int]:
5050
cache_hit = cache.has_key(cache_key)
5151

5252
def compute_dependencies():
53-
COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss").inc()
53+
if not _warming:
54+
COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss").inc()
5455
return list(extract_cohort_dependencies(cohort))
5556

56-
if cache_hit:
57+
if cache_hit and not _warming:
5758
COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit").inc()
5859

5960
result = cache.get_or_set(
@@ -114,7 +115,7 @@ def warm_team_cohort_dependency_cache(team_id: int, batch_size: int = 1000):
114115
for cohort in Cohort.objects.filter(team_id=team_id, deleted=False).iterator(chunk_size=batch_size):
115116
# Any invalidated dependencies cache is rebuilt here
116117
dependents_map.setdefault(_cohort_dependents_key(cohort.id), [])
117-
dependencies = get_cohort_dependencies(cohort)
118+
dependencies = get_cohort_dependencies(cohort, _warming=True)
118119
# Dependency keys aren't fully invalidated; make sure they don't expire.
119120
cache.touch(_cohort_dependencies_key(cohort.id), timeout=DEPENDENCY_CACHE_TIMEOUT)
120121
# Build reverse map

posthog/models/cohort/test/test_dependencies.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,23 @@ def test_cache_hit_miss_sequence(self):
390390
cache_type="dependents", result="hit"
391391
)._value._value
392392
self.assertEqual(dept_hits_after_second, dept_initial_hits + 1)
393+
394+
def test_warming_does_not_increment_counters(self):
395+
"""Verify that cache warming operations don't increment the counter metrics"""
396+
cohort_a = self._create_cohort(name="Test Cohort A")
397+
self._create_cohort(
398+
name="Test Cohort B", groups=[{"properties": [{"key": "id", "type": "cohort", "value": cohort_a.id}]}]
399+
)
400+
401+
cache.clear()
402+
403+
initial_hits = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit")._value._value
404+
initial_misses = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss")._value._value
405+
406+
warm_team_cohort_dependency_cache(self.team.id)
407+
408+
# Verify counters did not increment during warming
409+
final_hits = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit")._value._value
410+
final_misses = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss")._value._value
411+
self.assertEqual(final_hits, initial_hits)
412+
self.assertEqual(final_misses, initial_misses)

0 commit comments

Comments
 (0)