: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(billing): meter logs and traces as one combined MB counter (#84442)
Abheek Basued2cf3a85f88
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.pyindex d53030749535..56fc605a2b86 100644--- a/posthog/tasks/test/test_usage_report.py+++ b/posthog/tasks/test/test_usage_report.py@@ -3309,6 +3309,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh"logs_bytes_in_period": 1_500_000_000,"logs_records_in_period": 1000,"logs_mb_in_period": 1500,+ "logs_and_traces_mb_in_period": 1500,"logs_retention_14d_mb_in_period": 500,"logs_retention_30d_mb_in_period": 1000,"logs_retention_90d_mb_in_period": 0,@@ -3326,6 +3327,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh"logs_bytes_in_period": 2_500_000_000,"logs_records_in_period": 2000,"logs_mb_in_period": 2500,+ "logs_and_traces_mb_in_period": 2500,"logs_retention_14d_mb_in_period": 0,"logs_retention_30d_mb_in_period": 0,"logs_retention_90d_mb_in_period": 2500,@@ -3346,6 +3348,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh"logs_bytes_in_period": 1_200_000,"logs_records_in_period": 5,"logs_mb_in_period": 1,+ "logs_and_traces_mb_in_period": 1,"logs_retention_14d_mb_in_period": 0,"logs_retention_30d_mb_in_period": 0,"logs_retention_90d_mb_in_period": 0,@@ -3465,23 +3468,32 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh@parameterized.expand([- # MB is floored to whole decimal MB like logs_mb_in_period.+ # Per-signal MB is floored to whole decimal MB, and the billable combined metric is+ # floored once off the summed bytes: 77_000_000 + 2_500_000 -> 79 MB.("with_usage",{"bytes_ingested": 2_500_000, "records_ingested": 40},+ 77_000_000,{"apm_tracing_bytes_in_period": 2_500_000,"apm_tracing_spans_in_period": 40,"apm_tracing_mb_in_period": 2,+ "logs_mb_in_period": 77,+ "logs_and_traces_mb_in_period": 79,},),+ # Flooring once off the summed bytes, rather than adding the floored per-signal figures,+ # keeps the remainders both signals drop: 77_600_000 + 999_999 -> 78 MB, not 77 + 0.(- "sub_mb_floors_to_zero",+ "sub_mb_remainders_add_up",{"bytes_ingested": 999_999, "records_ingested": 5},+ 77_600_000,{"apm_tracing_bytes_in_period": 999_999,"apm_tracing_spans_in_period": 5,"apm_tracing_mb_in_period": 0,+ "logs_mb_in_period": 77,+ "logs_and_traces_mb_in_period": 78,},),]@@ -3492,6 +3504,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickhself,_name: str,metrics: dict[str, int],+ logs_bytes: int,expected: dict[str, int],billing_task_mock: MagicMock,posthog_capture_mock: MagicMock,@@ -3510,7 +3523,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickhteam_id=self.org_1_team_1.id,app_source="logs",metric_name="bytes_ingested",- count=77_000_000,+ count=logs_bytes,)period = get_previous_day(at=now() + relativedelta(days=1))@@ -3520,7 +3533,7 @@ class TestHogFunctionUsageReports(ClickhouseDestroyTablesMixin, TestCase, Clickh_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.+ # Only org_1_team_1 has logs or traces usage, so the org-level rollup equals that 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, fielddiff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.pyindex 353efdab5611..eed781c7491d 100644--- a/posthog/tasks/usage_report.py+++ b/posthog/tasks/usage_report.py@@ -166,7 +166,9 @@ USAGE_REPORT_PARENT_TASK_KWARGS = {}+# Mutable: `_add_team_report_to_org_reports` accumulates each team's counters into the org's+# `OrgReport` field by field with `setattr`.[email protected](frozen=False)class UsageReportCounters:event_count_in_period: intenhanced_persons_event_count_in_period: int@@ -339,7 +341,13 @@ class UsageReportCounters:workflow_sms_sent_in_period: intworkflow_billable_invocations_in_period: int- # Logs+ # Logs and traces share one billing product, one meter and one free tier, so the billable metric is+ # the combined MB. It is floored once off the summed bytes, so it can come out 1 MB above+ # logs_mb_in_period + apm_tracing_mb_in_period, which each drop their own sub-MB remainder.+ logs_and_traces_mb_in_period: int++ # Logs. The per-signal MB is report-only — it exists so we can see the logs/traces split without+ # billing the two apart.logs_bytes_in_period: intlogs_records_in_period: intlogs_mb_in_period: int@@ -359,7 +367,7 @@ class UsageReportCounters:flutter_logs_records_in_period: intruby_logs_records_in_period: int- # Distributed Tracing (APM)+ # Distributed Tracing (APM). apm_tracing_mb_in_period is report-only, like logs_mb_in_period.apm_tracing_bytes_in_period: intapm_tracing_spans_in_period: intapm_tracing_mb_in_period: int@@ -3148,6 +3156,8 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounterlocal_evaluation_requests_count_in_period = all_data["teams_with_local_evaluation_requests_count_in_period"].get(team.id, 0)+ logs_bytes_in_period = all_data["teams_with_logs_bytes_in_period"].get(team.id, 0)+ apm_tracing_bytes_in_period = all_data["teams_with_apm_tracing_bytes_in_period"].get(team.id, 0)return UsageReportCounters(event_count_in_period=all_data["teams_with_event_count_in_period"].get(team.id, 0),enhanced_persons_event_count_in_period=all_data["teams_with_enhanced_persons_event_count_in_period"].get(@@ -3332,9 +3342,10 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounterworkflow_billable_invocations_in_period=all_data["teams_with_workflow_billable_invocations_in_period"].get(team.id, 0),- logs_bytes_in_period=all_data["teams_with_logs_bytes_in_period"].get(team.id, 0),+ logs_and_traces_mb_in_period=int((logs_bytes_in_period + apm_tracing_bytes_in_period) // 1_000_000),+ logs_bytes_in_period=logs_bytes_in_period,logs_records_in_period=all_data["teams_with_logs_records_in_period"].get(team.id, 0),- logs_mb_in_period=int(all_data["teams_with_logs_bytes_in_period"].get(team.id, 0) // 1_000_000),+ logs_mb_in_period=int(logs_bytes_in_period // 1_000_000),logs_retention_14d_mb_in_period=int(all_data["teams_with_logs_retention_14d_bytes_in_period"].get(team.id, 0) // 1_000_000),@@ -3350,9 +3361,9 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounterandroid_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_bytes_in_period=apm_tracing_bytes_in_period,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),+ apm_tracing_mb_in_period=int(apm_tracing_bytes_in_period // 1_000_000),)