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.

fix(customer-analytics): emit sync metrics through SDK (#89226)

Arthur Moreira de Deus4dd4e609c044
diff --git a/products/customer_analytics/backend/metrics.py b/products/customer_analytics/backend/metrics.py
index c89084ed0f6a..89cc24f05e50 100644
--- a/products/customer_analytics/backend/metrics.py
+++ b/products/customer_analytics/backend/metrics.py
@@ -1,3 +1,4 @@
+import posthoganalytics
from prometheus_client import Counter, Gauge, Histogram
from posthog.otel_metrics import OtelInstrumentFactory
@@ -54,15 +55,22 @@ _ACCOUNT_PROPERTY_SYNC_PHASE_DURATION_SECONDS = Histogram(
)
_otel = OtelInstrumentFactory("customer-analytics-account-track-rules")
-_account_property_sync_otel = OtelInstrumentFactory("customer-analytics-account-property-sync")
def record_account_property_sync_phase_duration(*, phase: str, segment: str, duration_seconds: float) -> None:
labels = {"phase": phase, "segment": segment}
_ACCOUNT_PROPERTY_SYNC_PHASE_DURATION_SECONDS.labels(**labels).observe(duration_seconds)
- _account_property_sync_otel.record_histogram_twin(
- _ACCOUNT_PROPERTY_SYNC_PHASE_DURATION_SECONDS, duration_seconds, labels
- )
+ client = posthoganalytics.default_client
+ if client is not None:
+ try:
+ client.metrics.histogram(
+ "customer_analytics_account_property_sync_phase_duration_seconds",
+ duration_seconds,
+ unit="s",
+ attributes=labels,
+ )
+ except Exception:
+ pass
def record_account_track_rule_run(
diff --git a/products/customer_analytics/backend/metrics_test.py b/products/customer_analytics/backend/metrics_test.py
new file mode 100644
index 000000000000..32ec1b411259
--- /dev/null
+++ b/products/customer_analytics/backend/metrics_test.py
@@ -0,0 +1,33 @@
+from unittest.mock import MagicMock, patch
+
+from products.customer_analytics.backend.metrics import record_account_property_sync_phase_duration
+
+
+def test_account_property_sync_phase_duration_records_posthog_metric() -> None:
+ client = MagicMock()
+
+ with patch("products.customer_analytics.backend.metrics.posthoganalytics.default_client", client):
+ record_account_property_sync_phase_duration(
+ phase="apply_values",
+ segment="tracked",
+ duration_seconds=1.25,
+ )
+
+ client.metrics.histogram.assert_called_once_with(
+ "customer_analytics_account_property_sync_phase_duration_seconds",
+ 1.25,
+ unit="s",
+ attributes={"phase": "apply_values", "segment": "tracked"},
+ )
+
+
+def test_account_property_sync_phase_duration_ignores_metric_failures() -> None:
+ client = MagicMock()
+ client.metrics.histogram.side_effect = RuntimeError()
+
+ with patch("products.customer_analytics.backend.metrics.posthoganalytics.default_client", client):
+ record_account_property_sync_phase_duration(
+ phase="apply_values",
+ segment="tracked",
+ duration_seconds=1.25,
+ )