Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(llma): add cache_name label to hypercache signal-update counter (#64129)

Phil Haackad2d1d101bd0
diff --git a/posthog/storage/gateway_credential_signal_handlers.py b/posthog/storage/gateway_credential_signal_handlers.py
index 55a43db39b1e..5b65ac9d9d07 100644
--- a/posthog/storage/gateway_credential_signal_handlers.py
+++ b/posthog/storage/gateway_credential_signal_handlers.py
@@ -42,7 +42,7 @@ from posthog.storage.hypercache_manager import HYPERCACHE_SIGNAL_UPDATE_COUNTER
logger = structlog.get_logger(__name__)
-_NAMESPACE = "gateway_credential"
+_CACHE_NAME = "gateway_credential"
_LOADED_HASH_ATTR = "_fp_loaded_hash"
_LOADED_ELIGIBLE_ATTR = "_fp_loaded_eligible"
@@ -162,7 +162,9 @@ def _on_credential_save(
# Scope was removed: clear promptly rather than waiting for a task.
clear_gateway_credential(new_hash)
except Exception as e:
- HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(namespace=_NAMESPACE, operation="enqueue", result="failure").inc()
+ HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
+ namespace="team_metadata", cache_name=_CACHE_NAME, operation="enqueue", result="failure"
+ ).inc()
logger.exception("Failed to enqueue gateway credential cache update", kind=kind, error=str(e))
# A sync clear/enqueue failed (e.g. transient Redis) — queue the task so a
# revoke self-heals in queue-time instead of waiting out the blob TTL. The
diff --git a/posthog/storage/hypercache_manager.py b/posthog/storage/hypercache_manager.py
index b5d686f5775b..ab76718ec066 100644
--- a/posthog/storage/hypercache_manager.py
+++ b/posthog/storage/hypercache_manager.py
@@ -57,7 +57,7 @@ CACHE_SIZE_SAMPLE_LIMIT = 1000
HYPERCACHE_SIGNAL_UPDATE_COUNTER = Counter(
"posthog_hypercache_signal_updates",
"Cache updates triggered by Django signals",
- labelnames=["namespace", "operation", "result"],
+ labelnames=["namespace", "cache_name", "operation", "result"],
)
diff --git a/posthog/storage/team_llm_gateway_policy_signal_handlers.py b/posthog/storage/team_llm_gateway_policy_signal_handlers.py
index 17ff21de9a17..3e2634894ae4 100644
--- a/posthog/storage/team_llm_gateway_policy_signal_handlers.py
+++ b/posthog/storage/team_llm_gateway_policy_signal_handlers.py
@@ -144,7 +144,10 @@ def _update_cache_on_save(sender: type[Team], instance: Team, created: bool, **k
clear_team_llm_gateway_policy_cache(instance, kinds=kinds)
except Exception as e:
HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
- namespace="team_llm_gateway_policy", operation="enqueue", result="failure"
+ namespace="team_metadata",
+ cache_name="llm_gateway_policy",
+ operation="enqueue",
+ result="failure",
).inc()
logger.exception(
"Failed to enqueue llm-gateway policy cache update",
diff --git a/posthog/storage/test/test_team_llm_gateway_policy_cache.py b/posthog/storage/test/test_team_llm_gateway_policy_cache.py
index 616d074a29e6..c58be398edb1 100644
--- a/posthog/storage/test/test_team_llm_gateway_policy_cache.py
+++ b/posthog/storage/test/test_team_llm_gateway_policy_cache.py
@@ -134,6 +134,29 @@ class TestLLMGatewayPolicySignals(BaseTest):
mock_delay.assert_not_called()
+ @patch("posthog.storage.team_llm_gateway_policy_signal_handlers.transaction")
+ @patch("posthog.storage.team_llm_gateway_policy_signal_handlers.HYPERCACHE_SIGNAL_UPDATE_COUNTER")
+ @patch("posthog.storage.team_llm_gateway_policy_signal_handlers.settings")
+ @patch("posthog.tasks.team_llm_gateway_policy.update_team_llm_gateway_policy_cache_task.delay")
+ def test_enqueue_failure_records_counter_and_does_not_propagate(
+ self, mock_delay, mock_settings, mock_counter, mock_transaction
+ ):
+ mock_settings.AI_GATEWAY_REDIS_URL = "redis://localhost"
+ mock_settings.TEST = True
+ mock_transaction.on_commit.side_effect = lambda fn: fn()
+ mock_delay.side_effect = Exception("broker down")
+
+ self.team.llm_gateway_revoked_at = datetime(2026, 5, 20, 12, 0, 0, tzinfo=UTC)
+ try:
+ self.team.save()
+ except Exception:
+ self.fail("enqueue failure should not propagate to the caller")
+
+ mock_counter.labels.assert_called_once_with(
+ namespace="team_metadata", cache_name="llm_gateway_policy", operation="enqueue", result="failure"
+ )
+ mock_counter.labels.return_value.inc.assert_called_once_with()
+
@patch("posthog.storage.team_llm_gateway_policy_signal_handlers.settings")
@patch("posthog.storage.team_llm_gateway_policy_signal_handlers.clear_team_llm_gateway_policy_cache")
def test_team_delete_clears_cache(self, mock_clear, mock_settings):
diff --git a/posthog/tasks/gateway_credential.py b/posthog/tasks/gateway_credential.py
index 5860513af990..fc96e6b25f92 100644
--- a/posthog/tasks/gateway_credential.py
+++ b/posthog/tasks/gateway_credential.py
@@ -29,7 +29,7 @@ from posthog.storage.hypercache_manager import HYPERCACHE_SIGNAL_UPDATE_COUNTER
logger = structlog.get_logger(__name__)
-_NAMESPACE = "gateway_credential"
+_CACHE_NAME = "gateway_credential"
_SECRET_KEY_KIND = "project_secret_api_key"
_OAUTH_KIND = "oauth_access_token"
@@ -51,7 +51,9 @@ def update_gateway_credential_cache_task(credential_kind: str, credential_id: st
return
project_gateway_credential(credential)
- HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(namespace=_NAMESPACE, operation="update", result="success").inc()
+ HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
+ namespace="team_metadata", cache_name=_CACHE_NAME, operation="update", result="success"
+ ).inc()
@shared_task(ignore_result=True, queue=CeleryQueue.DEFAULT.value)
diff --git a/posthog/tasks/team_llm_gateway_policy.py b/posthog/tasks/team_llm_gateway_policy.py
index b99b94f0d37d..5dd04a80b732 100644
--- a/posthog/tasks/team_llm_gateway_policy.py
+++ b/posthog/tasks/team_llm_gateway_policy.py
@@ -35,13 +35,14 @@ def update_team_llm_gateway_policy_cache_task(team_id: int) -> None:
except Team.DoesNotExist:
logger.debug("Team does not exist for llm-gateway policy cache update", team_id=team_id)
HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
- namespace="team_llm_gateway_policy", operation="update", result="failure"
+ namespace="team_metadata", cache_name="llm_gateway_policy", operation="update", result="failure"
).inc()
return
success = update_team_llm_gateway_policy_cache(team)
HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
- namespace="team_llm_gateway_policy",
+ namespace="team_metadata",
+ cache_name="llm_gateway_policy",
operation="update",
result="success" if success else "failure",
).inc()
diff --git a/posthog/tasks/team_metadata.py b/posthog/tasks/team_metadata.py
index 4265cf77769a..9b8987d70540 100644
--- a/posthog/tasks/team_metadata.py
+++ b/posthog/tasks/team_metadata.py
@@ -47,12 +47,17 @@ def update_team_metadata_cache_task(team_id: int) -> None:
team = Team.objects.get(id=team_id)
except Team.DoesNotExist:
logger.debug("Team does not exist for metadata cache update", team_id=team_id)
- HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(namespace="team_metadata", operation="update", result="failure").inc()
+ HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
+ namespace="team_metadata", cache_name="team_metadata", operation="update", result="failure"
+ ).inc()
return
success = update_team_metadata_cache(team)
HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
- namespace="team_metadata", operation="update", result="success" if success else "failure"
+ namespace="team_metadata",
+ cache_name="team_metadata",
+ operation="update",
+ result="success" if success else "failure",
).inc()
@@ -165,7 +170,9 @@ def clear_team_metadata_cache_on_delete(sender: type[Team], instance: Team, **kw
def _record_enqueue_failure() -> None:
- HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(namespace="team_metadata", operation="enqueue", result="failure").inc()
+ HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
+ namespace="team_metadata", cache_name="team_metadata", operation="enqueue", result="failure"
+ ).inc()
def _name_may_have_changed(update_fields: frozenset[str] | None) -> bool:
diff --git a/posthog/tasks/test/test_team_metadata_signals.py b/posthog/tasks/test/test_team_metadata_signals.py
index aa6c59169576..a07b6b96a1ad 100644
--- a/posthog/tasks/test/test_team_metadata_signals.py
+++ b/posthog/tasks/test/test_team_metadata_signals.py
@@ -63,6 +63,22 @@ class TestTeamMetadataInvalidationSignals(BaseTest):
mock_task.delay.assert_not_called()
+ def test_enqueue_failure_records_counter_and_does_not_propagate(self) -> None:
+ with patch("posthog.tasks.team_metadata.update_related_teams_metadata_cache_task") as mock_task:
+ mock_task.delay.side_effect = Exception("broker down")
+ with patch("posthog.tasks.team_metadata.HYPERCACHE_SIGNAL_UPDATE_COUNTER") as mock_counter:
+ try:
+ with self.captureOnCommitCallbacks(execute=True):
+ self.organization.name = "Renamed org"
+ self.organization.save(update_fields=["name"])
+ except Exception:
+ self.fail("enqueue failure should not propagate to the caller")
+
+ mock_counter.labels.assert_called_once_with(
+ namespace="team_metadata", cache_name="team_metadata", operation="enqueue", result="failure"
+ )
+ mock_counter.labels.return_value.inc.assert_called_once_with()
+
@override_settings(FLAGS_REDIS_URL="redis://localhost:6379")
class TestRelatedTeamsMetadataFanoutTask(BaseTest):
diff --git a/products/feature_flags/backend/tasks.py b/products/feature_flags/backend/tasks.py
index 0a16c293ef7f..19f9c1452724 100644
--- a/products/feature_flags/backend/tasks.py
+++ b/products/feature_flags/backend/tasks.py
@@ -60,12 +60,14 @@ def update_team_service_flags_cache(team_id: int) -> None:
team = Team.objects.get(id=team_id)
except Team.DoesNotExist:
logger.debug("Team does not exist for service flags cache update", team_id=team_id)
- HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(namespace="feature_flags", operation="update", result="failure").inc()
+ HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
+ namespace="feature_flags", cache_name="flags", operation="update", result="failure"
+ ).inc()
return
success = update_flags_cache(team)
HYPERCACHE_SIGNAL_UPDATE_COUNTER.labels(
- namespace="feature_flags", operation="update", result="success" if success else "failure"
+ namespace="feature_flags", cache_name="flags", operation="update", result="success" if success else "failure"
).inc()