: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
fix(flags): poll billing counter across the bucket boundary (#86914)
posthog[bot]841bf2659055
diff --git a/rust/feature-flags/tests/common/mod.rs b/rust/feature-flags/tests/common/mod.rsindex d310ed062f5e..619fb545553f 100644--- a/rust/feature-flags/tests/common/mod.rs+++ b/rust/feature-flags/tests/common/mod.rs@@ -503,21 +503,29 @@ impl Drop for ServerHandle {}}-/// Poll `HGET key field` for up to ~1s, returning the value as soon as the-/// key exists. Panics on timeout. Use for the positive ("the write should-/// land") case in billing aggregator tests — for the negative case, sleep-/// one flush window and check.+/// Poll for a billing counter across every bucket field in `first_bucket..=last_bucket`,+/// returning the first value found. Panics on timeout. Use for the positive ("the write+/// should land") case in billing aggregator tests — for the negative case, sleep one flush+/// window and check. The server captures the bucket inside `record()` while it handles the+/// request, so a request that straddles a 2-minute bucket boundary lands the increment in+/// either the bucket seen before or after the roundtrip. Pass both ends of the range to poll+/// for the counter without racing the boundary.#[allow(dead_code)]-pub async fn poll_for_billing_counter(+pub async fn poll_for_billing_counter_across_buckets(client: &Arc<dyn common_redis::Client + Send + Sync>,key: &str,- field: &str,+ first_bucket: u64,+ last_bucket: u64,) -> String {for _ in 0..40 {- if let Ok(v) = client.hget(key.to_string(), field.to_string()).await {- return v;+ for bucket in first_bucket..=last_bucket {+ if let Ok(v) = client.hget(key.to_string(), bucket.to_string()).await {+ return v;+ }}tokio::time::sleep(Duration::from_millis(25)).await;}- panic!("billing counter at {key}:{field} did not appear within 1s");+ panic!(+ "billing counter at {key} did not appear within 1s for buckets {first_bucket}..={last_bucket}"+ );}diff --git a/rust/feature-flags/tests/test_flag_definitions.rs b/rust/feature-flags/tests/test_flag_definitions.rsindex 0e55975889c5..ad9560420a31 100644--- a/rust/feature-flags/tests/test_flag_definitions.rs+++ b/rust/feature-flags/tests/test_flag_definitions.rs@@ -2605,8 +2605,8 @@ async fn test_flag_definitions_billing_counter(#[case] skip_writes: bool) {let server = common::ServerHandle::for_config(config).await;let http = reqwest::Client::new();- // Capture before the request — the HTTP roundtrip can cross a 2-minute bucket boundary.- let bucket_field = current_bucket().to_string();+ // Bracket the request: the record lands in whichever 2-minute bucket it crosses.+ let bucket_before = current_bucket();let response = http.get(format!(@@ -2623,18 +2623,27 @@ async fn test_flag_definitions_billing_counter(#[case] skip_writes: bool) {"Response body: {}",response.text().await.unwrap());+ let bucket_after = current_bucket();if skip_writes {// Sleep ~5 flush windows so even a slow CI scheduler couldn't hide// an erroneous `record()` behind a delayed first tick.tokio::time::sleep(std::time::Duration::from_millis(500)).await;- let counter = redis.hget(billing_key, bucket_field).await;- assert!(- counter.is_err(),- "FlagDefinitions billing counter should NOT be incremented when skip_writes=true, got {counter:?}"- );+ for bucket in bucket_before..=bucket_after {+ let counter = redis.hget(billing_key.clone(), bucket.to_string()).await;+ assert!(+ counter.is_err(),+ "FlagDefinitions billing counter should NOT be incremented when skip_writes=true, got {counter:?}"+ );+ }} else {- let counter = common::poll_for_billing_counter(&redis, &billing_key, &bucket_field).await;+ let counter = common::poll_for_billing_counter_across_buckets(+ &redis,+ &billing_key,+ bucket_before,+ bucket_after,+ )+ .await;assert_eq!(counter, "1","FlagDefinitions billing counter should be incremented once"diff --git a/rust/feature-flags/tests/test_flags.rs b/rust/feature-flags/tests/test_flags.rsindex 0763c40ca4e0..10560474f095 100644--- a/rust/feature-flags/tests/test_flags.rs+++ b/rust/feature-flags/tests/test_flags.rs@@ -5817,25 +5817,34 @@ async fn test_skip_writes_suppresses_billing_redis_counter("distinct_id": distinct_id,});- // Capture before the request — the HTTP roundtrip can cross a 2-minute bucket boundary.- let bucket_field = current_bucket().to_string();+ // Bracket the request: the record lands in whichever 2-minute bucket it crosses.+ let bucket_before = current_bucket();let res = server.send_flags_request(payload.to_string(), Some("2"), None).await;assert_eq!(StatusCode::OK, res.status());+ let bucket_after = current_bucket();if skip_writes {// Sleep ~5 flush windows so even a slow CI scheduler couldn't hide// an erroneous `record()` behind a delayed first tick.tokio::time::sleep(std::time::Duration::from_millis(500)).await;- let counter = client.hget(billing_key, bucket_field).await;- assert!(- counter.is_err(),- "billing counter should NOT be incremented when skip_writes=true, got {counter:?}"- );+ for bucket in bucket_before..=bucket_after {+ let counter = client.hget(billing_key.clone(), bucket.to_string()).await;+ assert!(+ counter.is_err(),+ "billing counter should NOT be incremented when skip_writes=true, got {counter:?}"+ );+ }} else {- let counter = poll_for_billing_counter(&client, &billing_key, &bucket_field).await;+ let counter = poll_for_billing_counter_across_buckets(+ &client,+ &billing_key,+ bucket_before,+ bucket_after,+ )+ .await;assert_eq!(counter, "1","billing counter should be incremented when skip_writes=false"@@ -5903,8 +5912,8 @@ async fn test_billing_increments_land_in_production_keyspace() -> Result<()> {"distinct_id": distinct_id,});- // Capture before the request — the HTTP roundtrip can cross a 2-minute bucket boundary.- let bucket_field = current_bucket().to_string();+ // Bracket the request: the record lands in whichever 2-minute bucket it crosses.+ let bucket_before = current_bucket();// Direct reqwest call so we can set the SDK user-agent that drives// Library::from_headers → Library::PosthogNode.@@ -5917,11 +5926,16 @@ async fn test_billing_increments_land_in_production_keyspace() -> Result<()> {.send().await?;assert_eq!(StatusCode::OK, res.status());+ let bucket_after = current_bucket();- let team_counter = poll_for_billing_counter(&client, &team_key, &bucket_field).await;+ let team_counter =+ poll_for_billing_counter_across_buckets(&client, &team_key, bucket_before, bucket_after)+ .await;assert_eq!(team_counter, "1", "team key should reflect one request");- let library_counter = poll_for_billing_counter(&client, &library_key, &bucket_field).await;+ let library_counter =+ poll_for_billing_counter_across_buckets(&client, &library_key, bucket_before, bucket_after)+ .await;assert_eq!(library_counter, "1","library key should reflect one request"@@ -5976,30 +5990,34 @@ async fn test_shutdown_flush_writes_production_billing_counter() -> Result<()> {insert_flags_for_team_in_redis(client.clone(), team.id, Some(flag_json.to_string())).await?;let billing_key = get_team_request_key(team.id, FlagRequestType::Decide);- let bucket_field = current_bucket().to_string();client.del(billing_key.clone()).await.unwrap();let server = ServerHandle::for_config(config).await;let payload = json!({ "token": token, "distinct_id": distinct_id });+ // Bracket the request: the record lands in whichever 2-minute bucket it crosses.+ let bucket_before = current_bucket();let res = server.send_flags_request(payload.to_string(), Some("2"), None).await;assert_eq!(StatusCode::OK, res.status());+ let bucket_after = current_bucket();// Confirm the periodic flusher has NOT yet landed the aggregator's// write — sanity check that we're really exercising the shutdown// path. With a 60s flush interval and immediate-after-request// lookup, the target counter should still be missing.- let pre_shutdown = client.hget(billing_key.clone(), bucket_field.clone()).await;- assert!(- pre_shutdown.is_err(),- "billing counter at {billing_key} should NOT be in Redis before \- shutdown — the aggregator's periodic flusher is set to 60s and \- the test is faster than that. Got {pre_shutdown:?}, which means \- the flush window collapsed and this test no longer proves what \- it claims."- );+ for bucket in bucket_before..=bucket_after {+ let pre_shutdown = client.hget(billing_key.clone(), bucket.to_string()).await;+ assert!(+ pre_shutdown.is_err(),+ "billing counter at {billing_key} should NOT be in Redis before \+ shutdown — the aggregator's periodic flusher is set to 60s and \+ the test is faster than that. Got {pre_shutdown:?}, which means \+ the flush window collapsed and this test no longer proves what \+ it claims."+ );+ }// Trigger graceful shutdown. axum drains in-flight requests, then// `serve()` calls `billing_aggregator.shutdown().await` which performs@@ -6009,7 +6027,9 @@ async fn test_shutdown_flush_writes_production_billing_counter() -> Result<()> {// Poll for the counter to land. The helper polls for ~1s// (40 × 25ms) — the graceful-drain window plus a single shutdown// flush against a healthy local Redis is well under that.- let counter = poll_for_billing_counter(&client, &billing_key, &bucket_field).await;+ let counter =+ poll_for_billing_counter_across_buckets(&client, &billing_key, bucket_before, bucket_after)+ .await;assert_eq!(counter, "1","billing counter must reach Redis via the shutdown flush path"