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(replay): meter replay vision observations per team (#66037)

Paul D'Ambra5422778fb7da
diff --git a/ee/hogai/tools/read_billing_tool/tool.py b/ee/hogai/tools/read_billing_tool/tool.py
index 44030394eebd..348304d14537 100644
--- a/ee/hogai/tools/read_billing_tool/tool.py
+++ b/ee/hogai/tools/read_billing_tool/tool.py
@@ -23,6 +23,7 @@ USAGE_TYPES = [
{"label": "Group analytics", "value": "group_analytics"},
{"label": "Recordings", "value": "recording_count_in_period"},
{"label": "Mobile recordings", "value": "mobile_recording_count_in_period"},
+ {"label": "Recording observations", "value": "recording_observations_count_in_period"},
{"label": "Feature flag requests", "value": "billable_feature_flag_requests_count_in_period"},
{"label": "Exceptions", "value": "exceptions_captured_in_period"},
{"label": "Survey responses", "value": "survey_responses_count_in_period"},
diff --git a/frontend/src/scenes/billing/constants.ts b/frontend/src/scenes/billing/constants.ts
index e5f2a4f86dd6..657c384a772c 100644
--- a/frontend/src/scenes/billing/constants.ts
+++ b/frontend/src/scenes/billing/constants.ts
@@ -5,6 +5,7 @@ export const USAGE_TYPES = [
{ label: 'Group analytics', value: 'group_analytics' },
{ label: 'Recordings', value: 'recording_count_in_period' },
{ label: 'Mobile recordings', value: 'mobile_recording_count_in_period' },
+ { label: 'Recording observations', value: 'recording_observations_count_in_period' },
{ label: 'Feature flag requests', value: 'billable_feature_flag_requests_count_in_period' },
{ label: 'Exceptions', value: 'exceptions_captured_in_period' },
{ label: 'Survey responses', value: 'survey_responses_count_in_period' },
diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
index 8505ff0d91ce..44e8faee0d8e 100644
--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr
+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
@@ -1018,15 +1018,13 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.59
'''
- SELECT distinct_id as team,
- sum(JSONExtractInt(properties, 'count')) as sum
+ SELECT team_id,
+ count(distinct uuid) as count
FROM events
- WHERE team_id = 99999
- AND event='decide usage'
+ WHERE event = '$recording_observed'
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
- AND has(['correct'], replaceRegexpAll(JSONExtractRaw(properties, 'token'), '^"|"$', ''))
- GROUP BY team
+ GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.6
@@ -1071,7 +1069,7 @@
sum(JSONExtractInt(properties, 'count')) as sum
FROM events
WHERE team_id = 99999
- AND event='local evaluation usage'
+ AND event='decide usage'
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
AND has(['correct'], replaceRegexpAll(JSONExtractRaw(properties, 'token'), '^"|"$', ''))
@@ -1080,21 +1078,16 @@
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.61
'''
- WITH JSONExtractInt(log_comment, 'team_id') as team_id,
- JSONExtractString(log_comment, 'query_type') as query_type,
- JSONExtractString(log_comment, 'access_method') as access_method
- SELECT team_id,
- sum(read_bytes) as count
- FROM clusterAllReplicas(posthog, system.query_log)
- WHERE (type = 'QueryFinish'
- OR type = 'ExceptionWhileProcessing')
- AND is_initial_query = 1
- AND event_time >= 'today 00:00:00'
- AND event_time < '2022-01-11 05:59:59'
- AND query_start_time >= '2022-01-10 00:00:00'
- AND query_start_time < '2022-01-10 23:59:59'
- AND access_method = ''
- GROUP BY team_id
+
+ SELECT distinct_id as team,
+ sum(JSONExtractInt(properties, 'count')) as sum
+ FROM events
+ WHERE team_id = 99999
+ AND event='local evaluation usage'
+ AND timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ AND has(['correct'], replaceRegexpAll(JSONExtractRaw(properties, 'token'), '^"|"$', ''))
+ GROUP BY team
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.62
@@ -1103,7 +1096,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_rows) as count
+ sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1122,7 +1115,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(query_duration_ms) as count
+ sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1141,7 +1134,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_bytes) as count
+ sum(query_duration_ms) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1150,7 +1143,7 @@
AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
- AND access_method = 'personal_api_key'
+ AND access_method = ''
GROUP BY team_id
'''
# ---
@@ -1160,7 +1153,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_rows) as count
+ sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1179,7 +1172,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(query_duration_ms) as count
+ sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1198,17 +1191,16 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_bytes) as count
+ sum(query_duration_ms) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
- AND query_type IN (['EventsQuery'])
AND event_time >= 'today 00:00:00'
AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
- AND access_method = ''
+ AND access_method = 'personal_api_key'
GROUP BY team_id
'''
# ---
@@ -1218,7 +1210,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_rows) as count
+ sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1238,7 +1230,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(query_duration_ms) as count
+ sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1288,6 +1280,26 @@
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.70
+ '''
+ WITH JSONExtractInt(log_comment, 'team_id') as team_id,
+ JSONExtractString(log_comment, 'query_type') as query_type,
+ JSONExtractString(log_comment, 'access_method') as access_method
+ SELECT team_id,
+ sum(query_duration_ms) as count
+ FROM clusterAllReplicas(posthog, system.query_log)
+ WHERE (type = 'QueryFinish'
+ OR type = 'ExceptionWhileProcessing')
+ AND is_initial_query = 1
+ AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
+ AND query_start_time >= '2022-01-10 00:00:00'
+ AND query_start_time < '2022-01-10 23:59:59'
+ AND access_method = ''
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.71
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1307,7 +1319,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.71
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.72
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1327,7 +1339,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.72
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.73
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1347,7 +1359,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.73
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
'''
SELECT team_id,
@@ -1371,7 +1383,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
'''
SELECT team_id,
@@ -1386,7 +1398,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
'''
SELECT team_id,
@@ -1400,7 +1412,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
'''
SELECT team_id,
@@ -1413,7 +1425,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
'''
SELECT team_id,
@@ -1425,20 +1437,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
- '''
-
- SELECT team_id,
- SUM(count) as count
- FROM app_metrics2
- WHERE app_source='hog_flow'
- AND metric_name IN ('billable_invocation')
- AND metric_kind IN ('email')
- AND timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 23:59:59'
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.79
'''
@@ -1447,7 +1445,7 @@
FROM app_metrics2
WHERE app_source='hog_flow'
AND metric_name IN ('billable_invocation')
- AND metric_kind IN ('push')
+ AND metric_kind IN ('email')
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
GROUP BY team_id
@@ -1496,7 +1494,7 @@
FROM app_metrics2
WHERE app_source='hog_flow'
AND metric_name IN ('billable_invocation')
- AND metric_kind IN ('sms')
+ AND metric_kind IN ('push')
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
GROUP BY team_id
@@ -1510,7 +1508,7 @@
FROM app_metrics2
WHERE app_source='hog_flow'
AND metric_name IN ('billable_invocation')
- AND metric_kind IN ('fetch')
+ AND metric_kind IN ('sms')
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
GROUP BY team_id
@@ -1519,6 +1517,20 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.82
'''
+ SELECT team_id,
+ SUM(count) as count
+ FROM app_metrics2
+ WHERE app_source='hog_flow'
+ AND metric_name IN ('billable_invocation')
+ AND metric_kind IN ('fetch')
+ AND timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.83
+ '''
+
SELECT team_id,
SUM(count) as count
FROM app_metrics2
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index 90d237b17eac..a997c990cf4a 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -661,6 +661,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"mobile_recording_bytes_in_period": 6,
"mobile_recording_count_in_period": 1,
"mobile_billable_recording_count_in_period": 0,
+ "recording_observations_count_in_period": 0,
"group_types_total": 2,
"dashboard_count": 2,
"dashboard_template_count": 0,
@@ -736,6 +737,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"mobile_recording_bytes_in_period": 0,
"mobile_recording_count_in_period": 0,
"mobile_billable_recording_count_in_period": 0,
+ "recording_observations_count_in_period": 0,
"group_types_total": 2,
"dashboard_count": 2,
"dashboard_template_count": 0,
@@ -805,6 +807,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"mobile_recording_bytes_in_period": 6,
"mobile_recording_count_in_period": 1,
"mobile_billable_recording_count_in_period": 0,
+ "recording_observations_count_in_period": 0,
"group_types_total": 0,
"dashboard_count": 0,
"dashboard_template_count": 0,
@@ -897,6 +900,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"mobile_recording_bytes_in_period": 0,
"mobile_recording_count_in_period": 0,
"mobile_billable_recording_count_in_period": 0,
+ "recording_observations_count_in_period": 0,
"group_types_total": 0,
"dashboard_count": 0,
"dashboard_template_count": 0,
@@ -972,6 +976,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"mobile_recording_bytes_in_period": 0,
"mobile_recording_count_in_period": 0,
"mobile_billable_recording_count_in_period": 0,
+ "recording_observations_count_in_period": 0,
"group_types_total": 0,
"dashboard_count": 0,
"dashboard_template_count": 0,
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index 67d5422f748a..23fe5f6a2d12 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -134,6 +134,9 @@ class UsageReportCounters:
mobile_recording_bytes_in_period: int
mobile_billable_recording_count_in_period: int
+ # Replay Vision
+ recording_observations_count_in_period: int
+
# Persons and Groups
group_types_total: int
@@ -908,6 +911,27 @@ def get_teams_with_recording_count_in_period(
)
+@timed_log()
+@retry(tries=QUERY_RETRIES, delay=QUERY_RETRY_DELAY, backoff=QUERY_RETRY_BACKOFF)
+def get_teams_with_recording_observations_count_in_period(begin: datetime, end: datetime) -> list[tuple[int, int]]:
+ # Replay Vision emits one `$recording_observed` event per observation into the team's events table,
+ # with `event_uuid` set to the observation id. Count distinct uuids so at-least-once ingestion
+ # duplicates (same observation, same uuid) aren't over-counted — this is a billing input.
+ with tags_context(product=Product.REPLAY_VISION, feature=Feature.USAGE_REPORT):
+ return sync_execute(
+ """
+ SELECT team_id, count(distinct uuid) as count
+ FROM events
+ WHERE event = '$recording_observed' AND timestamp >= %(begin)s AND timestamp < %(end)s
+ GROUP BY team_id
+ """,
+ {"begin": begin, "end": end},
+ workload=Workload.OFFLINE,
+ settings=CH_BILLING_SETTINGS,
+ ch_user=ClickHouseUser.BILLING,
+ )
+
+
@timed_log()
@retry(tries=QUERY_RETRIES, delay=QUERY_RETRY_DELAY, backoff=QUERY_RETRY_BACKOFF)
def get_teams_with_zero_duration_recording_count_in_period(begin: datetime, end: datetime) -> list[tuple[int, int]]:
@@ -2212,6 +2236,7 @@ def has_non_zero_usage(report: UsageReportCounters) -> bool:
or report.workflow_push_sent_in_period > 0
or report.workflow_sms_sent_in_period > 0
or report.workflow_billable_invocations_in_period > 0
+ or report.recording_observations_count_in_period > 0
)
@@ -2300,6 +2325,9 @@ def _get_all_usage_data(period_start: datetime, period_end: datetime) -> dict[st
"teams_with_mobile_billable_recording_count_in_period": get_teams_with_mobile_billable_recording_count_in_period(
period_start, period_end
),
+ "teams_with_recording_observations_count_in_period": get_teams_with_recording_observations_count_in_period(
+ period_start, period_end
+ ),
"teams_with_decide_requests_count_in_period": get_teams_with_feature_flag_requests_count_in_period(
period_start, period_end, FlagRequestType.DECIDE
),
@@ -2552,6 +2580,9 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounter
mobile_billable_recording_count_in_period=all_data["teams_with_mobile_billable_recording_count_in_period"].get(
team.id, 0
),
+ recording_observations_count_in_period=all_data["teams_with_recording_observations_count_in_period"].get(
+ team.id, 0
+ ),
group_types_total=all_data["teams_with_group_types_total"].get(team.id, 0),
decide_requests_count_in_period=decide_requests_count_in_period,
local_evaluation_requests_count_in_period=local_evaluation_requests_count_in_period,
diff --git a/posthog/temporal/usage_report/queries.py b/posthog/temporal/usage_report/queries.py
index 51a5e2790fad..d81adf7788df 100644
--- a/posthog/temporal/usage_report/queries.py
+++ b/posthog/temporal/usage_report/queries.py
@@ -76,6 +76,7 @@ from posthog.tasks.usage_report import (
get_teams_with_query_metric,
get_teams_with_recording_bytes_in_period,
get_teams_with_recording_count_in_period,
+ get_teams_with_recording_observations_count_in_period,
get_teams_with_rows_exported_in_period,
get_teams_with_rows_synced_in_period,
get_teams_with_sdk_logs_records_in_period,
@@ -289,6 +290,10 @@ QUERIES: list[QuerySpec] = [
name="teams_with_mobile_billable_recording_count_in_period",
fn=get_teams_with_mobile_billable_recording_count_in_period,
),
+ QuerySpec(
+ name="teams_with_recording_observations_count_in_period",
+ fn=get_teams_with_recording_observations_count_in_period,
+ ),
# ---- ClickHouse: feature flag requests -----------------------------------
QuerySpec(
name="teams_with_decide_requests_count_in_period",
diff --git a/products/replay_vision/backend/temporal/activities/emit_observation_event.py b/products/replay_vision/backend/temporal/activities/emit_observation_event.py
index 0d7903045894..77896dc4dd8b 100644
--- a/products/replay_vision/backend/temporal/activities/emit_observation_event.py
+++ b/products/replay_vision/backend/temporal/activities/emit_observation_event.py
@@ -46,6 +46,9 @@ def _emit_event(inputs: EmitObservationEventInputs) -> None:
properties: dict = {
# Deterministic id so a worker crash mid-flush doesn't produce a duplicate event row.
"$insert_id": str(observation.id),
+ # Owning team/org so observations can be attributed and billed per tenant.
+ "team_id": observation.team_id,
+ "organization_id": str(team.organization_id),
"scanner_id": str(observation.scanner_id),
"scanner_name": snapshot.name,
"scanner_type": snapshot.scanner_type.value,