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(logs): add metrics for alerting cohort size tracking (#57794)

Jon McCallume57afa5a8613
diff --git a/posthog/temporal/common/worker.py b/posthog/temporal/common/worker.py
index 0f67d9f635a2..14ce365d4299 100644
--- a/posthog/temporal/common/worker.py
+++ b/posthog/temporal/common/worker.py
@@ -49,6 +49,8 @@ from posthog.temporal.session_replay.delete_recordings.metrics import (
from products.batch_exports.backend.temporal.metrics import BatchExportsMetricsInterceptor
from products.logs.backend.temporal.metrics import (
+ LOGS_ALERTING_COUNT_HISTOGRAM_BUCKETS,
+ LOGS_ALERTING_COUNT_HISTOGRAM_METRICS,
LOGS_ALERTING_LATENCY_HISTOGRAM_BUCKETS,
LOGS_ALERTING_LATENCY_HISTOGRAM_METRICS,
LogsAlertingMetricsInterceptor,
@@ -249,6 +251,7 @@ async def create_worker(
)
)
| dict(zip(LOGS_ALERTING_LATENCY_HISTOGRAM_METRICS, itertools.repeat(LOGS_ALERTING_LATENCY_HISTOGRAM_BUCKETS)))
+ | dict(zip(LOGS_ALERTING_COUNT_HISTOGRAM_METRICS, itertools.repeat(LOGS_ALERTING_COUNT_HISTOGRAM_BUCKETS)))
| {"batch_exports_activity_attempt": [1.0, 5.0, 10.0, 100.0]}
)
if task_queue == settings.DATA_MODELING_TASK_QUEUE:
diff --git a/products/logs/backend/temporal/metrics.py b/products/logs/backend/temporal/metrics.py
index 953f6c3af696..dd7d881e2ec3 100644
--- a/products/logs/backend/temporal/metrics.py
+++ b/products/logs/backend/temporal/metrics.py
@@ -46,6 +46,20 @@ LOGS_ALERTING_LATENCY_HISTOGRAM_METRICS = (
"logs_alerting_cohort_update_ms",
)
+LOGS_ALERTING_COUNT_HISTOGRAM_METRICS = ("logs_alerting_cohort_size",)
+
+LOGS_ALERTING_COUNT_HISTOGRAM_BUCKETS = [
+ 1.0,
+ 5.0,
+ 10.0,
+ 20.0,
+ 30.0,
+ 50.0,
+ 100.0,
+ 250.0,
+ 500.0,
+]
+
LOGS_ALERTING_LATENCY_HISTOGRAM_BUCKETS = [
100.0,
500.0,
@@ -189,11 +203,14 @@ def record_cohort_update_duration(duration_ms: int) -> None:
def record_cohort_size(size: int) -> None:
- _record_histogram(
- "logs_alerting_cohort_size",
- "Number of alerts in a cohort sharing one batched ClickHouse query and one bulk Postgres save",
- size,
+ """Record a non-timedelta histogram observation — cohort size is a count, not a duration."""
+ meter = get_metric_meter()
+ hist = meter.create_histogram(
+ name="logs_alerting_cohort_size",
+ description="Number of alerts in a cohort sharing one batched ClickHouse query and one bulk Postgres save",
+ unit="alerts",
)
+ hist.record(size)
CohortSaveFallbackReason = typing.Literal["integrity_error"]
diff --git a/products/logs/backend/test/test_logs_alerting_metrics.py b/products/logs/backend/test/test_logs_alerting_metrics.py
index 63554c7b2165..4253bec383cd 100644
--- a/products/logs/backend/test/test_logs_alerting_metrics.py
+++ b/products/logs/backend/test/test_logs_alerting_metrics.py
@@ -222,13 +222,6 @@ class TestRecordCohortSaveSubstageDurations:
"Postgres bulk_update time for LogsAlertConfiguration rows in a cohort",
28,
),
- (
- "cohort_size",
- record_cohort_size,
- "logs_alerting_cohort_size",
- "Number of alerts in a cohort sharing one batched ClickHouse query and one bulk Postgres save",
- 17,
- ),
]
)
@patch("products.logs.backend.temporal.metrics._record_histogram")
@@ -239,6 +232,26 @@ class TestRecordCohortSaveSubstageDurations:
mock_record.assert_called_once_with(metric_name, description, sample_value)
+class TestRecordCohortSize:
+ @patch("products.logs.backend.temporal.metrics.get_metric_meter")
+ def test_records_count_histogram(self, mock_get_meter: MagicMock):
+ mock_meter = MagicMock()
+ mock_hist = MagicMock()
+ mock_meter.create_histogram.return_value = mock_hist
+ mock_get_meter.return_value = mock_meter
+
+ record_cohort_size(17)
+
+ kwargs = mock_meter.create_histogram.call_args.kwargs
+ assert kwargs["name"] == "logs_alerting_cohort_size"
+ assert kwargs["unit"] == "alerts"
+ assert (
+ kwargs["description"]
+ == "Number of alerts in a cohort sharing one batched ClickHouse query and one bulk Postgres save"
+ )
+ mock_hist.record.assert_called_once_with(17)
+
+
class TestIncrementCohortSaveFallback:
@patch("products.logs.backend.temporal.metrics.get_metric_meter")
def test_increments_counter_with_reason(self, mock_get_meter: MagicMock):