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(tracing): add distributed tracing usage report counters (#73471)

Jon McCallum0c6259c16ce6
diff --git a/posthog/clickhouse/query_tagging.py b/posthog/clickhouse/query_tagging.py
index 60bbdacc7f8e..b67726f5ff4e 100644
--- a/posthog/clickhouse/query_tagging.py
+++ b/posthog/clickhouse/query_tagging.py
@@ -84,6 +84,7 @@ class Product(StrEnum):
SIGNALS = "signals"
SQL_EDITOR = "sql_editor"
SURVEYS = "surveys"
+ TRACING = "tracing"
USER_INTERVIEWS = "user_interviews"
WAREHOUSE = "warehouse"
WEB_ANALYTICS = "web_analytics"
diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
index bfdbe2572e6c..e8dd6aaaca5f 100644
--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr
+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
@@ -439,26 +439,30 @@
'''
SELECT team_id,
- multiIf(replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'web', 'web', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'js', 'web_lite', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python', 'unknown') AS library,
- count(1) as total
- FROM events
- WHERE event = '$exception'
+ metric_name,
+ SUM(count) as count
+ FROM app_metrics2
+ WHERE app_source='traces'
+ AND metric_name IN ('bytes_ingested',
+ 'records_ingested')
AND timestamp >= '2022-01-10 00:00:00'
AND timestamp < '2022-01-10 23:59:59'
GROUP BY team_id,
- library
+ metric_name
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.29
'''
SELECT team_id,
- count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
+ multiIf(replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'web', 'web', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'js', 'web_lite', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python', 'unknown') AS library,
+ count(1) as total
FROM events
- WHERE timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 02:00:00'
- AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
- GROUP BY team_id
+ WHERE event = '$exception'
+ AND timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ GROUP BY team_id,
+ library
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.3
@@ -502,8 +506,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 02:00:00'
- AND timestamp < '2022-01-10 04:00:00'
+ WHERE timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 02:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -514,8 +518,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 04:00:00'
- AND timestamp < '2022-01-10 06:00:00'
+ WHERE timestamp >= '2022-01-10 02:00:00'
+ AND timestamp < '2022-01-10 04:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -526,8 +530,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 06:00:00'
- AND timestamp < '2022-01-10 08:00:00'
+ WHERE timestamp >= '2022-01-10 04:00:00'
+ AND timestamp < '2022-01-10 06:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -538,8 +542,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 08:00:00'
- AND timestamp < '2022-01-10 10:00:00'
+ WHERE timestamp >= '2022-01-10 06:00:00'
+ AND timestamp < '2022-01-10 08:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -550,8 +554,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 10:00:00'
- AND timestamp < '2022-01-10 12:00:00'
+ WHERE timestamp >= '2022-01-10 08:00:00'
+ AND timestamp < '2022-01-10 10:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -562,8 +566,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 12:00:00'
- AND timestamp < '2022-01-10 14:00:00'
+ WHERE timestamp >= '2022-01-10 10:00:00'
+ AND timestamp < '2022-01-10 12:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -574,8 +578,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 14:00:00'
- AND timestamp < '2022-01-10 16:00:00'
+ WHERE timestamp >= '2022-01-10 12:00:00'
+ AND timestamp < '2022-01-10 14:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -586,8 +590,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 16:00:00'
- AND timestamp < '2022-01-10 18:00:00'
+ WHERE timestamp >= '2022-01-10 14:00:00'
+ AND timestamp < '2022-01-10 16:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -598,8 +602,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 18:00:00'
- AND timestamp < '2022-01-10 20:00:00'
+ WHERE timestamp >= '2022-01-10 16:00:00'
+ AND timestamp < '2022-01-10 18:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -610,8 +614,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 20:00:00'
- AND timestamp < '2022-01-10 22:00:00'
+ WHERE timestamp >= '2022-01-10 18:00:00'
+ AND timestamp < '2022-01-10 20:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -657,8 +661,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 22:00:00'
- AND timestamp < '2022-01-10 23:59:59'
+ WHERE timestamp >= '2022-01-10 20:00:00'
+ AND timestamp < '2022-01-10 22:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -669,11 +673,9 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 02:00:00'
+ WHERE timestamp >= '2022-01-10 22:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
- AND person_mode IN ('full',
- 'force_upgrade')
GROUP BY team_id
'''
# ---
@@ -683,8 +685,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 02:00:00'
- AND timestamp < '2022-01-10 04:00:00'
+ WHERE timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 02:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -697,8 +699,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 04:00:00'
- AND timestamp < '2022-01-10 06:00:00'
+ WHERE timestamp >= '2022-01-10 02:00:00'
+ AND timestamp < '2022-01-10 04:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -711,8 +713,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 06:00:00'
- AND timestamp < '2022-01-10 08:00:00'
+ WHERE timestamp >= '2022-01-10 04:00:00'
+ AND timestamp < '2022-01-10 06:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -725,8 +727,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 08:00:00'
- AND timestamp < '2022-01-10 10:00:00'
+ WHERE timestamp >= '2022-01-10 06:00:00'
+ AND timestamp < '2022-01-10 08:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -739,8 +741,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 10:00:00'
- AND timestamp < '2022-01-10 12:00:00'
+ WHERE timestamp >= '2022-01-10 08:00:00'
+ AND timestamp < '2022-01-10 10:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -753,8 +755,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 12:00:00'
- AND timestamp < '2022-01-10 14:00:00'
+ WHERE timestamp >= '2022-01-10 10:00:00'
+ AND timestamp < '2022-01-10 12:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -767,8 +769,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 14:00:00'
- AND timestamp < '2022-01-10 16:00:00'
+ WHERE timestamp >= '2022-01-10 12:00:00'
+ AND timestamp < '2022-01-10 14:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -781,8 +783,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 16:00:00'
- AND timestamp < '2022-01-10 18:00:00'
+ WHERE timestamp >= '2022-01-10 14:00:00'
+ AND timestamp < '2022-01-10 16:00:00'
AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -827,6 +829,20 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.50
'''
+ SELECT team_id,
+ count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
+ FROM events
+ WHERE timestamp >= '2022-01-10 16:00:00'
+ AND timestamp < '2022-01-10 18:00:00'
+ AND event NOT IN ['$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
+ AND person_mode IN ('full',
+ 'force_upgrade')
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.51
+ '''
+
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
@@ -838,7 +854,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.51
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.52
'''
SELECT team_id,
@@ -852,7 +868,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.52
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.53
'''
SELECT team_id,
@@ -866,7 +882,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.53
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.54
'''
SELECT team_id,
@@ -882,7 +898,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.54
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.55
'''
SELECT team_id,
@@ -893,7 +909,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.55
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.56
'''
SELECT team_id,
@@ -916,7 +932,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.56
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.57
'''
SELECT team_id,
@@ -939,7 +955,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.57
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.58
'''
SELECT team_id,
@@ -963,7 +979,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.58
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.59
'''
SELECT team_id,
@@ -986,30 +1002,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.59
- '''
-
- SELECT team_id,
- sum(total_size) as bytes
- FROM
- (SELECT any(team_id) as team_id,
- session_id,
- sum(size) as total_size
- FROM session_replay_events
- WHERE min_first_timestamp >= '2022-01-10 00:00:00'
- AND min_first_timestamp < '2022-01-10 23:59:59'
- GROUP BY session_id
- HAVING ifNull(argMinMerge(snapshot_source), 'web') == 'mobile'
- AND max(is_deleted) = 0)
- WHERE session_id NOT IN
- (SELECT DISTINCT session_id
- FROM session_replay_events
- WHERE min_first_timestamp >= '2022-01-09 00:00:00'
- AND min_first_timestamp < '2022-01-10 00:00:00'
- GROUP BY session_id)
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.6
'''
@@ -1048,6 +1040,30 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.60
'''
+ SELECT team_id,
+ sum(total_size) as bytes
+ FROM
+ (SELECT any(team_id) as team_id,
+ session_id,
+ sum(size) as total_size
+ FROM session_replay_events
+ WHERE min_first_timestamp >= '2022-01-10 00:00:00'
+ AND min_first_timestamp < '2022-01-10 23:59:59'
+ GROUP BY session_id
+ HAVING ifNull(argMinMerge(snapshot_source), 'web') == 'mobile'
+ AND max(is_deleted) = 0)
+ WHERE session_id NOT IN
+ (SELECT DISTINCT session_id
+ FROM session_replay_events
+ WHERE min_first_timestamp >= '2022-01-09 00:00:00'
+ AND min_first_timestamp < '2022-01-10 00:00:00'
+ GROUP BY session_id)
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.61
+ '''
+
SELECT team_id,
count(distinct session_id) as count
FROM
@@ -1072,7 +1088,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.61
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.62
'''
SELECT distinct_id as team,
@@ -1086,7 +1102,7 @@
GROUP BY team
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.62
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.63
'''
SELECT distinct_id as team,
@@ -1100,7 +1116,7 @@
GROUP BY team
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.63
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.64
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1119,7 +1135,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.64
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.65
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1138,7 +1154,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.65
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.66
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1157,7 +1173,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.66
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.67
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1176,7 +1192,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.67
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.68
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1195,7 +1211,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.68
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.69
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1214,26 +1230,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.69
- '''
- 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 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.7
'''
@@ -1275,7 +1271,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')
@@ -1295,7 +1291,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')
@@ -1310,6 +1306,26 @@
'''
# ---
# 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,
+ 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.73
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1329,7 +1345,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.73
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1349,7 +1365,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1369,7 +1385,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
'''
SELECT team_id,
@@ -1393,7 +1409,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
'''
SELECT team_id,
@@ -1408,7 +1424,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
'''
SELECT team_id,
@@ -1422,7 +1438,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.79
'''
SELECT team_id,
@@ -1435,24 +1451,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.79
- '''
-
- SELECT team_id,
- COUNT() - uniqExactIf(request_id, verified
- AND request_id != '') as count
- FROM
- (SELECT team_id,
- replaceRegexpAll(JSONExtractRaw(properties, '$ai_gateway_verified'), '^"|"$', '') IN ('true',
- '1') as verified,
- if(verified, replaceRegexpAll(JSONExtractRaw(properties, '$ai_gateway_request_id'), '^"|"$', ''), '') as request_id
- FROM events
- WHERE event IN ['$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters']
- 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.8
'''
@@ -1491,6 +1489,24 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.80
'''
+ SELECT team_id,
+ COUNT() - uniqExactIf(request_id, verified
+ AND request_id != '') as count
+ FROM
+ (SELECT team_id,
+ replaceRegexpAll(JSONExtractRaw(properties, '$ai_gateway_verified'), '^"|"$', '') IN ('true',
+ '1') as verified,
+ if(verified, replaceRegexpAll(JSONExtractRaw(properties, '$ai_gateway_request_id'), '^"|"$', ''), '') as request_id
+ FROM events
+ WHERE event IN ['$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters']
+ 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.81
+ '''
+
SELECT team_id,
SUM(count) as count
FROM app_metrics2
@@ -1502,7 +1518,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.81
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.82
'''
SELECT team_id,
@@ -1516,7 +1532,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.82
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.83
'''
SELECT team_id,
@@ -1530,7 +1546,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.83
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.84
'''
SELECT team_id,
@@ -1544,7 +1560,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.84
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.85
'''
SELECT team_id,
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index 10506645e7ad..8d94ddf86fa4 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -3257,6 +3257,70 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh
field = f"{sdk}_logs_records_in_period"
assert counters[field] == expected, f"{scope}: {field} should be {expected}, got {counters[field]}"
+ @parameterized.expand(
+ [
+ # MB is floored to whole decimal MB like logs_mb_in_period.
+ (
+ "with_usage",
+ {"bytes_ingested": 2_500_000, "records_ingested": 40},
+ {
+ "apm_tracing_bytes_in_period": 2_500_000,
+ "apm_tracing_spans_in_period": 40,
+ "apm_tracing_mb_in_period": 2,
+ },
+ ),
+ (
+ "sub_mb_floors_to_zero",
+ {"bytes_ingested": 999_999, "records_ingested": 5},
+ {
+ "apm_tracing_bytes_in_period": 999_999,
+ "apm_tracing_spans_in_period": 5,
+ "apm_tracing_mb_in_period": 0,
+ },
+ ),
+ ]
+ )
+ @patch("posthog.tasks.usage_report.get_ph_client")
+ @patch("posthog.tasks.usage_report.send_report_to_billing_service")
+ def test_apm_tracing_usage_metrics(
+ self,
+ _name: str,
+ metrics: dict[str, int],
+ expected: dict[str, int],
+ billing_task_mock: MagicMock,
+ posthog_capture_mock: MagicMock,
+ ) -> None:
+ self._setup_teams()
+
+ for metric_name, count in metrics.items():
+ create_app_metric2(
+ team_id=self.org_1_team_1.id,
+ app_source="traces",
+ metric_name=metric_name,
+ count=count,
+ )
+ # Same metric names under the logs app_source must not leak into the tracing counters.
+ create_app_metric2(
+ team_id=self.org_1_team_1.id,
+ app_source="logs",
+ metric_name="bytes_ingested",
+ count=77_000_000,
+ )
+
+ period = get_previous_day(at=now() + relativedelta(days=1))
+ period_start, period_end = period
+ all_reports = _get_all_org_reports(period_start, period_end)
+
+ org_1_report = _get_full_org_usage_report_as_dict(
+ _get_full_org_usage_report(all_reports[str(self.org_1.id)], get_instance_metadata(period))
+ )
+
+ # Only org_1_team_1 has traces usage, so the org-level rollup equals that single team's values.
+ team_1_report = org_1_report["teams"][str(self.org_1_team_1.id)]
+ for field, value in expected.items():
+ assert org_1_report[field] == value, field
+ assert team_1_report[field] == value, field
+
@freeze_time("2022-01-10T10:00:00Z")
class TestErrorTrackingUsageReport(ClickhouseDestroyTablesMixin, TestCase, ClickhouseTestMixin):
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index 186786331744..a76128dbd3c6 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -307,6 +307,11 @@ class UsageReportCounters:
flutter_logs_records_in_period: int
ruby_logs_records_in_period: int
+ # Distributed Tracing (APM)
+ apm_tracing_bytes_in_period: int
+ apm_tracing_spans_in_period: int
+ apm_tracing_mb_in_period: int
+
# Instance metadata to be included in overall report
@dataclasses.dataclass
@@ -2313,6 +2318,44 @@ def get_teams_with_sdk_logs_records_in_period(
return by_sdk
+@timed_log()
+@retry(tries=QUERY_RETRIES, delay=QUERY_RETRY_DELAY, backoff=QUERY_RETRY_BACKOFF)
+def get_teams_with_apm_tracing_usage_in_period(
+ begin: datetime,
+ end: datetime,
+) -> dict[str, list[tuple[int, int]]]:
+ """
+ Returns Distributed Tracing (APM) ingested bytes and span counts per team for the period,
+ keyed by `bytes` / `spans`; each value is a list of `(team_id, count)` tuples ready for
+ `convert_team_usage_rows_to_dict`.
+
+ The traces ingestion consumer subclasses the logs consumer, so it emits the same
+ pre-aggregated `app_metrics2` counters under `app_source='traces'`. A span is one record,
+ so `records_ingested` is the span count.
+ """
+ with tags_context(product=Product.TRACING, feature=Feature.USAGE_REPORT):
+ rows = sync_execute(
+ """
+ SELECT team_id, metric_name, SUM(count) as count
+ FROM app_metrics2
+ WHERE app_source='traces'
+ AND metric_name IN ('bytes_ingested', 'records_ingested')
+ AND timestamp >= %(begin)s AND timestamp < %(end)s
+ GROUP BY team_id, metric_name
+ """,
+ {"begin": begin, "end": end},
+ workload=Workload.OFFLINE,
+ settings=CH_BILLING_SETTINGS,
+ ch_user=ClickHouseUser.BILLING,
+ )
+
+ key_by_metric = {"bytes_ingested": "bytes", "records_ingested": "spans"}
+ usage: dict[str, list[tuple[int, int]]] = {"bytes": [], "spans": []}
+ for team_id, metric_name, count in rows:
+ usage[key_by_metric[metric_name]].append((team_id, count))
+ return usage
+
+
def _trim_oversize_usage_report_payload(full_report_dict: dict[str, Any]) -> dict[str, Any]:
"""Drop the per-team breakdown when the serialized report would exceed Kafka's
message size limit, so the org-level roll-up still makes it through ingestion.
@@ -2431,6 +2474,7 @@ def has_non_zero_usage(report: UsageReportCounters) -> bool:
or report.posthog_code_credits_used_in_period > 0
or report.task_sandbox_seconds_in_period > 0
or report.logs_bytes_in_period > 0
+ or report.apm_tracing_bytes_in_period > 0
or report.workflow_emails_sent_in_period > 0
or report.workflow_push_sent_in_period > 0
or report.workflow_sms_sent_in_period > 0
@@ -2467,6 +2511,7 @@ def _get_all_usage_data(period_start: datetime, period_end: datetime) -> dict[st
period_start, period_end, team_ids_with_logs=team_ids_with_logs
)
logs_retention_by_tier = get_teams_with_logs_retention_bytes_in_period(period_start, period_end)
+ apm_tracing_usage = get_teams_with_apm_tracing_usage_in_period(period_start, period_end)
exception_metrics_by_library, exception_metrics = get_teams_with_exceptions_captured_in_period(
period_start, period_end
)
@@ -2727,6 +2772,8 @@ def _get_all_usage_data(period_start: datetime, period_end: datetime) -> dict[st
"teams_with_android_logs_records_in_period": sdk_logs_by_suffix["android"],
"teams_with_flutter_logs_records_in_period": sdk_logs_by_suffix["flutter"],
"teams_with_ruby_logs_records_in_period": sdk_logs_by_suffix["ruby"],
+ "teams_with_apm_tracing_bytes_in_period": apm_tracing_usage["bytes"],
+ "teams_with_apm_tracing_spans_in_period": apm_tracing_usage["spans"],
}
@@ -2922,6 +2969,9 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounter
android_logs_records_in_period=all_data["teams_with_android_logs_records_in_period"].get(team.id, 0),
flutter_logs_records_in_period=all_data["teams_with_flutter_logs_records_in_period"].get(team.id, 0),
ruby_logs_records_in_period=all_data["teams_with_ruby_logs_records_in_period"].get(team.id, 0),
+ apm_tracing_bytes_in_period=all_data["teams_with_apm_tracing_bytes_in_period"].get(team.id, 0),
+ apm_tracing_spans_in_period=all_data["teams_with_apm_tracing_spans_in_period"].get(team.id, 0),
+ apm_tracing_mb_in_period=int(all_data["teams_with_apm_tracing_bytes_in_period"].get(team.id, 0) // 1_000_000),
)
diff --git a/posthog/temporal/tests/usage_report/test_aggregate_activity.py b/posthog/temporal/tests/usage_report/test_aggregate_activity.py
index d22a24f7a343..5075b6d5e6f3 100644
--- a/posthog/temporal/tests/usage_report/test_aggregate_activity.py
+++ b/posthog/temporal/tests/usage_report/test_aggregate_activity.py
@@ -112,6 +112,13 @@ def _canned_query_payload(query_name: str, team_a_id: int, team_b_id: int, *extr
"30d": [(team_b_id, 2_000)],
"90d": [],
}
+ if query_name == "apm_tracing_usage":
+ # Distinct bytes/spans values so a bytes<->spans mapping swap fails the
+ # assertions instead of passing silently. 2_500_000 bytes floors to 2 MB.
+ return {
+ "bytes": [(team_a_id, 2_500_000)],
+ "spans": [(team_a_id, 42)],
+ }
if query_name == "teams_with_event_count_in_period":
return [(team_a_id, 100), (team_b_id, 50), *extra_total_rows]
@@ -250,6 +257,15 @@ async def test_aggregate_writes_chunks_and_manifest(minio_workflow_ctx: Workflow
assert by_org[str(org_a.id)]["ios_logs_records_in_period"] == 4
assert by_org[str(org_a.id)]["ruby_logs_records_in_period"] == 7
+ # apm_tracing_usage multi-key fan-out: bytes and spans land in their own
+ # destination counters (a bytes<->spans swap would fail here), and MB is
+ # the floored bytes total.
+ assert by_org[str(org_a.id)]["apm_tracing_bytes_in_period"] == 2_500_000
+ assert by_org[str(org_a.id)]["apm_tracing_spans_in_period"] == 42
+ assert by_org[str(org_a.id)]["apm_tracing_mb_in_period"] == 2
+ assert by_org[str(org_b.id)]["apm_tracing_bytes_in_period"] == 0
+ assert by_org[str(org_b.id)]["apm_tracing_spans_in_period"] == 0
+
# has_non_zero_usage is computed and present on every line.
assert by_org[str(org_a.id)]["has_non_zero_usage"] is True
assert by_org[str(org_b.id)]["has_non_zero_usage"] is True
diff --git a/posthog/temporal/tests/usage_report/test_parity.py b/posthog/temporal/tests/usage_report/test_parity.py
index 2a66ebabc681..8ddd2e0eb51a 100644
--- a/posthog/temporal/tests/usage_report/test_parity.py
+++ b/posthog/temporal/tests/usage_report/test_parity.py
@@ -73,6 +73,11 @@ def _seed_all_data(team_a_id: int, team_b_id: int, team_c_id: int) -> dict[str,
"teams_with_node_exceptions_captured_in_period": {team_b_id: 1},
"teams_with_api_queries_count": {team_a_id: 10},
"teams_with_api_queries_read_bytes": {team_a_id: 1_500_000},
+ # apm_tracing is a multi-output spec (bytes + spans) and drives a
+ # derived per-team MB field. team_a2's sub-MB bytes must floor to 0
+ # per team before the org sum, so org_a's MB is 2 (not 3).
+ "teams_with_apm_tracing_bytes_in_period": {team_a_id: 2_500_000, team_b_id: 600_000, team_c_id: 999_999},
+ "teams_with_apm_tracing_spans_in_period": {team_a_id: 40, team_b_id: 10, team_c_id: 5},
}
return {key: interesting.get(key, {}) for key in _all_destination_keys()}
@@ -256,6 +261,17 @@ def test_end_to_end_parity_celery_task_vs_temporal_activity(
assert temporal_per_org[str(org_a.id)]["team_count"] == 2
assert temporal_per_org[str(org_b.id)]["team_count"] == 1
+ # apm_tracing is a multi-output spec: the byte→spans mapping must fan out
+ # correctly and the derived MB field must floor per team before the org
+ # sum. org_a is two teams (2_500_000 + 600_000 bytes → 2 MB, since the
+ # 600_000 team floors to 0), org_b is one (999_999 bytes → 0 MB).
+ assert temporal_per_org[str(org_a.id)]["apm_tracing_bytes_in_period"] == 2_500_000 + 600_000
+ assert temporal_per_org[str(org_a.id)]["apm_tracing_spans_in_period"] == 40 + 10
+ assert temporal_per_org[str(org_a.id)]["apm_tracing_mb_in_period"] == 2
+ assert temporal_per_org[str(org_b.id)]["apm_tracing_bytes_in_period"] == 999_999
+ assert temporal_per_org[str(org_b.id)]["apm_tracing_spans_in_period"] == 5
+ assert temporal_per_org[str(org_b.id)]["apm_tracing_mb_in_period"] == 0
+
@pytest.mark.django_db(transaction=True)
def test_temporal_build_org_reports_does_not_run_per_org_membership_queries() -> None:
diff --git a/posthog/temporal/usage_report/queries.py b/posthog/temporal/usage_report/queries.py
index f79291fd78c8..14004852fc40 100644
--- a/posthog/temporal/usage_report/queries.py
+++ b/posthog/temporal/usage_report/queries.py
@@ -56,6 +56,7 @@ from posthog.tasks.usage_report import (
get_teams_with_ai_credits_used_in_period,
get_teams_with_ai_event_count_in_period,
get_teams_with_api_queries_metrics,
+ get_teams_with_apm_tracing_usage_in_period,
get_teams_with_billable_enhanced_persons_event_count_in_period,
get_teams_with_billable_event_count_in_period,
get_teams_with_cdp_billable_invocations_in_period,
@@ -520,6 +521,16 @@ QUERIES: list[QuerySpec] = [
"90d": "teams_with_logs_retention_90d_bytes_in_period",
},
),
+ # ---- ClickHouse: APM tracing ---------------------------------------------
+ QuerySpec(
+ name="apm_tracing_usage",
+ fn=get_teams_with_apm_tracing_usage_in_period,
+ output="multi",
+ multi_keys_mapping={
+ "bytes": "teams_with_apm_tracing_bytes_in_period",
+ "spans": "teams_with_apm_tracing_spans_in_period",
+ },
+ ),
# ---- Snapshot queries (kind="snapshot") ---------------------------------
# ⚠️ Read the disclaimer at the top of this module. These ignore the
# period and reflect *current* state at run time. Re-running for the same