Skip to content

perf(billing): Filter usage query to billable outcomes - #127348

Merged
noahsmartin merged 1 commit into
masterfrom
nm/billing-usage-filter-billable-outcomes
Oct 14, 2026
Merged

noahsmartin merged 1 commit into
masterfrom
nm/billing-usage-filter-billable-outcomes

Conversation

@noahsmartin

@noahsmartin noahsmartin commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Add an outcome IN (ACCEPTED, FILTERED, RATE_LIMITED) condition to the WHERE clause of the billing platform's usage query (_build_query). Both get_usage and get_usage_by_project use this query.

Every aggregate in the query (total, accepted, dropped, filtered, over_quota, spike_protection, dynamic_sampling) already counts only those three outcomes. Because the WHERE clause didn't filter on outcome, ClickHouse still read and grouped INVALID, ABUSE, CLIENT_DISCARD and CARDINALITY_LIMITED rows, then discarded them in the sums

Every aggregate in the billing usage query only counts ACCEPTED,
FILTERED and RATE_LIMITED outcomes, but the WHERE clause didn't filter
on outcome. ClickHouse read and grouped rows for every outcome type,
including CLIENT_DISCARD, which has many distinct reasons. Filtering on
outcome lets ClickHouse skip those rows using the outcome column in the
sort key.

The usage sums don't change. Two things do: last_usage_ts now reflects
the latest billable outcome instead of the latest outcome of any type,
and days with only non-billable outcomes no longer show up as
all-zero entries.
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 14, 2026
@noahsmartin
noahsmartin marked this pull request as ready for review October 14, 2026 04:59
@noahsmartin
noahsmartin requested review from a team as code owners October 14, 2026 04:59
@noahsmartin
noahsmartin merged commit 65b87b7 into master Oct 14, 2026
70 checks passed
@noahsmartin
noahsmartin deleted the nm/billing-usage-filter-billable-outcomes branch October 14, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants