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.

fix(capture-v1): short-circuit all-invalid batches before quota limiter (#62826)

Eli Reismand52d61092e9b
diff --git a/rust/capture/src/v1/analytics/process.rs b/rust/capture/src/v1/analytics/process.rs
index 1bd85f1bf063..7eec29c15c5a 100644
--- a/rust/capture/src/v1/analytics/process.rs
+++ b/rust/capture/src/v1/analytics/process.rs
@@ -53,6 +53,11 @@ pub async fn process_batch(
let mut events = validate_events(context, batch)?;
+ // Nothing left to process — return 200 with per-event drops.
+ if events.iter().all(|ev| ev.result != EventResult::Ok) {
+ return Ok(BatchResponse::build(context, &events));
+ }
+
crate::v1::quota_limiter_shim::apply_quota_limits(
&state.quota_limiter,
&context.api_token,
@@ -2451,6 +2456,39 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn process_batch_all_validation_dropped_returns_200_not_402() {
+ let test_state = crate::v1::test_utils::TestStateBuilder::new().build();
+ let state = test_state.state;
+
+ let mut ctx = test_utils::test_context();
+ // Every event is invalid — empty name, empty distinct_id, bad timestamp.
+ let batch = valid_batch(vec![
+ Event {
+ event: String::new(),
+ ..valid_event()
+ },
+ Event {
+ distinct_id: String::new(),
+ ..valid_event()
+ },
+ Event {
+ timestamp: "not-a-date".to_string(),
+ ..valid_event()
+ },
+ ]);
+
+ let resp = process_batch(&state, &mut ctx, batch).await.unwrap();
+ assert_eq!(resp.entries().len(), 3);
+ for (_, entry) in resp.entries() {
+ assert_eq!(
+ entry.result,
+ EventResult::Drop,
+ "all-invalid batch must return 200 with per-event drops, not 402"
+ );
+ }
+ }
+
// =========================================================================
// Integration-style tests: publish → merge → response flow
// =========================================================================
diff --git a/rust/capture/src/v1/quota_limiter_shim.rs b/rust/capture/src/v1/quota_limiter_shim.rs
index 634546af41ec..0c347552f4f0 100644
--- a/rust/capture/src/v1/quota_limiter_shim.rs
+++ b/rust/capture/src/v1/quota_limiter_shim.rs
@@ -19,9 +19,10 @@ const SCOPED_CHECKS: &[ScopedCheck] = &[
/// Apply billing quota limits to a batch of events in-place.
///
-/// Checks the global limiter first (short-circuit on full batch drop), then
-/// iterates scoped limiters, marking matching events as `Limited` / `Drop`.
-/// Returns `Error::BillingLimitExceeded` when the entire batch is limited.
+/// Checks global limiter first (short-circuit), then scoped limiters.
+/// Returns `Error::BillingLimitExceeded` when every event is limited.
+/// Expects at least one `Ok` event — all-dropped batches should
+/// short-circuit in `process_batch` before reaching here.
pub async fn apply_quota_limits(
limiter: &CaptureQuotaLimiter,
token: &str,