Skip to content

Commit

Permalink
fix(queries): Do not count cache hits by warming itself (#24192)
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie authored Aug 6, 2024
1 parent 0e64d85 commit ff8a4a1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions posthog/hogql_queries/query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ def get_async_query_status(self, *, cache_key: str) -> Optional[QueryStatus]:
except QueryNotFoundError:
return None

def count_query_cache_hit(self, hit: str, trigger: str = "") -> None:
if (get_query_tag_value("trigger") or "").startswith("warming"):
# We don't want to count for cache hits caused by warming itself
return
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit=hit, trigger=trigger).inc()

def handle_cache_and_async_logic(
self, execution_mode: ExecutionMode, cache_manager: QueryCacheManager, user: Optional[User] = None
) -> Optional[CR | CacheMissResponse]:
Expand All @@ -479,18 +485,12 @@ def handle_cache_and_async_logic(
assert isinstance(cached_response, CachedResponse)

if not self._is_stale(last_refresh=last_refresh_from_cached_result(cached_response)):
QUERY_CACHE_HIT_COUNTER.labels(
team_id=self.team.pk,
cache_hit="hit",
trigger=cached_response.calculation_trigger or "",
).inc()
self.count_query_cache_hit(hit="hit", trigger=cached_response.calculation_trigger or "")
# We have a valid result that's fresh enough, let's return it
cached_response.query_status = self.get_async_query_status(cache_key=cache_manager.cache_key)
return cached_response

QUERY_CACHE_HIT_COUNTER.labels(
team_id=self.team.pk, cache_hit="stale", trigger=cached_response.calculation_trigger or ""
).inc()
self.count_query_cache_hit(hit="stale", trigger=cached_response.calculation_trigger or "")
# We have a stale result. If we aren't allowed to calculate, let's still return it
# – otherwise let's proceed to calculation
if execution_mode == ExecutionMode.CACHE_ONLY_NEVER_CALCULATE:
Expand All @@ -512,7 +512,7 @@ def handle_cache_and_async_logic(
cached_response.query_status = self.get_async_query_status(cache_key=cache_manager.cache_key)
return cached_response
else:
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit="miss", trigger="").inc()
self.count_query_cache_hit(hit="miss", trigger="")
# We have no cached result. If we aren't allowed to calculate, let's return the cache miss
# – otherwise let's proceed to calculation
if execution_mode == ExecutionMode.CACHE_ONLY_NEVER_CALCULATE:
Expand Down

0 comments on commit ff8a4a1

Please sign in to comment.