: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(flags): out-of-band billing counter via shadow keyspace (#57150)
Phil Haackdec8d1ea1734
diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex 38af843a76a1..663a74835437 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2225,6 +2225,7 @@ name = "common-redis"version = "0.1.0"dependencies = ["async-trait",+ "futures-timer","redis","serde-pickle","testcontainers",diff --git a/rust/Cargo.toml b/rust/Cargo.tomlindex f512cebc166e..aa20db3e49f6 100644--- a/rust/Cargo.toml+++ b/rust/Cargo.toml@@ -97,6 +97,7 @@ envconfig = "0.10.0"eyre = "0.6.9"flate2 = "1.0"futures = { version = "0.3.29" }+futures-timer = "3.0"futures-util = { version = "0.3.31" }governor = { version = "0.5.1", features = ["dashmap"] }http = { version = "1.1.0" }diff --git a/rust/common/redis/Cargo.toml b/rust/common/redis/Cargo.tomlindex afb997a3207a..1499ef6cd2fb 100644--- a/rust/common/redis/Cargo.toml+++ b/rust/common/redis/Cargo.toml@@ -8,6 +8,10 @@ workspace = true[dependencies]async-trait = { workspace = true }+# `futures-timer` (not `tokio::time::sleep`) lets the mock module simulate+# blocking pipeline calls without making `tokio` a runtime dependency of every+# crate that links `common-redis`. Tiny crate, runtime-agnostic.+futures-timer = { workspace = true }redis = { workspace = true }serde-pickle = { version = "1.1.1" }thiserror = { workspace = true }@@ -15,6 +19,6 @@ tracing = { workspace = true }zstd = "0.13"[dev-dependencies]-tokio = { workspace = true }testcontainers = { workspace = true }+tokio = { workspace = true }diff --git a/rust/common/redis/src/client.rs b/rust/common/redis/src/client.rsindex a65f7df44731..2c96a26f1854 100644--- a/rust/common/redis/src/client.rs+++ b/rust/common/redis/src/client.rs@@ -519,6 +519,13 @@ impl Client for RedisClient {commands: Vec<PipelineCommand>,) -> Result<Vec<Result<PipelineResult, CustomRedisError>>, CustomRedisError> {let mut pipe = redis::pipe();+ // Wrap the batch in MULTI/EXEC so partial commits are impossible.+ // Without this, a connection drop after some commands have been+ // acknowledged returns `Err` here while leaving those commands+ // committed in Redis — which breaks any caller that retries on `Err`+ // with non-idempotent commands (e.g. HINCRBY in the billing+ // aggregator: a retry would double-increment the surviving writes).+ pipe.atomic();// Track commands for result processinglet mut metas: Vec<PipelineCommand> = Vec::with_capacity(commands.len());diff --git a/rust/common/redis/src/lib.rs b/rust/common/redis/src/lib.rsindex 312f49f8448e..be2e60e17893 100644--- a/rust/common/redis/src/lib.rs+++ b/rust/common/redis/src/lib.rs@@ -274,7 +274,15 @@ pub trait Client: Send + Sync {items: Vec<(String, String, usize)>, // (key, value, ttl_seconds)) -> Result<Vec<bool>, CustomRedisError>;async fn batch_del(&self, keys: Vec<String>) -> Result<(), CustomRedisError>;- /// Execute a batch of pipeline commands in a single round-trip.+ /// Execute a batch of pipeline commands atomically in a single round-trip.+ ///+ /// Commands are wrapped in `MULTI`/`EXEC`, so either every command commits+ /// or none do. A connection drop, server hangup, or read timeout after the+ /// pipeline is sent leaves Redis in its prior state — there is no+ /// partial-commit window that would surface as `Err` here while some+ /// commands had already taken effect server-side. This matters for+ /// non-idempotent operations (e.g. `HINCRBY`) where a retry on `Err`+ /// would otherwise risk double-counting.////// Returns a vector of results, one for each command in the same order./// The outer Result is for connection-level errors, inner Results are per-command.diff --git a/rust/common/redis/src/mock.rs b/rust/common/redis/src/mock.rsindex b7e13814f185..40f2a713cc7f 100644--- a/rust/common/redis/src/mock.rs+++ b/rust/common/redis/src/mock.rs@@ -1,6 +1,8 @@use async_trait::async_trait;use std::collections::HashMap;+use std::sync::atomic::{AtomicUsize, Ordering};use std::sync::{Arc, Mutex};+use std::time::Duration;use crate::pipeline::{PipelineCommand, PipelineResult};use crate::{Client, CustomRedisError, RedisValueFormat};@@ -22,6 +24,11 @@ pub struct MockRedisClient {expire_ret: HashMap<String, Result<bool, CustomRedisError>>,mget_ret: HashMap<String, Option<Vec<u8>>>,mget_error: Option<CustomRedisError>,+ pipeline_error: Option<CustomRedisError>,+ pipeline_block: Option<Duration>,+ pipeline_errors_by_call: HashMap<usize, CustomRedisError>,+ /// Shared via `Arc` so clones of the mock agree on call sequencing.+ pipeline_call_counter: Arc<AtomicUsize>,calls: Arc<Mutex<Vec<MockRedisCall>>>,}@@ -43,6 +50,10 @@ impl Default for MockRedisClient {expire_ret: HashMap::new(),mget_ret: HashMap::new(),mget_error: None,+ pipeline_error: None,+ pipeline_block: None,+ pipeline_errors_by_call: HashMap::new(),+ pipeline_call_counter: Arc::new(AtomicUsize::new(0)),calls: Arc::new(Mutex::new(Vec::new())),}}@@ -167,6 +178,30 @@ impl MockRedisClient {self.mget_error = Some(err);self.clone()}++ /// Make every `execute_pipeline` call return the given connection-level+ /// error. Commands are still recorded before the error is returned, so+ /// tests can assert which chunks were attempted.+ pub fn pipeline_error(&mut self, err: CustomRedisError) -> Self {+ self.pipeline_error = Some(err);+ self.clone()+ }++ /// Block every `execute_pipeline` call for the given duration before+ /// resolving. Used to exercise shutdown-timeout paths.+ pub fn pipeline_block(&mut self, duration: Duration) -> Self {+ self.pipeline_block = Some(duration);+ self.clone()+ }++ /// Fail the Nth `execute_pipeline` call (0-indexed across the lifetime of+ /// this mock) with the given error. Other calls proceed normally. Takes+ /// precedence over `pipeline_error`. Use to exercise partial-failure+ /// flush paths where some chunks succeed and others don't.+ pub fn pipeline_error_at_call(&mut self, call_index: usize, err: CustomRedisError) -> Self {+ self.pipeline_errors_by_call.insert(call_index, err);+ self.clone()+ }}#[derive(Debug, Clone)]@@ -570,6 +605,24 @@ impl Client for MockRedisClient {&self,commands: Vec<PipelineCommand>,) -> Result<Vec<Result<PipelineResult, CustomRedisError>>, CustomRedisError> {+ if let Some(duration) = self.pipeline_block {+ futures_timer::Delay::new(duration).await;+ }+ let call_index = self.pipeline_call_counter.fetch_add(1, Ordering::SeqCst);+ // Per-call error takes precedence so tests can mix success and failure.+ let injected_error = self+ .pipeline_errors_by_call+ .get(&call_index)+ .cloned()+ .or_else(|| self.pipeline_error.clone());+ if let Some(err) = injected_error {+ // Record commands as attempted so tests can assert which chunks+ // were tried, then return the injected connection-level error.+ for cmd in commands {+ drop(self.execute_pipeline_command(cmd));+ }+ return Err(err);+ }let results = commands.into_iter().map(|cmd| self.execute_pipeline_command(cmd))diff --git a/rust/feature-flags/src/api/flag_definitions.rs b/rust/feature-flags/src/api/flag_definitions.rsindex 7fee820881f6..e73db0755e79 100644--- a/rust/feature-flags/src/api/flag_definitions.rs+++ b/rust/feature-flags/src/api/flag_definitions.rs@@ -4,11 +4,10 @@ use crate::{errors::{ClientFacingError, FlagError},},flags::{- flag_analytics::{increment_request_count, is_billable_flag_key},- flag_request::FlagRequestType,+ flag_analytics::is_billable_flag_key, flag_request::FlagRequestType,flag_service::FlagService,},- handler::{billing::record_billing_increment_timing, types::Library},+ handler::{billing::record_billing_increment, types::Library},metrics::consts::{FLAG_DEFINITIONS_AUTH_COUNTER, FLAG_DEFINITIONS_CACHE_HIT_COUNTER,FLAG_DEFINITIONS_CACHE_MISS_COUNTER, FLAG_DEFINITIONS_ETAG_COUNTER,@@ -30,7 +29,6 @@ use serde_json::Value;use sqlx::PgPool;use std::collections::HashSet;use std::sync::Arc;-use std::time::Instant;use tracing::{info, warn};const ALLOWLIST_TTL_SECS: u64 = 60;@@ -237,26 +235,14 @@ pub async fn flags_definitions(// matching Django's /local_evaluation behavior.if !*state.config.skip_writes && has_billable_flags(&cached_response) {let library = Library::from_headers(&headers);- let start = Instant::now();- let result = increment_request_count(+ record_billing_increment(state.redis_client.clone(),+ state.billing_aggregator.as_ref(),team.id,- 1,FlagRequestType::FlagDefinitions,- Some(library),+ library,).await;- let elapsed_ms = start.elapsed().as_millis() as u64;-- record_billing_increment_timing(&result, elapsed_ms);-- if let Err(e) = result {- inc(- "flag_request_redis_error",- &[("error".to_string(), e.to_string())],- 1,- );- }}Ok(ok_response_with_etag(diff --git a/rust/feature-flags/src/billing/aggregator.rs b/rust/feature-flags/src/billing/aggregator.rsnew file mode 100644index 000000000000..9a8db25123aa--- /dev/null+++ b/rust/feature-flags/src/billing/aggregator.rs@@ -0,0 +1,2177 @@+//! `BillingAggregator` — in-process per-pod aggregation with periodic flush.+//!+//! `record()` is synchronous, lock-scoped, and returns in microseconds. A+//! background task wakes every `flush_interval`, atomically swaps the inner+//! map, and issues pipelined `HINCRBY`s to Redis. On graceful shutdown a+//! final flush runs before the service exits.+//!+//! # Durability trade-off+//!+//! Aggregation defers writes until the next flush, so failure modes differ+//! from a per-request synchronous write:+//!+//! - **Redis error on a flush chunk** (normal tick): failing chunk plus+//! any unattempted remainder are re-queued into `pending` and retried on+//! the next tick. Re-queued entries are **rebucketed to the current+//! bucket** so a sustained outage does not accumulate one new+//! `AggregationKey` per (team, request_type, library) on every+//! `CACHE_BUCKET_SIZE` rollover — cardinality stays bounded by the+//! active (team × request_type × library) surface regardless of outage+//! duration. Counts are conserved; per-bucket time attribution during+//! the outage is reattributed to the bucket current at retry time. The+//! `max_pending_entries` cap remains as a safety tripwire (not a+//! working-set estimate) — `flags_billing_unflushed_requests_total{cause="cap_drop"}`+//! firing now signals exotic failure (e.g. a single sustained-outage+//! pod with cardinality already at cap before the outage), not the+//! normal outage trajectory.+//! - **Graceful shutdown** (SIGTERM): a final best-effort flush runs+//! within `shutdown_flush_timeout`. Anything not flushed is reported+//! under `flags_billing_unflushed_requests_total{cause="shutdown_drop"}`.+//! - **Ungraceful termination** (SIGKILL past the grace window, OOM, node+//! loss, panic): **up to one `flush_interval` of records is lost.** The+//! `pending` map lives only in process memory; there is no WAL. The+//! crashed pod cannot emit at the moment of loss, so detection relies+//! on the **last scrape before the crash**: `flags_billing_pending_records`+//! gauges the per-pod count of at-risk records, and a non-zero value+//! correlated with a pod restart in+//! `kube_pod_container_status_restarts_total` attributes the loss. The+//! fleet-wide `flags_billing_records_total` vs.+//! `flags_billing_entries_flushed_total` rate ratio is a secondary+//! signal for the same gap.+//!+//! ## Detecting a wedged flusher before it drops counts+//!+//! - `flags_billing_pending_entries` — live gauge; sustained growth+//! is the leading indicator.+//! - `flags_billing_seconds_since_successful_flush` — stale-flush+//! alarm; rises even when `flags_billing_flush_errors_total` stays+//! flat (the signature of a hung `execute_pipeline`).+//! - `flags_billing_flush_errors_total` — labeled by `error_type`;+//! alert via `rate(flags_billing_flush_errors_total[1m]) > 0 for: 30s`+//! to catch a wedged Redis link.+//!+//! ## Knob-sizing guidance+//!+//! `flush_interval` directly bounds the crash-loss window. Shorter = less+//! worst-case data loss, more Redis RTTs; longer = better aggregation,+//! bigger worst-case loss. The default (10s) is sized so a typical SIGKILL+//! loses well under a minute of billable activity per pod while still+//! giving the aggregation meaningful compression.+//!+//! See `validate` for knob bounds. Mis-setting any knob to zero silently+//! breaks billing in a different way (panic, cap every record, instant+//! shutdown loss) — `validate` rejects them at boot.++use std::collections::HashMap;+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, Mutex};+use std::time::Duration;++use common_metrics::{gauge, histogram, inc};+use common_redis::{Client as RedisClient, CustomRedisError, PipelineCommand};+use rand::Rng;+use tokio::sync::Notify;+use tokio::task::JoinHandle;++use crate::flags::flag_analytics::{+ current_bucket, get_team_request_library_shadow_key, get_team_request_shadow_key,+};+use crate::flags::flag_request::FlagRequestType;+use crate::handler::types::Library;+use crate::metrics::consts::{+ FLAGS_BILLING_ENTRIES_FLUSHED, FLAGS_BILLING_FLUSH_DURATION_MS, FLAGS_BILLING_FLUSH_ERRORS,+ FLAGS_BILLING_PENDING_ENTRIES, FLAGS_BILLING_PENDING_RECORDS, FLAGS_BILLING_RECORDS,+ FLAGS_BILLING_RECORD_DURATION_US, FLAGS_BILLING_SECONDS_SINCE_SUCCESSFUL_FLUSH,+ FLAGS_BILLING_UNFLUSHED_REQUESTS,+};++fn record_labels_for(request_type: FlagRequestType) -> Vec<(String, String)> {+ vec![(+ "request_type".to_string(),+ request_type.as_str().to_string(),+ )]+}++/// Cause label for `flags_billing_unflushed_requests_total`. Kept as a+/// closed enum so a typo can't quietly create a new label value.+#[derive(Debug, Clone, Copy)]+enum UnflushedCause {+ CapDrop,+ FlushDroppedOnError,+ ShutdownDrop,+ RedisError,+}++impl UnflushedCause {+ fn as_str(self) -> &'static str {+ match self {+ UnflushedCause::CapDrop => "cap_drop",+ UnflushedCause::FlushDroppedOnError => "flush_dropped_on_error",+ UnflushedCause::ShutdownDrop => "shutdown_drop",+ UnflushedCause::RedisError => "redis_error",+ }+ }+}++fn inc_unflushed(cause: UnflushedCause, count: u64) {+ if count == 0 {+ return;+ }+ inc(+ FLAGS_BILLING_UNFLUSHED_REQUESTS,+ &[("cause".to_string(), cause.as_str().to_string())],+ count,+ );+}++/// Key used to aggregate repeated `record()` calls in-process.+///+/// `bucket` is computed from the record time, not the flush time — late-flushed+/// records must still land in the bucket they arrived in.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]+pub struct AggregationKey {+ pub team_id: i32,+ pub request_type: FlagRequestType,+ pub library: Option<Library>,+ pub bucket: u64,+}++/// Policy for handling a chunk error during a flush. The flusher's normal+/// tick uses `BailOnError` (if Redis is rejecting, save RTTs and retry next+/// interval). The shutdown path uses `BestEffort` — it's our last chance to+/// land writes before the pod exits, so don't abandon trailing chunks over a+/// single transient failure.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum FlushPolicy {+ BailOnError,+ BestEffort,+}++#[derive(Debug, Clone)]+pub struct BillingAggregatorConfig {+ /// Interval between flushes. Must be non-zero — see `validate`.+ pub flush_interval: Duration,+ /// Safety tripwire — expected steady-state size is orders of magnitude+ /// smaller. A non-zero rate of+ /// `flags_billing_unflushed_requests_total{cause="cap_drop"}` is an alert.+ /// Must be non-zero — see `validate`.+ pub max_pending_entries: usize,+ /// Must be non-zero — see `validate`.+ pub per_flush_batch_size: usize,+ /// Must be non-zero — see `validate`.+ pub shutdown_flush_timeout: Duration,+}++impl Default for BillingAggregatorConfig {+ fn default() -> Self {+ Self {+ flush_interval: Duration::from_secs(10),+ max_pending_entries: 500_000,+ per_flush_batch_size: 200,+ shutdown_flush_timeout: Duration::from_secs(15),+ }+ }+}++impl BillingAggregatorConfig {+ /// Reject zero/degenerate knob values that would break the flusher or+ /// silently disable billing. Called by `BillingAggregator::start` before+ /// spawning the background task so a misconfigured deploy fails loudly at+ /// boot instead of crashing the flusher task minutes later.+ pub fn validate(&self) -> Result<(), String> {+ if self.flush_interval.is_zero() {+ return Err("flush_interval must be > 0 (tokio::time::interval panics on zero)".into());+ }+ if self.max_pending_entries == 0 {+ return Err(+ "max_pending_entries must be > 0 (zero caps every new key immediately)".into(),+ );+ }+ if self.per_flush_batch_size == 0 {+ return Err("per_flush_batch_size must be > 0".into());+ }+ if self.shutdown_flush_timeout.is_zero() {+ return Err(+ "shutdown_flush_timeout must be > 0 (zero loses every pending count on shutdown)"+ .into(),+ );+ }+ Ok(())+ }+}++/// In-process counter aggregation for billable flag requests.+pub struct BillingAggregator {+ inner: Arc<Inner>,+ flusher: Mutex<Option<JoinHandle<()>>>,+ metrics_sampler: Mutex<Option<JoinHandle<()>>>,+}++struct Inner {+ config: BillingAggregatorConfig,+ redis: Arc<dyn RedisClient + Send + Sync>,+ pending: Mutex<HashMap<AggregationKey, u64>>,+ /// Running sum of `pending.values()`. Updated under the `pending` lock so+ /// increments and the lock-protected HashMap mutation are guaranteed+ /// consistent. The flush drain swaps it to zero, the requeue path adds+ /// back what's reinjected, and shutdown reads it as the residual count of+ /// records still in `pending`.+ pending_total: AtomicU64,+ /// Counts drained from `pending` but not yet credited to any terminal+ /// counter (`entries_flushed_total` or+ /// `unflushed_requests_total{cause="flush_dropped_on_error"}`).+ /// Non-zero only while `flush_once` is mid-flight. If the flusher task is+ /// aborted mid-flush (e.g. shutdown timeout), this preserves the count of+ /// lost records so `record_shutdown_drops` can report it.+ in_flight_uncredited: AtomicU64,+ /// Unix epoch (ms) of the last successful flush. Zero = no successful+ /// flush has occurred yet. Read by the metrics sampler to compute+ /// `seconds_since_successful_flush`. A hung flusher shows up here as a+ /// monotonically increasing age while `flush_errors_total` stays at 0+ /// (a hung flush never completes its success/failure accounting).+ last_successful_flush_epoch_ms: AtomicU64,+ /// Per-request-type record counters. Bumped on every `record()` call and+ /// drained at flush time, so the hot path does one atomic increment+ /// instead of an `inc()` call that allocates label strings every time.+ record_count_decide: AtomicU64,+ record_count_flag_definitions: AtomicU64,+ shutdown_signal: Notify,+}++impl Inner {+ fn new(+ redis: Arc<dyn RedisClient + Send + Sync>,+ config: BillingAggregatorConfig,+ ) -> Arc<Self> {+ Arc::new(Self {+ config,+ redis,+ pending: Mutex::new(HashMap::new()),+ pending_total: AtomicU64::new(0),+ in_flight_uncredited: AtomicU64::new(0),+ last_successful_flush_epoch_ms: AtomicU64::new(0),+ record_count_decide: AtomicU64::new(0),+ record_count_flag_definitions: AtomicU64::new(0),+ shutdown_signal: Notify::new(),+ })+ }++ /// Sum any remaining entries in `pending` plus any drained-but-uncredited+ /// counts from an interrupted flush, and emit them as shutdown drops.+ fn record_shutdown_drops(&self) {+ let remaining_pending = self.pending_total.load(Ordering::Relaxed);+ let in_flight = self.in_flight_uncredited.load(Ordering::Relaxed);+ inc_unflushed(UnflushedCause::ShutdownDrop, remaining_pending + in_flight);+ }+}++impl BillingAggregator {+ /// Construct an aggregator and spawn its flusher task.+ ///+ /// The flusher ticks on `flush_interval + random jitter` so a fleet-wide+ /// deploy doesn't synchronize every pod's flush into the same Redis burst.+ pub fn start(+ redis: Arc<dyn RedisClient + Send + Sync>,+ config: BillingAggregatorConfig,+ ) -> Arc<Self> {+ // Fail fast on misconfiguration. A misconfigured FLAGS_BILLING_*+ // env var would otherwise panic the flusher task silently, or silently+ // disable billing — both worse than refusing to boot.+ if let Err(e) = config.validate() {+ panic!("invalid BillingAggregatorConfig: {e}");+ }++ let flush_interval_ms = config.flush_interval.as_millis() as u64;+ let max_pending_entries = config.max_pending_entries;+ let per_flush_batch_size = config.per_flush_batch_size;++ let inner = Inner::new(redis, config);+ let flusher = tokio::spawn(run_flusher(inner.clone()));+ let metrics_sampler = tokio::spawn(run_metrics_sampler(inner.clone()));++ tracing::info!(+ flush_interval_ms,+ max_pending_entries,+ per_flush_batch_size,+ "BillingAggregator started"+ );++ Arc::new(Self {+ inner,+ flusher: Mutex::new(Some(flusher)),+ metrics_sampler: Mutex::new(Some(metrics_sampler)),+ })+ }++ /// Record a single billable request. Non-blocking; no Redis I/O.+ ///+ /// **Lifecycle constraint:** must not be called after `shutdown()` has+ /// returned. The flusher task is consumed by shutdown, so a post-shutdown+ /// record lands in `pending` and is never flushed or accounted as a drop —+ /// it is silently lost with no metric. In the normal server lifecycle+ /// `serve()` calls `shutdown()` only after `axum::serve()` has fully+ /// drained, which means every request handler that holds the aggregator+ /// `Arc` has already returned. Background tasks or deferred work that+ /// outlive `axum::serve()` and still hold the aggregator `Arc` would+ /// violate this constraint — don't do that.+ pub fn record(&self, team_id: i32, request_type: FlagRequestType, library: Option<Library>) {+ // Time the body so the duration histogram can surface `pending` mutex+ // contention. Empty label slice so the per-call emission doesn't+ // allocate inside `apply_label_filter`.+ let start = std::time::Instant::now();++ // Bump the per-request-type atomic counter; the flusher emits the+ // `FLAGS_BILLING_RECORDS` metric in batches at flush time so the+ // hot path doesn't pay the per-call label clone in `inc()`.+ match request_type {+ FlagRequestType::Decide => &self.inner.record_count_decide,+ FlagRequestType::FlagDefinitions => &self.inner.record_count_flag_definitions,+ }+ .fetch_add(1, Ordering::Relaxed);++ let key = AggregationKey {+ team_id,+ request_type,+ library,+ bucket: current_bucket(),+ };++ {+ let mut pending = self.inner.pending.lock().unwrap();++ if pending.len() >= self.inner.config.max_pending_entries && !pending.contains_key(&key)+ {+ // Cap hit on a new key: drop the incoming record rather than+ // evicting an existing entry. Eviction would be O(n) under+ // the hot-path mutex (a scan to find the oldest bucket). The+ // cap is a tripwire, not a steady-state path —+ // `flags_billing_unflushed_requests_total{cause="cap_drop"}`+ // alerts on any non-zero rate — and the flusher drains the+ // map every `flush_interval`, so the cap state is transient.+ // Existing-key increments are always allowed (they don't+ // grow the map).+ inc_unflushed(UnflushedCause::CapDrop, 1);+ } else {+ *pending.entry(key).or_insert(0) += 1;+ // Update the running total under the same lock so a+ // concurrent reader can never observe `pending` and+ // `pending_total` out of step.+ self.inner.pending_total.fetch_add(1, Ordering::Relaxed);+ }+ }++ // `as_nanos() / 1000.0` preserves sub-microsecond resolution that+ // `as_micros()` would truncate, since uncontended record() is+ // typically a few hundred nanoseconds.+ let elapsed_us = start.elapsed().as_nanos() as f64 / 1000.0;+ histogram(FLAGS_BILLING_RECORD_DURATION_US, &[], elapsed_us);+ }++ /// Perform a final flush and stop the flusher task.+ ///+ /// Must be called after `axum::serve(...).await` resolves so all+ /// in-flight requests have already recorded. Times out after+ /// `shutdown_flush_timeout`; any remaining entries are counted in+ /// `flags_billing_unflushed_requests_total{cause="shutdown_drop"}`.+ ///+ /// Concurrent callers: only the first caller performs the flush; any+ /// other caller racing into `shutdown()` returns immediately without+ /// waiting for the first caller's flush to complete. If multiple tasks+ /// need to await flush completion, wrap the call in a `tokio::sync::OnceCell`+ /// (or similar) outer guard. Sequential calls are safe — the second is+ /// a no-op. Call exactly once from the task that owns the server lifecycle.+ pub async fn shutdown(&self) {+ // Sampler is best-effort emission — `abort` is fine; a lost last+ // tick doesn't matter operationally.+ if let Some(handle) = self.metrics_sampler.lock().unwrap().take() {+ handle.abort();+ }++ // `notify_one()` (not `notify_waiters()`): if the flusher happens to be+ // mid-tick when we fire, the notify must be stored so the next+ // `notified().await` returns immediately. `notify_waiters()` would be+ // a no-op in that window.+ self.inner.shutdown_signal.notify_one();++ let handle = self.flusher.lock().unwrap().take();+ if let Some(handle) = handle {+ // Hold onto an abort handle: dropping a `JoinHandle` does NOT+ // cancel the task, and we need to be sure a timed-out flusher+ // can't keep running after the process-level shutdown has moved+ // on (and can't later double-credit counts we just recorded as+ // shutdown drops).+ let abort_handle = handle.abort_handle();+ let timeout = self.inner.config.shutdown_flush_timeout;+ match tokio::time::timeout(timeout, handle).await {+ Ok(Ok(())) => {+ tracing::info!("BillingAggregator: shutdown flush completed");+ }+ Ok(Err(join_err)) => {+ tracing::error!(error = %join_err, "BillingAggregator flusher panicked during shutdown");+ self.inner.record_shutdown_drops();+ }+ Err(_) => {+ tracing::warn!(+ timeout_ms = timeout.as_millis() as u64,+ "BillingAggregator: shutdown flush timed out"+ );+ abort_handle.abort();+ self.inner.record_shutdown_drops();+ }+ }+ }+ }++ #[cfg(test)]+ pub fn pending_len(&self) -> usize {+ self.inner.pending.lock().unwrap().len()+ }++ #[cfg(test)]+ pub fn in_flight_uncredited(&self) -> u64 {+ self.inner.in_flight_uncredited.load(Ordering::Relaxed)+ }++ #[cfg(test)]+ pub fn pending_total(&self) -> u64 {+ self.inner.pending_total.load(Ordering::Relaxed)+ }++ /// Tests sometimes need to set up `pending` directly (e.g. to inject+ /// non-`current_bucket()` buckets, or to construct an empty-drain+ /// scenario by clearing previously-recorded data). Going through this+ /// helper keeps `pending_total` in step with `pending` — `record()`+ /// maintains that invariant on the production path, and the metrics+ /// sampler reads `pending_total` lock-free assuming it holds.+ #[cfg(test)]+ fn seed_pending(&self, entries: impl IntoIterator<Item = (AggregationKey, u64)>) {+ let mut pending = self.inner.pending.lock().unwrap();+ let mut total = self.inner.pending_total.load(Ordering::Relaxed);+ for (key, count) in entries {+ *pending.entry(key).or_insert(0) += count;+ total += count;+ }+ self.inner.pending_total.store(total, Ordering::Relaxed);+ }++ /// Construct an aggregator without spawning the background flusher. For+ /// test harnesses that don't exercise periodic flushing — `record()` still+ /// fills the pending map, but counts only land in Redis if the test calls+ /// `flush_once` directly. `shutdown()` is a no-op (no task to join), so+ /// nothing leaks at runtime teardown. Not for production use.+ #[doc(hidden)]+ pub fn for_tests(+ redis: Arc<dyn RedisClient + Send + Sync>,+ config: BillingAggregatorConfig,+ ) -> Arc<Self> {+ Arc::new(Self {+ inner: Inner::new(redis, config),+ flusher: Mutex::new(None),+ metrics_sampler: Mutex::new(None),+ })+ }+}++/// Spawned task: tick → drain → flush, until the shutdown signal fires.+async fn run_flusher(inner: Arc<Inner>) {+ // Initial jitter desynchronizes fleet-wide flushes after a coordinated+ // deploy. Race it against shutdown so a quick SIGTERM doesn't wait for the+ // jitter to elapse before flushing.+ let jitter = pick_jitter(inner.config.flush_interval);+ tokio::select! {+ _ = tokio::time::sleep(jitter) => {}+ _ = inner.shutdown_signal.notified() => {+ flush_once(&inner, FlushPolicy::BestEffort).await;+ return;+ }+ }++ let mut interval = tokio::time::interval(inner.config.flush_interval);+ // `Delay` (not `Skip`): if a flush runs longer than the interval, the next+ // tick still fires — buffered records keep accumulating and the cap+ // tripwire handles unbounded growth. `Skip` would silently drop ticks+ // during sustained slowness, leaving counts stranded longer than+ // `flush_interval`.+ interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);+ // First tick fires immediately — swallow it so the first real flush+ // happens one interval from now, after traffic has had a chance to+ // accumulate.+ interval.tick().await;++ loop {+ tokio::select! {+ _ = interval.tick() => {+ flush_once(&inner, FlushPolicy::BailOnError).await;+ }+ _ = inner.shutdown_signal.notified() => {+ tracing::info!("BillingAggregator: shutdown signal received, performing final flush");+ flush_once(&inner, FlushPolicy::BestEffort).await;+ return;+ }+ }+ }+}++/// Outcome of a single pipeline-chunk execution. `ErrBail` is how+/// `flush_chunk` signals to its caller that the current flush should stop+/// attempting further chunks (the caller is responsible for requeuing any+/// entries it hadn't yet handed off).+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum ChunkOutcome {+ Ok,+ Err,+ ErrBail,+}++/// Execute one pipeline chunk and classify the result. Moves `chunk_entries`+/// into `requeue` on a `BailOnError` error so those entries retry next tick;+/// clears `chunk_entries` otherwise. `flushed_counts` is updated in place so+/// the caller can reconcile `in_flight_uncredited`. Dropped counts in+/// `BestEffort` mode are not tracked here — the residual in+/// `in_flight_uncredited` after the loop equals the total dropped, and the+/// caller credits it under `flush_dropped_on_error`.+///+/// Treats any `Ok(_)` from `execute_pipeline` as a fully-successful chunk —+/// per-command inner errors inside the returned vector are not inspected.+/// Safe today because every command in the pipeline is `HINCRBY` on a+/// pre-existing hash key, which cannot fail at the per-command level.+/// Adding non-`HINCRBY` operations to this pipeline requires revisiting this+/// assumption to avoid silent over-crediting of `entries_flushed_total`.+async fn flush_chunk(+ inner: &Arc<Inner>,+ commands: Vec<PipelineCommand>,+ chunk_counts: u64,+ chunk_entries: &mut Vec<(AggregationKey, u64)>,+ policy: FlushPolicy,+ flushed_counts: &mut u64,+ requeue: &mut Vec<(AggregationKey, u64)>,+) -> ChunkOutcome {+ match inner.redis.execute_pipeline(commands).await {+ Ok(_) => {+ *flushed_counts += chunk_counts;+ // Credit per-chunk so a shutdown abort between chunks doesn't+ // strand counts already written to Redis under an unincremented+ // `entries_flushed_total`.+ inc(FLAGS_BILLING_ENTRIES_FLUSHED, &[], chunk_counts);+ chunk_entries.clear();+ ChunkOutcome::Ok+ }+ Err(e) => {+ record_chunk_error(&e, chunk_counts, policy);+ match policy {+ FlushPolicy::BailOnError => {+ requeue.append(chunk_entries);+ ChunkOutcome::ErrBail+ }+ FlushPolicy::BestEffort => {+ chunk_entries.clear();+ ChunkOutcome::Err+ }+ }+ }+ }+}++/// Swap the pending map, flush the drained batch to Redis, record metrics.+///+/// Chunks at `AggregationKey` boundaries (never splits a key's team-level+/// and SDK-level writes across chunks). Per-chunk success credits+/// `entries_flushed_total` so successful chunks stay credited even when a+/// later chunk fails. `in_flight_uncredited` tracks what's still unaccounted+/// for so a mid-flush abort (e.g. shutdown timeout) is still reportable via+/// shutdown drops.+///+/// On a chunk error: `BailOnError` (normal tick) stops attempting further+/// chunks and merges the failing chunk's entries plus the unattempted+/// remainder back into `pending` for the next tick, bumping+/// `flush_requeued_total`. `BestEffort` (shutdown) keeps attempting and+/// records unrecoverable losses under+/// `unflushed_requests_total{cause="flush_dropped_on_error"}` — the process+/// is exiting, there is no next tick.+async fn flush_once(inner: &Arc<Inner>, policy: FlushPolicy) {+ // Drain the per-request-type record counters and emit+ // FLAGS_BILLING_RECORDS. Doing this here instead of in `record()`+ // keeps the hot path free of the per-call `apply_label_filter`+ // allocation that `inc()` triggers.+ let decide_records = inner.record_count_decide.swap(0, Ordering::Relaxed);+ if decide_records > 0 {+ inc(+ FLAGS_BILLING_RECORDS,+ &record_labels_for(FlagRequestType::Decide),+ decide_records,+ );+ }+ let flag_def_records = inner+ .record_count_flag_definitions+ .swap(0, Ordering::Relaxed);+ if flag_def_records > 0 {+ inc(+ FLAGS_BILLING_RECORDS,+ &record_labels_for(FlagRequestType::FlagDefinitions),+ flag_def_records,+ );+ }++ // Swap pending and zero `pending_total` under the same lock so a+ // concurrent reader can never see pending empty with the atomic non-zero+ // (or vice-versa). The atomic's value at swap time equals the sum of the+ // drained map's values, since they're maintained together.+ let (drained, total_counts): (HashMap<AggregationKey, u64>, u64) = {+ let mut pending = inner.pending.lock().unwrap();+ let drained = std::mem::take(&mut *pending);+ let total = inner.pending_total.swap(0, Ordering::Relaxed);+ (drained, total)+ };++ if drained.is_empty() {+ // An empty drain is still evidence the flusher loop is alive. Stamp+ // the epoch so an idle pod doesn't trip the stale-flush alarm — a+ // low-traffic pod that flushed once at boot and then went quiet+ // would otherwise show monotonically rising+ // `seconds_since_successful_flush` while remaining perfectly healthy.+ inner+ .last_successful_flush_epoch_ms+ .store(now_epoch_ms(), Ordering::Relaxed);+ return;+ }++ let start = std::time::Instant::now();+ // Publish in-flight *before* the first await so an abort anywhere in+ // the flush still reports the correct residual. `total_counts` came+ // from `pending_total.swap(0)` above, so it equals the sum of drained+ // values without re-walking them.+ inner+ .in_flight_uncredited+ .store(total_counts, Ordering::Relaxed);++ let batch_size = inner.config.per_flush_batch_size.max(1);+ // Each key produces 1-2 commands, so over-provision by 1 to avoid a+ // realloc when the last key pushes the buffer past batch_size.+ let mut buffer: Vec<PipelineCommand> = Vec::with_capacity(batch_size + 1);+ let mut chunk_entries: Vec<(AggregationKey, u64)> = Vec::with_capacity(batch_size);+ let mut buffer_counts: u64 = 0;+ let mut flushed_counts: u64 = 0;+ let mut any_error = false;+ let mut requeue: Vec<(AggregationKey, u64)> = Vec::new();++ let mut iter = drained.into_iter();+ while let Some((key, count)) = iter.next() {+ // Clamp to i64::MAX for the HINCRBY signed parameter. The pending+ // map's u64 counts are bounded by `max_pending_entries * (records+ // per key per flush interval)`, well below i64::MAX in practice.+ let count_i64 = i64::try_from(count).unwrap_or(i64::MAX);+ let field = key.bucket.to_string();+ // Push the library command first so the `field` String can be moved+ // into the team command without an extra clone.+ if let Some(library) = key.library {+ buffer.push(PipelineCommand::HIncrBy {+ key: get_team_request_library_shadow_key(key.team_id, key.request_type, library),+ field: field.clone(),+ count: count_i64,+ });+ }+ buffer.push(PipelineCommand::HIncrBy {+ key: get_team_request_shadow_key(key.team_id, key.request_type),+ field,+ count: count_i64,+ });+ chunk_entries.push((key, count));+ buffer_counts += count;++ if buffer.len() >= batch_size {+ let outcome = flush_chunk(+ inner,+ std::mem::take(&mut buffer),+ buffer_counts,+ &mut chunk_entries,+ policy,+ &mut flushed_counts,+ &mut requeue,+ )+ .await;+ buffer_counts = 0;+ buffer.reserve(batch_size + 1);+ match outcome {+ ChunkOutcome::Ok => {+ inner+ .in_flight_uncredited+ .store(total_counts - flushed_counts, Ordering::Relaxed);+ }+ ChunkOutcome::Err => {+ any_error = true;+ }+ ChunkOutcome::ErrBail => {+ any_error = true;+ // Drain the remainder of the iterator into requeue so+ // unattempted entries retry on the next tick. Also emit+ // `unflushed_requests_total{cause="redis_error"}` for the+ // remainder so the rate reflects all requests blocked by+ // the error, not just the chunk that hit Redis. These+ // records aren't terminally lost (they'll retry next+ // tick), but the metric is an incident-magnitude signal,+ // and bail-mode otherwise hides everything past the+ // first failing chunk.+ let remainder: Vec<(AggregationKey, u64)> = iter.by_ref().collect();+ let remainder_counts: u64 = remainder.iter().map(|(_, c)| *c).sum();+ inc_unflushed(UnflushedCause::RedisError, remainder_counts);+ requeue.extend(remainder);+ break;+ }+ }+ }+ }++ // Trailing partial chunk. In bail mode we broke above with an empty+ // buffer; in best-effort mode this may reveal a final error.+ if !buffer.is_empty() {+ let outcome = flush_chunk(+ inner,+ std::mem::take(&mut buffer),+ buffer_counts,+ &mut chunk_entries,+ policy,+ &mut flushed_counts,+ &mut requeue,+ )+ .await;+ match outcome {+ ChunkOutcome::Ok => {+ inner+ .in_flight_uncredited+ .store(total_counts - flushed_counts, Ordering::Relaxed);+ }+ ChunkOutcome::Err | ChunkOutcome::ErrBail => {+ any_error = true;+ }+ }+ }++ let elapsed_ms = start.elapsed().as_millis() as f64;+ histogram(FLAGS_BILLING_FLUSH_DURATION_MS, &[], elapsed_ms);++ // Re-merge failed entries into `pending` so they retry next tick.+ // Ordering: drop `in_flight_uncredited` *before* inserting into+ // `pending`. A mid-requeue abort then under-counts (some entries in+ // neither place) rather than double-counting (counted in both). Under-+ // count is the preferable failure mode for billing.+ //+ // Lock discipline: the O(N) merge runs *outside* the pending lock.+ // We swap out the fresh pending (small — only records added during+ // the flush), merge locally, and swap back. This keeps the hot-path+ // `record()` critical section unblocked under a Redis outage, when+ // `requeue` can be up to `max_pending_entries` large.+ //+ // Rebucket-on-requeue: entries are reattributed to `current_bucket()`+ // before being merged. Without this, a sustained Redis outage would+ // accumulate one new `AggregationKey` per (team, request_type, library)+ // every `CACHE_BUCKET_SIZE` (120s) as the wall clock advances —+ // cardinality scales with outage minutes regardless of traffic, and the+ // `max_pending_entries` cap eventually trips `cap_drop` on new keys.+ // Collapsing forward bounds cardinality to (team × request_type ×+ // library) and conserves the count, at the cost of per-bucket time+ // attribution during the outage. Billing aggregates over months, so the+ // attribution slip is invisible at invoice time; per-bucket quota+ // enforcement during the outage itself is impossible anyway because+ // the data isn't in Redis.+ let requeued_counts: u64 = requeue.iter().map(|(_, c)| *c).sum();+ if !requeue.is_empty() {+ inner+ .in_flight_uncredited+ .fetch_sub(requeued_counts, Ordering::Relaxed);+ let mut merged: HashMap<AggregationKey, u64> = {+ let mut pending = inner.pending.lock().unwrap();+ std::mem::take(&mut *pending)+ };+ let now_bucket = current_bucket();+ for (mut key, count) in requeue {+ key.bucket = now_bucket;+ *merged.entry(key).or_insert(0) += count;+ }+ {+ let mut pending = inner.pending.lock().unwrap();+ // Reconcile any records that arrived during the merge.+ for (key, count) in pending.drain() {+ *merged.entry(key).or_insert(0) += count;+ }+ *pending = merged;+ // The requeued counts weren't in `pending_total` (they came from+ // `drained`, which already swapped to 0). Records that arrived+ // during the flush are already in `pending_total` from their+ // `record()` calls. Add the requeued sum back inside this lock so+ // a sampler tick can never see `pending_total` lagging the map.+ inner+ .pending_total+ .fetch_add(requeued_counts, Ordering::Relaxed);+ }+ }++ // Zero `in_flight_uncredited` *before* crediting `flush_dropped_on_error`+ // so a concurrent `record_shutdown_drops` cannot read both: a non-zero+ // residual *and* the about-to-happen FlushDroppedOnError credit. Without+ // this swap-before-credit ordering, a shutdown deadline firing between+ // the credit and the zero would re-credit `dropped_counts` as+ // `shutdown_drop`, double-counting in+ // `flags_billing_unflushed_requests_total`.+ //+ // After the swap, exactly one of the two paths sees the residual:+ // - this thread credits FlushDroppedOnError(dropped),+ // - or `record_shutdown_drops` reads in_flight=0 and credits only+ // remaining_pending under ShutdownDrop.+ //+ // `AcqRel` because this is a read-modify-write whose Release half pairs+ // with any later observer of `in_flight_uncredited` — Relaxed would+ // permit the credit to be reordered before the swap on weak-memory+ // architectures, reintroducing the race the swap is meant to close.+ let in_flight_residual = inner.in_flight_uncredited.swap(0, Ordering::AcqRel);+ if any_error {+ inc_unflushed(UnflushedCause::FlushDroppedOnError, in_flight_residual);+ } else {+ // Record the successful-flush timestamp so the metrics sampler can+ // compute `seconds_since_successful_flush`. Stamp on any fully+ // error-free flush — a partial-success BestEffort flush still+ // dropped counts and shouldn't reset the "stale flush" alarm. The+ // empty-drain branch above stamps for the same reason: a tick with+ // nothing to flush is still proof the loop is alive.+ inner+ .last_successful_flush_epoch_ms+ .store(now_epoch_ms(), Ordering::Relaxed);+ }+}++/// Emit `FLUSH_ERRORS` (1 per failed chunk, classified by `error_type`) and+/// — under `BailOnError` only — `unflushed_requests_total{cause="redis_error"}`+/// for the chunk's aggregated request count, plus a warn-level log carrying+/// the raw error (never on a metric label — unbounded cardinality risk).+///+/// `requests_in_chunk` is the count of records affected by this chunk's+/// failure. Under `BailOnError`, `flush_once` separately emits the same cause+/// for the unattempted remainder so the rate reflects all requests blocked by+/// the error, not just the chunk that hit it. See the+/// `FLAGS_BILLING_UNFLUSHED_REQUESTS` cause docs in `metrics::consts` for why+/// `redis_error` is suppressed on the `BestEffort` (shutdown) path.+fn record_chunk_error(e: &CustomRedisError, requests_in_chunk: u64, policy: FlushPolicy) {+ inc(+ FLAGS_BILLING_FLUSH_ERRORS,+ &[(+ "error_type".to_string(),+ classify_redis_error(e).to_string(),+ )],+ 1,+ );+ if policy == FlushPolicy::BailOnError {+ inc_unflushed(UnflushedCause::RedisError, requests_in_chunk);+ }+ tracing::warn!(+ error = %e,+ requests_in_chunk,+ "BillingAggregator: flush pipeline failed"+ );+}++pub(crate) fn classify_redis_error(err: &CustomRedisError) -> &'static str {+ // Exhaustive on purpose: a new `CustomRedisError` variant should produce a+ // compile error here so a new error type gets a deliberate+ // `error_type` label rather than disappearing into "other".+ match err {+ CustomRedisError::Timeout => "timeout",+ CustomRedisError::Redis(_) => "transport",+ CustomRedisError::NotFound => "not_found",+ CustomRedisError::ParseError(_) => "parse",+ CustomRedisError::InvalidConfiguration(_) => "config",+ }+}++/// Interval between live-gauge samples. Sized to the Prometheus scrape+/// cadence (15s default): faster sampling produces values nothing reads,+/// while still surfacing a wedged flusher within a couple of scrape+/// windows rather than waiting for the next flush interval. Each tick+/// briefly takes the `pending` mutex, which is the same lock `record()`+/// holds on the hot path, so cadence is a contention budget — not just a+/// CPU one.+const METRICS_SAMPLE_INTERVAL: Duration = Duration::from_secs(5);++fn now_epoch_ms() -> u64 {+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_millis() as u64)+ .unwrap_or(0)+}++/// Spawned task: periodically samples `pending.len()`, `in_flight_uncredited`,+/// and time-since-last-successful-flush, and emits them as gauges.+///+/// Runs independently of `run_flusher` so a hung `execute_pipeline` can't+/// freeze these gauges — the whole point of sampling outside the flush+/// path is to surface a wedged flusher. Exits via `abort()` at shutdown.+async fn run_metrics_sampler(inner: Arc<Inner>) {+ let mut interval = tokio::time::interval(METRICS_SAMPLE_INTERVAL);+ interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);+ // Swallow the immediate first tick so startup doesn't emit zero-valued+ // samples before the aggregator has seen any traffic.+ interval.tick().await;++ loop {+ interval.tick().await;+ sample_metrics(&inner);+ }+}++fn sample_metrics(inner: &Arc<Inner>) {+ let pending_len = inner.pending.lock().unwrap().len();+ gauge(FLAGS_BILLING_PENDING_ENTRIES, &[], pending_len as f64);++ // Lock-free read: `pending_total` is maintained under the same lock as+ // `pending` mutations, so its value is a consistent snapshot from the+ // sampler's perspective. Slight skew vs. `pending_len` is fine — both+ // gauges are scrape-cadence approximations.+ let pending_records = inner.pending_total.load(Ordering::Relaxed);+ gauge(FLAGS_BILLING_PENDING_RECORDS, &[], pending_records as f64);++ let last_ms = inner.last_successful_flush_epoch_ms.load(Ordering::Relaxed);+ gauge(+ FLAGS_BILLING_SECONDS_SINCE_SUCCESSFUL_FLUSH,+ &[],+ compute_staleness_seconds(last_ms, now_epoch_ms()),+ );+}++/// Stale-flush gauge math, factored out so tests can pin the invariants+/// without observing global metric state. `last_ms == 0` means no successful+/// flush has happened yet — report 0 rather than "now - 0 = decades since+/// 1970", so a freshly booted pod with no traffic doesn't trip alerts.+/// `saturating_sub` clamps the backward-NTP-step case (`now < last`) to 0.+fn compute_staleness_seconds(last_ms: u64, now_ms: u64) -> f64 {+ if last_ms == 0 {+ 0.0+ } else {+ now_ms.saturating_sub(last_ms) as f64 / 1000.0+ }+}++fn pick_jitter(flush_interval: Duration) -> Duration {+ // Up to 10% of the flush interval, capped at 1s — enough to desynchronize+ // fleet-wide flushes without leaving records stranded for multiple+ // seconds at startup.+ const MAX_JITTER_MS: u64 = 1_000;+ let max_jitter_ms = ((flush_interval.as_millis() / 10) as u64).clamp(1, MAX_JITTER_MS);+ let jitter_ms = rand::thread_rng().gen_range(0..max_jitter_ms);+ Duration::from_millis(jitter_ms)+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::flags::flag_analytics::{+ get_team_request_library_shadow_key, get_team_request_shadow_key,+ };+ use common_redis::{MockRedisClient, MockRedisValue};+ use rstest::rstest;++ fn test_config() -> BillingAggregatorConfig {+ BillingAggregatorConfig {+ flush_interval: Duration::from_millis(50),+ max_pending_entries: 10_000,+ per_flush_batch_size: 200,+ shutdown_flush_timeout: Duration::from_secs(1),+ }+ }++ /// Build an aggregator without spawning the flusher, so tests can drive+ /// flushes deterministically via `flush_once`.+ fn new_test_aggregator(+ config: BillingAggregatorConfig,+ ) -> (Arc<MockRedisClient>, Arc<BillingAggregator>) {+ new_test_aggregator_with_redis(MockRedisClient::new(), config)+ }++ fn new_test_aggregator_with_redis(+ redis: MockRedisClient,+ config: BillingAggregatorConfig,+ ) -> (Arc<MockRedisClient>, Arc<BillingAggregator>) {+ let redis = Arc::new(redis);+ let agg = Arc::new(BillingAggregator {+ inner: Inner::new(redis.clone(), config),+ flusher: Mutex::new(None),+ metrics_sampler: Mutex::new(None),+ });+ (redis, agg)+ }++ fn hincrby_calls(redis: &MockRedisClient) -> Vec<(String, i64)> {+ redis+ .get_calls()+ .into_iter()+ .filter(|c| c.op == "pipeline_hincrby")+ .filter_map(|c| match c.value {+ MockRedisValue::I64(v) => Some((c.key, v)),+ _ => None,+ })+ .collect()+ }++ /// Poll a condition every 1ms up to 1s. For tests that need to observe+ /// a specific in-flight state of the flusher (e.g. "drain has happened+ /// and Redis is awaiting"). Sleeping a fixed wall-clock duration races+ /// scheduler latency on slow CI; polling on the deterministic signal+ /// is what `test_shutdown_timeout_credits_residual_to_in_flight`+ /// already does, factored out here so other race-window tests share+ /// the same pattern.+ async fn wait_until(mut cond: impl FnMut() -> bool) {+ for _ in 0..1000 {+ if cond() {+ return;+ }+ tokio::time::sleep(Duration::from_millis(1)).await;+ }+ panic!("wait_until: condition not satisfied within 1s");+ }++ /// Record `n` Decide requests for teams `1..=n` with the PosthogJs library.+ /// Each call generates one team-level + one library-level entry under the+ /// same bucket. Used by the flush-policy tests to set up a known shape.+ fn record_n_decide_with_library(agg: &Arc<BillingAggregator>, n: i32) {+ for team_id in 1..=n {+ agg.record(team_id, FlagRequestType::Decide, Some(Library::PosthogJs));+ }+ }++ #[rstest]+ // Repeated record on the same key collapses into one entry with the summed count.+ #[case::same_key_aggregates(+ &[(1, Some(Library::PosthogJs)), (1, Some(Library::PosthogJs)), (1, Some(Library::PosthogJs))],+ &[((1, Some(Library::PosthogJs)), 3)],+ )]+ // Same team but distinct libraries (including no library) split into separate keys.+ #[case::distinct_libraries_split(+ &[(1, Some(Library::PosthogJs)), (1, Some(Library::PosthogNode)), (1, None)],+ &[+ ((1, Some(Library::PosthogJs)), 1),+ ((1, Some(Library::PosthogNode)), 1),+ ((1, None), 1),+ ],+ )]+ fn test_record_aggregation(+ #[case] records: &[(i32, Option<Library>)],+ #[case] expected_counts: &[((i32, Option<Library>), u64)],+ ) {+ let (_, agg) = new_test_aggregator(test_config());++ for &(team_id, library) in records {+ agg.record(team_id, FlagRequestType::Decide, library);+ }++ assert_eq!(+ agg.pending_len(),+ expected_counts.len(),+ "pending_len must match number of expected distinct keys",+ );+ let bucket = current_bucket();+ let pending = agg.inner.pending.lock().unwrap();+ for &((team_id, library), expected) in expected_counts {+ let key = AggregationKey {+ team_id,+ request_type: FlagRequestType::Decide,+ library,+ bucket,+ };+ assert_eq!(+ pending.get(&key).copied(),+ Some(expected),+ "team {team_id} library {library:?} count mismatch",+ );+ }+ }++ /// `pending_total` is maintained alongside `pending` so the metrics+ /// sampler can read it lock-free. If `record()`, the flush drain, and+ /// the requeue path don't keep them in step, the `pending_counts` gauge+ /// drifts away from reality. This test pins the invariant+ /// `pending_total == sum(pending.values())` after each kind of+ /// transition.+ #[tokio::test]+ async fn test_pending_total_matches_pending_values_sum() {+ let (_, agg) = new_test_aggregator(test_config());++ let assert_consistent = |agg: &Arc<BillingAggregator>, label: &str| {+ let pending = agg.inner.pending.lock().unwrap();+ let walked: u64 = pending.values().sum();+ let atomic = agg.pending_total();+ assert_eq!(+ walked, atomic,+ "{label}: pending_total ({atomic}) drifted from pending.values().sum() ({walked})"+ );+ };++ // After records on distinct keys.+ agg.record(1, FlagRequestType::Decide, None);+ agg.record(2, FlagRequestType::Decide, None);+ assert_consistent(&agg, "after distinct-key records");++ // After repeated records on the same key.+ agg.record(1, FlagRequestType::Decide, None);+ agg.record(1, FlagRequestType::Decide, None);+ assert_consistent(&agg, "after repeated-key records");++ // After a successful flush.+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ assert_consistent(&agg, "after successful flush");+ assert_eq!(agg.pending_total(), 0, "pending_total must zero on drain");++ // After records following a flush.+ agg.record(3, FlagRequestType::FlagDefinitions, None);+ agg.record(3, FlagRequestType::FlagDefinitions, None);+ assert_consistent(&agg, "after post-flush records");+ }++ /// `pending_total` must absorb requeued counts when a flush bails out.+ /// Without this, a Redis outage would drain pending to "empty" while+ /// records actually pile back into `pending` from the requeue path.+ #[tokio::test]+ async fn test_pending_total_absorbs_requeued_counts_on_bail() {+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout); // every flush attempt fails+ let redis: Arc<dyn RedisClient + Send + Sync> = Arc::new(mock);+ let agg = BillingAggregator::for_tests(redis, test_config());++ // Three records on distinct keys (so they don't collapse).+ agg.record(1, FlagRequestType::Decide, None);+ agg.record(2, FlagRequestType::Decide, None);+ agg.record(3, FlagRequestType::Decide, None);+ assert_eq!(agg.pending_total(), 3);++ // BailOnError flush against an erroring mock: the failing chunk+ // and unattempted remainder both requeue. After flush, pending+ // should hold the same 3 records and pending_total should match.+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let pending = agg.inner.pending.lock().unwrap();+ let walked: u64 = pending.values().sum();+ drop(pending);+ assert_eq!(+ walked,+ agg.pending_total(),+ "pending_total drifted from pending after bail+requeue"+ );+ assert_eq!(agg.pending_total(), 3, "all three records must requeue");+ }++ #[tokio::test]+ async fn test_flush_once_writes_pipelined_hincrby() {+ let (redis, agg) = new_test_aggregator(test_config());++ agg.record(42, FlagRequestType::Decide, Some(Library::PosthogJs));+ agg.record(42, FlagRequestType::Decide, Some(Library::PosthogJs));+ agg.record(7, FlagRequestType::Decide, None);++ let bucket = current_bucket();+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let calls = hincrby_calls(&redis);+ // 3 HIncrBys: team-key for team 42, sdk-key for team 42, team-key for+ // team 7. Team 7 has no library, so no sdk-key.+ assert_eq!(calls.len(), 3);++ let expected_team_42_team = format!(+ "{}:{bucket}",+ get_team_request_shadow_key(42, FlagRequestType::Decide)+ );+ let expected_team_42_sdk = format!(+ "{}:{bucket}",+ get_team_request_library_shadow_key(42, FlagRequestType::Decide, Library::PosthogJs)+ );+ let expected_team_7_team = format!(+ "{}:{bucket}",+ get_team_request_shadow_key(7, FlagRequestType::Decide)+ );++ assert!(+ calls.contains(&(expected_team_42_team.clone(), 2)),+ "expected team 42 team key with count 2, got {:?}",+ calls+ );+ assert!(+ calls.contains(&(expected_team_42_sdk.clone(), 2)),+ "expected team 42 sdk key with count 2, got {:?}",+ calls+ );+ assert!(+ calls.contains(&(expected_team_7_team.clone(), 1)),+ "expected team 7 team key with count 1, got {:?}",+ calls+ );+ }++ #[tokio::test]+ async fn test_flush_once_writes_flag_definitions_keys() {+ let (redis, agg) = new_test_aggregator(test_config());++ agg.record(+ 42,+ FlagRequestType::FlagDefinitions,+ Some(Library::PosthogJs),+ );++ let bucket = current_bucket();+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let calls = hincrby_calls(&redis);+ let expected_team = format!("posthog:local_evaluation_requests:42:shadow:{bucket}");+ let expected_sdk =+ format!("posthog:local_evaluation_requests:sdk:42:posthog-js:shadow:{bucket}");+ assert_eq!(calls.len(), 2);+ assert!(calls.contains(&(expected_team, 1)));+ assert!(calls.contains(&(expected_sdk, 1)));+ }++ #[tokio::test]+ async fn test_flush_once_drains_pending() {+ let (_, agg) = new_test_aggregator(test_config());++ agg.record(1, FlagRequestType::Decide, None);+ assert_eq!(agg.pending_len(), 1);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ assert_eq!(agg.pending_len(), 0);+ }++ #[tokio::test]+ async fn test_flush_chunks_commands_at_batch_size() {+ // per_flush_batch_size=2, 5 keys × 2 commands each = 10 commands → 5 chunks.+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (redis, agg) = new_test_aggregator(config);++ record_n_decide_with_library(&agg, 5);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let hincrby_count = hincrby_calls(&redis).len();+ assert_eq!(+ hincrby_count, 10,+ "all commands should be flushed across chunks"+ );+ }++ #[rstest]+ // At cap, a new key (team 3) is dropped — the original two entries survive+ // unchanged, and team 3 never appears in pending.+ #[case::new_key_dropped_at_cap(3, &[(1, 1), (2, 1)])]+ // At cap, an existing key (team 1) still increments — pending_len is+ // unchanged but the count for team 1 grows.+ #[case::existing_key_increments_at_cap(1, &[(1, 2), (2, 1)])]+ fn test_cap_third_record(#[case] third_team: i32, #[case] expected_counts: &[(i32, u64)]) {+ let config = BillingAggregatorConfig {+ max_pending_entries: 2,+ ..test_config()+ };+ let (_, agg) = new_test_aggregator(config);++ agg.record(1, FlagRequestType::Decide, None);+ agg.record(2, FlagRequestType::Decide, None);+ assert_eq!(agg.pending_len(), 2);+ agg.record(third_team, FlagRequestType::Decide, None);++ // Length-equals-expected implicitly verifies that no extra team+ // (e.g. a capped-out team 3) snuck in.+ assert_eq!(+ agg.pending_len(),+ expected_counts.len(),+ "cap must hold pending_len at max_pending_entries",+ );+ let bucket = current_bucket();+ let pending = agg.inner.pending.lock().unwrap();+ for &(team_id, expected) in expected_counts {+ let key = AggregationKey {+ team_id,+ request_type: FlagRequestType::Decide,+ library: None,+ bucket,+ };+ assert_eq!(+ pending.get(&key).copied(),+ Some(expected),+ "team {team_id} count mismatch",+ );+ }+ }++ #[tokio::test]+ async fn test_shutdown_flushes_then_stops() {+ let redis = Arc::new(MockRedisClient::new());+ let agg = BillingAggregator::start(+ redis.clone(),+ BillingAggregatorConfig {+ // Slow tick so shutdown has to drive the flush itself.+ flush_interval: Duration::from_secs(60),+ ..test_config()+ },+ );++ agg.record(1, FlagRequestType::Decide, None);+ agg.record(1, FlagRequestType::Decide, None);++ let bucket = current_bucket();+ agg.shutdown().await;++ let calls = hincrby_calls(&redis);+ assert_eq!(calls.len(), 1, "shutdown should trigger one HIncrBy");+ let expected_key = format!(+ "{}:{bucket}",+ get_team_request_shadow_key(1, FlagRequestType::Decide)+ );+ assert_eq!(calls[0], (expected_key, 2));+ }++ #[tokio::test]+ async fn test_shutdown_is_idempotent_sequentially() {+ let redis = Arc::new(MockRedisClient::new());+ let agg = BillingAggregator::start(+ redis.clone(),+ BillingAggregatorConfig {+ flush_interval: Duration::from_secs(60),+ ..test_config()+ },+ );+ agg.record(1, FlagRequestType::Decide, None);++ // First call performs the flush. Second call must not panic and+ // must not double-flush.+ agg.shutdown().await;+ let calls_after_first = hincrby_calls(&redis).len();+ agg.shutdown().await;+ let calls_after_second = hincrby_calls(&redis).len();+ assert_eq!(calls_after_first, calls_after_second);+ assert_eq!(calls_after_first, 1);+ }++ #[tokio::test]+ async fn test_shutdown_timeout_does_not_hang() {+ // Redis pipeline blocks longer than `shutdown_flush_timeout`. Shutdown+ // must return within roughly the timeout, not the block duration.+ let mut mock = MockRedisClient::new();+ mock.pipeline_block(Duration::from_secs(5));+ let redis = Arc::new(mock);+ let agg = BillingAggregator::start(+ redis.clone(),+ BillingAggregatorConfig {+ flush_interval: Duration::from_secs(60),+ shutdown_flush_timeout: Duration::from_millis(100),+ ..test_config()+ },+ );+ agg.record(1, FlagRequestType::Decide, None);++ let start = std::time::Instant::now();+ agg.shutdown().await;+ let elapsed = start.elapsed();++ assert!(+ elapsed < Duration::from_secs(2),+ "shutdown should honour timeout, took {:?}",+ elapsed+ );+ }++ #[tokio::test]+ async fn test_flush_bail_mode_breaks_on_first_chunk_error() {+ // 5 keys with libraries = 10 commands. batch_size=2 → 5 chunks.+ // With an always-fail mock, the first chunk must be attempted and the+ // rest must NOT be attempted (bail-on-first-error — used by the+ // flusher's normal tick path).+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (redis, agg) = new_test_aggregator_with_redis(mock, config);++ record_n_decide_with_library(&agg, 5);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ // Mock records commands before returning the connection error, so+ // exactly the failing first chunk's 2 commands appear in calls.+ let attempted = hincrby_calls(&redis).len();+ assert_eq!(+ attempted, 2,+ "only the first failing chunk should be attempted"+ );+ }++ #[tokio::test]+ async fn test_flush_bail_mode_requeues_failed_and_unattempted_entries() {+ // 5 keys × 2 cmds = 10 cmds at batch_size=2 → 5 chunks. Always-fail+ // mock: first chunk fails, remaining chunks are unattempted. Under+ // BailOnError, every drained entry must be re-queued into `pending`+ // so a later tick can retry — nothing should be silently dropped.+ //+ // Each team gets a distinct count (team T → T records) so the+ // assertion checks per-key identity, not just totals. A bug that+ // requeued the wrong 5 entries (e.g., duplicates of one chunk+ // rather than the actual leftover iterator) would otherwise pass.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (_, agg) = new_test_aggregator_with_redis(mock, config);++ for team_id in 1..=5 {+ for _ in 0..team_id {+ agg.record(team_id, FlagRequestType::Decide, Some(Library::PosthogJs));+ }+ }+ assert_eq!(agg.pending_len(), 5);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ assert_eq!(+ agg.pending_len(),+ 5,+ "all failed+unattempted entries must be re-queued"+ );+ let pending = agg.inner.pending.lock().unwrap();+ let total: u64 = pending.values().sum();+ assert_eq!(+ total,+ 1 + 2 + 3 + 4 + 5,+ "requeued counts must be preserved"+ );+ for (key, &count) in pending.iter() {+ assert_eq!(+ count, key.team_id as u64,+ "team {} requeued with wrong count {count}",+ key.team_id,+ );+ }+ // in-flight is cleared — requeued counts live in `pending`, not in+ // `in_flight_uncredited`, so shutdown drops can't double-count them.+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_flush_bail_mode_rebuckets_requeued_entries_to_current_bucket() {+ // Multiple stale-bucket entries for the same (team, request_type,+ // library) collapse into a single current-bucket entry on requeue.+ // Without rebucket-on-requeue, a sustained Redis outage would+ // accumulate one new key per CACHE_BUCKET_SIZE rollover, blowing+ // through `max_pending_entries` purely from the wall clock advancing.+ // After requeue, the count is conserved (3 + 5 + 7 = 15) and+ // cardinality is bounded by (team × request_type × library).+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let (_, agg) = new_test_aggregator_with_redis(mock, test_config());++ // Seed three distinct old buckets for the same tuple. Pick buckets+ // far below `current_bucket()` so the test doesn't race a real+ // bucket boundary.+ let old_buckets = [10u64, 11, 12];+ let counts = [3u64, 5, 7];+ agg.seed_pending(old_buckets.iter().zip(counts.iter()).map(|(&b, &c)| {+ (+ AggregationKey {+ team_id: 1,+ request_type: FlagRequestType::Decide,+ library: Some(Library::PosthogJs),+ bucket: b,+ },+ c,+ )+ }));+ assert_eq!(agg.pending_len(), 3, "seeded three distinct-bucket keys");++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let pending = agg.inner.pending.lock().unwrap();+ assert_eq!(+ pending.len(),+ 1,+ "rebucket must collapse stale-bucket entries for the same tuple into one"+ );+ let (key, &count) = pending.iter().next().unwrap();+ assert_eq!(count, 3 + 5 + 7, "counts must be conserved across rebucket");+ assert_eq!(+ key.bucket,+ current_bucket(),+ "rebucketed key must carry the current bucket, not any stale bucket"+ );+ assert!(+ !old_buckets.contains(&key.bucket),+ "rebucketed key must not retain any stale bucket value"+ );+ }++ #[tokio::test]+ async fn test_flush_bail_mode_requeue_merges_with_concurrent_records() {+ // A record() arriving during an in-flight flush lands in the fresh+ // `pending` map the flusher just drained from. If that flush then+ // errors and requeues, the requeued entries must be *merged* into+ // the fresh map (not overwrite concurrent records, not lose them).+ let mut mock = MockRedisClient::new();+ mock.pipeline_block(Duration::from_millis(50));+ mock.pipeline_error(CustomRedisError::Timeout);+ let (_, agg) = new_test_aggregator_with_redis(mock, test_config());++ // One pre-flush record for team 1 — will be drained and requeued.+ agg.record(1, FlagRequestType::Decide, None);++ let agg_for_flush = agg.clone();+ let flush = tokio::spawn(async move {+ flush_once(&agg_for_flush.inner, FlushPolicy::BailOnError).await;+ });++ // Race a concurrent record for team 1 into the post-drain map+ // while the flusher is parked on the Redis error. Poll on+ // `in_flight_uncredited > 0` — the flusher writes that *before*+ // its first await, so observing it non-zero proves pending was+ // drained and the flusher is now parked on Redis. Sleeping a+ // fixed duration would race scheduler latency on slow CI.+ wait_until(|| agg.in_flight_uncredited() > 0).await;+ agg.record(1, FlagRequestType::Decide, None);+ flush.await.unwrap();++ // Both counts survive: the requeued pre-flush record + the racing+ // record merged under the same AggregationKey = 2.+ let pending = agg.inner.pending.lock().unwrap();+ let total: u64 = pending.values().sum();+ assert_eq!(total, 2, "requeue must merge, not overwrite or drop");+ }++ #[tokio::test]+ async fn test_flush_bail_mode_recovery_flushes_requeued() {+ // Fail only the first pipeline call. The first flush requeues its+ // drained entries; the second flush should find them in `pending`+ // and land them in Redis.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(0, CustomRedisError::Timeout);+ let (redis, agg) = new_test_aggregator_with_redis(mock, test_config());++ agg.record(1, FlagRequestType::Decide, None);+ agg.record(2, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ // First flush errored; entries should be waiting in `pending`.+ assert_eq!(agg.pending_len(), 2, "first flush should requeue on error");+ let calls_after_first = hincrby_calls(&redis);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ assert_eq!(agg.pending_len(), 0, "second flush should drain pending");++ // The mock records every attempted command even on errors, so we+ // must look at the calls added by the second flush only. That's+ // what actually reached Redis.+ let all_calls = hincrby_calls(&redis);+ let second_flush_calls: Vec<_> = all_calls[calls_after_first.len()..].to_vec();+ let total: i64 = second_flush_calls.iter().map(|(_, v)| *v).sum();+ assert_eq!(+ total, 2,+ "total recorded requests must reach Redis after recovery, got {second_flush_calls:?}"+ );+ }++ #[tokio::test]+ async fn test_flush_bail_mode_middle_chunk_failure_credits_successful_and_requeues_rest() {+ // 3 keys × 2 cmds = 6 cmds at batch_size=2 → 3 chunks. Fail only+ // chunk 2 (call index 1). Chunk 1's HINCRBYs landed in Redis and+ // its counts must NOT be requeued (would double-credit next tick).+ // Chunks 2 and 3+ go into `requeue` under BailOnError.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(1, CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (_, agg) = new_test_aggregator_with_redis(mock, config);++ record_n_decide_with_library(&agg, 3);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ // Chunk 1 succeeded; its key is gone from `pending`. Chunks 2 & 3+ // are requeued = 2 keys. A regression that also requeued chunk 1's+ // already-credited entries would land 3 here.+ assert_eq!(+ agg.pending_len(),+ 2,+ "successful chunk's entries must NOT be requeued",+ );+ let pending_total: u64 = agg.inner.pending.lock().unwrap().values().sum();+ assert_eq!(pending_total, 2);+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_flush_best_effort_mode_attempts_all_chunks_despite_errors() {+ // Same setup as the bail-mode test but with FlushPolicy::BestEffort.+ // Every chunk fails, yet every chunk must still be attempted —+ // shutdown's last-chance semantics.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (redis, agg) = new_test_aggregator_with_redis(mock, config);++ record_n_decide_with_library(&agg, 5);++ flush_once(&agg.inner, FlushPolicy::BestEffort).await;++ let attempted = hincrby_calls(&redis).len();+ assert_eq!(+ attempted, 10,+ "all chunks should be attempted in best-effort mode"+ );+ // Accounting closed out: in-flight cleared.+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_flush_best_effort_credits_successful_chunks_around_error() {+ // Fail only the second pipeline call. Surrounding chunks should land+ // and credit their counts (exercises the partial-success path that's+ // the whole point of best-effort mode).+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(1, CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (redis, agg) = new_test_aggregator_with_redis(mock, config);++ record_n_decide_with_library(&agg, 5);++ flush_once(&agg.inner, FlushPolicy::BestEffort).await;++ // All 5 chunks were attempted (the mock records commands even on+ // injected errors), proving we continued past the failing chunk.+ let attempted = hincrby_calls(&redis).len();+ assert_eq!(attempted, 10);+ // Accounting closed out.+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_flush_best_effort_flushes_trailing_partial_after_error() {+ // Best-effort mode attempts the trailing partial chunk even if earlier+ // chunks errored. 3 keys w/ libraries (6 cmds) + 1 key w/o library+ // (1 cmd) = 7 cmds at batch_size=3 → 2 full chunks + 1 trailing+ // 1-cmd chunk. Fail only the first pipeline call; assert the+ // trailing partial still got attempted.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(0, CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 3,+ ..test_config()+ };+ let (redis, agg) = new_test_aggregator_with_redis(mock, config);++ for team_id in 1..=3 {+ agg.record(team_id, FlagRequestType::Decide, Some(Library::PosthogJs));+ }+ agg.record(99, FlagRequestType::Decide, None);++ flush_once(&agg.inner, FlushPolicy::BestEffort).await;++ // All 7 commands attempted despite the first chunk's error — proves+ // both that we continued past the error AND that the trailing partial+ // still ran.+ let attempted = hincrby_calls(&redis).len();+ assert_eq!(attempted, 7);+ }++ #[tokio::test]+ async fn test_shutdown_uses_best_effort_flush() {+ // When shutdown drives the flush, all chunks must be attempted even+ // if earlier ones error. With bail-on-error, only chunk 1 would land.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let redis = Arc::new(mock);+ let agg = BillingAggregator::start(+ redis.clone(),+ BillingAggregatorConfig {+ // Long interval so shutdown drives the flush, not a tick.+ flush_interval: Duration::from_secs(60),+ per_flush_batch_size: 2,+ ..test_config()+ },+ );++ record_n_decide_with_library(&agg, 5);++ agg.shutdown().await;++ let attempted = hincrby_calls(&redis).len();+ assert_eq!(+ attempted, 10,+ "shutdown should attempt all chunks in best-effort mode"+ );+ }++ #[tokio::test]+ async fn test_flush_clears_in_flight_uncredited_on_success() {+ let (_, agg) = new_test_aggregator(test_config());+ agg.record(1, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_flush_clears_in_flight_uncredited_on_error() {+ let mut mock = MockRedisClient::new();+ mock.pipeline_error(CustomRedisError::Timeout);+ let (_, agg) = new_test_aggregator_with_redis(mock, test_config());+ agg.record(1, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ // On completed (even errored) flush, in-flight must be cleared so a+ // later shutdown drop doesn't double-count this batch.+ assert_eq!(agg.inner.in_flight_uncredited.load(Ordering::Relaxed), 0);+ }++ #[tokio::test]+ async fn test_empty_flush_does_no_work() {+ let (redis, agg) = new_test_aggregator(test_config());++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let calls = redis.get_calls();+ assert!(+ calls.is_empty(),+ "flushing an empty map should make zero calls"+ );+ }++ #[tokio::test]+ async fn test_flush_preserves_distinct_buckets_per_key() {+ // Two records for the same (team, request_type, library) but in+ // different buckets must produce two separate HINCRBYs (distinct+ // `field` values), preserving late-flushed records in their original+ // bucket. Inject the keys directly so the test isn't dependent on+ // wall-clock crossing a bucket boundary.+ let (redis, agg) = new_test_aggregator(test_config());++ agg.seed_pending([+ (+ AggregationKey {+ team_id: 1,+ request_type: FlagRequestType::Decide,+ library: None,+ bucket: 100,+ },+ 3,+ ),+ (+ AggregationKey {+ team_id: 1,+ request_type: FlagRequestType::Decide,+ library: None,+ bucket: 101,+ },+ 5,+ ),+ ]);++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ // The mock records calls as "{key}:{field}", so a per-bucket HINCRBY+ // shows up under "{team_key}:{bucket}".+ let team_key = get_team_request_shadow_key(1, FlagRequestType::Decide);+ let calls = hincrby_calls(&redis);+ let bucket_100_key = format!("{team_key}:100");+ let bucket_101_key = format!("{team_key}:101");+ assert!(+ calls.iter().any(|(k, v)| k == &bucket_100_key && *v == 3),+ "bucket 100 must produce HINCRBY count=3, got {:?}",+ calls+ );+ assert!(+ calls.iter().any(|(k, v)| k == &bucket_101_key && *v == 5),+ "bucket 101 must produce HINCRBY count=5, got {:?}",+ calls+ );+ }++ #[tokio::test]+ async fn test_flush_conserves_total_count() {+ // Conservation invariant: the sum of HINCRBY values written to Redis+ // equals the total number of recorded requests. Catches drift in+ // the per-chunk accumulators and the in_flight_uncredited bookkeeping.+ let (redis, agg) = new_test_aggregator(test_config());++ let mut expected_total: i64 = 0;+ for team_id in 1..=10 {+ for _ in 0..(team_id as usize) {+ agg.record(team_id, FlagRequestType::Decide, None);+ expected_total += 1;+ }+ }++ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ // No library = one HINCRBY per key, so summing values gives the total.+ let actual_total: i64 = hincrby_calls(&redis).iter().map(|(_, v)| *v).sum();+ assert_eq!(+ actual_total, expected_total,+ "sum of HINCRBYs must equal recorded request count"+ );+ assert_eq!(agg.pending_len(), 0, "pending must be drained on success");+ assert_eq!(+ agg.in_flight_uncredited(),+ 0,+ "in-flight must be cleared on success"+ );+ }++ #[tokio::test]+ async fn test_shutdown_timeout_credits_residual_to_in_flight() {+ // When the flush hangs and the shutdown timeout fires, the recorded+ // count must be fully accounted for: pending is drained into the+ // flush-local map (so pending_len == 0), and the count is parked in+ // `in_flight_uncredited` so record_shutdown_drops can credit it as a+ // drop. This is the invariant that+ // `flags_billing_unflushed_requests_total{cause="shutdown_drop"}` depends on.+ let mut mock = MockRedisClient::new();+ mock.pipeline_block(Duration::from_secs(60)); // outlasts the timeout+ let redis: Arc<dyn RedisClient + Send + Sync> = Arc::new(mock);+ let config = BillingAggregatorConfig {+ flush_interval: Duration::from_millis(20),+ shutdown_flush_timeout: Duration::from_millis(50),+ ..test_config()+ };+ let agg = BillingAggregator::start(redis, config);++ for _ in 0..7 {+ agg.record(1, FlagRequestType::Decide, None);+ }+ // Wait for the flusher to drain `pending` and park the count in+ // `in_flight_uncredited` (which `flush_once` writes before its first+ // `.await`). Polling on `in_flight_uncredited` rather than sleeping+ // a fixed duration keeps the test deterministic on slow CI.+ for _ in 0..200 {+ if agg.in_flight_uncredited() == 7 {+ break;+ }+ tokio::time::sleep(Duration::from_millis(5)).await;+ }+ assert_eq!(+ agg.pending_len(),+ 0,+ "pending must be drained into the in-flight flush before shutdown"+ );+ assert_eq!(+ agg.in_flight_uncredited(),+ 7,+ "drained count must be parked in in_flight_uncredited so the timeout path can credit it as a drop"+ );++ agg.shutdown().await;++ // After shutdown returns, all bookkeeping must close out: the+ // residual count was credited via `record_shutdown_drops`, and both+ // accumulators are zero. Without this assertion, a regression that+ // skipped `record_shutdown_drops` on the timeout path would still+ // pass — the residual would silently leak with no signal.+ assert_eq!(+ agg.pending_len(),+ 0,+ "pending must remain empty after shutdown"+ );+ assert_eq!(+ agg.pending_total(),+ 0,+ "pending_total must remain zero after shutdown"+ );+ assert_eq!(+ agg.in_flight_uncredited(),+ 7,+ "in_flight_uncredited stays at the residual after shutdown — \+ record_shutdown_drops reads it but does not zero it. Any future \+ refactor that resets this on shutdown must also retain a way to \+ prove the drops were emitted; otherwise this test pin breaks."+ );+ }++ #[tokio::test]+ async fn test_record_during_in_flight_flush_lands_in_next_batch() {+ // The flusher releases the `pending` lock right after `mem::take`, so+ // a `record()` arriving while Redis is still in flight must land in+ // a fresh empty map and wait for the next flush. Regression guard+ // for any future change that inadvertently holds the lock across+ // `.await` — that would block `record()` for the duration of the+ // Redis round-trip on the request hot path.+ let mut mock = MockRedisClient::new();+ mock.pipeline_block(Duration::from_millis(100));+ let (redis, agg) = new_test_aggregator_with_redis(mock, test_config());++ agg.record(1, FlagRequestType::Decide, None);++ // Spawn the flush so we can race a `record()` against the in-flight+ // pipeline. Use a separate `Arc` clone for the task.+ let agg_for_flush = agg.clone();+ let flush = tokio::spawn(async move {+ flush_once(&agg_for_flush.inner, FlushPolicy::BailOnError).await;+ });++ // Wait until the flusher has drained `pending` and is parked on the+ // Redis await — `in_flight_uncredited` is set just before the first+ // `.await`, so observing it non-zero is the deterministic proof.+ // Polling beats sleeping a fixed duration: the latter would race+ // scheduler latency on slow CI.+ wait_until(|| agg.in_flight_uncredited() > 0).await;+ // The hot-path contract is that `record()` returns in microseconds+ // even while a flush is in flight. If a future change holds the+ // `pending` lock across the await, this call would block for the+ // remainder of the 100ms `pipeline_block` window. 25ms is generous+ // for a HashMap insert (~hundreds of ns) and tight enough to fail+ // hard if the lock is held.+ let record_start = std::time::Instant::now();+ agg.record(2, FlagRequestType::Decide, None);+ let record_elapsed = record_start.elapsed();+ assert!(+ record_elapsed < Duration::from_millis(25),+ "record() blocked for {record_elapsed:?} while a flush was in flight — \+ a regression that holds the pending lock across .await would block \+ the hot path for the duration of the Redis round-trip"+ );++ flush.await.unwrap();++ assert_eq!(+ agg.pending_len(),+ 1,+ "record(2) must land in the post-drain map and wait for next flush"+ );+ let calls = hincrby_calls(&redis);+ assert_eq!(+ calls.len(),+ 1,+ "only the in-flight chunk (record(1)) should have flushed, got {calls:?}"+ );+ assert_eq!(calls[0].1, 1, "the flushed HINCRBY count must be 1");+ }++ #[tokio::test]+ async fn test_empty_drain_stamps_last_successful_epoch() {+ // An idle pod runs the flusher tick with nothing to drain. That is+ // still evidence the loop is alive, so the staleness gauge must+ // reset — otherwise a low-traffic pod that flushed at boot would+ // show monotonically rising `seconds_since_successful_flush` and+ // trip the wedged-flusher alarm despite being healthy.+ let (_, agg) = new_test_aggregator(test_config());++ let before = now_epoch_ms();+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ let after = now_epoch_ms();++ let stamped = agg+ .inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed);+ assert!(+ (before..=after).contains(&stamped),+ "empty drain stamp {stamped} must be in [{before}, {after}]",+ );+ }++ #[tokio::test]+ async fn test_successful_flush_stamps_last_successful_epoch() {+ let (_, agg) = new_test_aggregator(test_config());+ assert_eq!(+ agg.inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed),+ 0,+ "no flush yet → epoch stays zero"+ );++ let before = now_epoch_ms();+ agg.record(1, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ let after = now_epoch_ms();++ let stamped = agg+ .inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed);+ // `stamped > 0` would be true on any clock past 1970 even if the+ // code stored a stale or constant value. Bracketing pins the stamp+ // to the actual flush moment.+ assert!(+ (before..=after).contains(&stamped),+ "stamp {stamped} must be in [{before}, {after}]",+ );+ }++ #[tokio::test]+ async fn test_failed_flush_does_not_overwrite_prior_successful_epoch() {+ // Prime with a successful flush so the epoch holds a known+ // non-zero value, then run a failing flush. The failure must+ // leave the prior stamp intact (not zero it, not overwrite it).+ // A test that only verifies "still 0" can't distinguish "field+ // never written" from "field correctly preserved."+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(1, CustomRedisError::Timeout);+ let (_, agg) = new_test_aggregator_with_redis(mock, test_config());++ agg.record(1, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;+ let primed = agg+ .inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed);+ assert!(primed > 0, "priming flush must stamp the epoch");++ agg.record(2, FlagRequestType::Decide, None);+ flush_once(&agg.inner, FlushPolicy::BailOnError).await;++ let after = agg+ .inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed);+ assert_eq!(+ after, primed,+ "failed flush must not disturb the prior success stamp",+ );+ }++ #[tokio::test]+ async fn test_best_effort_partial_success_does_not_stamp_epoch() {+ // A BestEffort (shutdown) flush that succeeds on some chunks but+ // errors on others is not a "successful flush" — dropped_counts > 0+ // and the staleness gauge should keep ticking.+ let mut mock = MockRedisClient::new();+ mock.pipeline_error_at_call(1, CustomRedisError::Timeout);+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 2,+ ..test_config()+ };+ let (_, agg) = new_test_aggregator_with_redis(mock, config);++ record_n_decide_with_library(&agg, 5);+ flush_once(&agg.inner, FlushPolicy::BestEffort).await;++ assert_eq!(+ agg.inner+ .last_successful_flush_epoch_ms+ .load(Ordering::Relaxed),+ 0,+ "partial-success flush must not stamp the epoch"+ );+ }++ #[test]+ fn test_sample_metrics_reads_live_pending_and_in_flight() {+ // Smoke test: sample_metrics doesn't panic on any inner state+ // (zero epoch, non-empty pending, live in-flight). The staleness+ // math is asserted directly via `compute_staleness_seconds` below.+ let (_, agg) = new_test_aggregator(test_config());+ agg.record(1, FlagRequestType::Decide, None);+ agg.record(2, FlagRequestType::Decide, None);+ agg.inner.in_flight_uncredited.store(42, Ordering::Relaxed);++ sample_metrics(&agg.inner);++ agg.inner+ .last_successful_flush_epoch_ms+ .store(now_epoch_ms(), Ordering::Relaxed);+ sample_metrics(&agg.inner);+ }++ #[test]+ fn test_staleness_is_zero_before_first_flush() {+ // A freshly booted pod with no traffic must not trip the+ // stale-flush alarm. `last_ms == 0` is the sentinel for "no+ // successful flush yet" and must collapse to 0 seconds, not+ // "decades since 1970".+ assert_eq!(compute_staleness_seconds(0, 1_700_000_000_000), 0.0);+ assert_eq!(compute_staleness_seconds(0, 0), 0.0);+ }++ #[test]+ fn test_staleness_reports_seconds_since_last_flush() {+ assert_eq!(+ compute_staleness_seconds(1_700_000_000_000, 1_700_000_005_500),+ 5.5,+ );+ assert_eq!(+ compute_staleness_seconds(1_700_000_000_000, 1_700_000_000_001),+ 0.001,+ );+ }++ #[test]+ fn test_staleness_saturates_on_backward_clock_skew() {+ // Backward NTP step: now < last. Saturating subtract clamps to 0+ // rather than wrapping into a huge positive value that would+ // misfire the staleness alarm.+ assert_eq!(+ compute_staleness_seconds(1_700_000_005_000, 1_700_000_000_000),+ 0.0,+ );+ }++ #[test]+ fn test_config_validate_rejects_zero_flush_interval() {+ let config = BillingAggregatorConfig {+ flush_interval: Duration::ZERO,+ ..test_config()+ };+ let err = config.validate().unwrap_err();+ assert!(+ err.contains("flush_interval"),+ "error should name the bad knob, got: {err}"+ );+ }++ #[test]+ fn test_config_validate_rejects_zero_max_pending_entries() {+ let config = BillingAggregatorConfig {+ max_pending_entries: 0,+ ..test_config()+ };+ let err = config.validate().unwrap_err();+ assert!(err.contains("max_pending_entries"), "got: {err}");+ }++ #[test]+ fn test_config_validate_rejects_zero_per_flush_batch_size() {+ let config = BillingAggregatorConfig {+ per_flush_batch_size: 0,+ ..test_config()+ };+ let err = config.validate().unwrap_err();+ assert!(err.contains("per_flush_batch_size"), "got: {err}");+ }++ #[test]+ fn test_config_validate_rejects_zero_shutdown_flush_timeout() {+ let config = BillingAggregatorConfig {+ shutdown_flush_timeout: Duration::ZERO,+ ..test_config()+ };+ let err = config.validate().unwrap_err();+ assert!(err.contains("shutdown_flush_timeout"), "got: {err}");+ }++ #[test]+ fn test_config_validate_accepts_default() {+ assert!(BillingAggregatorConfig::default().validate().is_ok());+ }++ #[tokio::test]+ #[should_panic(expected = "invalid BillingAggregatorConfig")]+ async fn test_start_panics_on_invalid_config() {+ let redis = Arc::new(MockRedisClient::new());+ BillingAggregator::start(+ redis,+ BillingAggregatorConfig {+ flush_interval: Duration::ZERO,+ ..test_config()+ },+ );+ }+}diff --git a/rust/feature-flags/src/billing_limiters.rs b/rust/feature-flags/src/billing/limiters.rssimilarity index 100%rename from rust/feature-flags/src/billing_limiters.rsrename to rust/feature-flags/src/billing/limiters.rsdiff --git a/rust/feature-flags/src/billing/mod.rs b/rust/feature-flags/src/billing/mod.rsnew file mode 100644index 000000000000..dd2b7024b0a5--- /dev/null+++ b/rust/feature-flags/src/billing/mod.rs@@ -0,0 +1,14 @@+//! Billing counters for the feature-flags service.+//!+//! - The authoritative billing write is the synchronous per-request+//! [`crate::flags::flag_analytics::increment_request_count`].+//! - [`aggregator`] is a *shadow* writer that targets `…:shadow`-suffixed+//! keys via [`crate::flags::flag_analytics::get_team_request_shadow_key`],+//! for reconciliation against the authoritative path.+//! - [`limiters`] reads the production counters to enforce per-tenant quotas.++pub mod aggregator;+pub mod limiters;++pub use aggregator::{AggregationKey, BillingAggregator, BillingAggregatorConfig};+pub use limiters::{FeatureFlagsLimiter, SessionReplayLimiter};diff --git a/rust/feature-flags/src/config.rs b/rust/feature-flags/src/config.rsindex dfa73732d3e3..565b97934e53 100644--- a/rust/feature-flags/src/config.rs+++ b/rust/feature-flags/src/config.rs@@ -686,6 +686,36 @@ pub struct Config {#[envconfig(from = "SERVICE_MODE", default = "all")]pub service_mode: ServiceMode,++ // Shadow-keyspace writer for reconciliation. Off by default.+ #[envconfig(from = "FLAGS_BILLING_AGGREGATOR_ENABLED", default = "false")]+ pub billing_aggregator_enabled: FlexBool,++ // BillingAggregator tuning knobs. `BillingAggregatorConfig::validate`+ // rejects zero values at boot — see the module docs on+ // `src/billing/aggregator.rs` for the durability trade-offs that hang+ // off these knobs.+ //+ // How often the flusher drains the in-memory map (milliseconds). Must+ // stay well below `CACHE_BUCKET_SIZE` (120s) so bucket rollover doesn't+ // collapse counts. Also directly bounds the worst-case crash-loss+ // window: a SIGKILL or OOM-kill past the shutdown grace period loses+ // up to one interval of records per pod.+ #[envconfig(from = "FLAGS_BILLING_FLUSH_INTERVAL_MS", default = "10000")]+ pub billing_flush_interval_ms: u64,++ // Safety cap on pending entries — tripwire, not a working-set estimate.+ #[envconfig(from = "FLAGS_BILLING_MAX_PENDING_ENTRIES", default = "500000")]+ pub billing_max_pending_entries: usize,++ // Maximum `HIncrBy` commands per pipeline round-trip during a flush.+ #[envconfig(from = "FLAGS_BILLING_PER_FLUSH_BATCH_SIZE", default = "200")]+ pub billing_per_flush_batch_size: usize,++ // Upper bound on the graceful-shutdown flush (milliseconds). Should stay+ // comfortably within the pod's `terminationGracePeriodSeconds`.+ #[envconfig(from = "FLAGS_BILLING_SHUTDOWN_FLUSH_TIMEOUT_MS", default = "15000")]+ pub billing_shutdown_flush_timeout_ms: u64,}/// Thread counts for Tokio (async I/O) and Rayon (CPU-bound parallel evaluation).@@ -896,6 +926,11 @@ impl Config {skip_pg_team_fallback: FlexBool(false),service_mode: ServiceMode::All,auth_token_cache_ttl_seconds: 300,+ billing_aggregator_enabled: FlexBool(false),+ billing_flush_interval_ms: 10_000,+ billing_max_pending_entries: 500_000,+ billing_per_flush_batch_size: 200,+ billing_shutdown_flush_timeout_ms: 15_000,}}diff --git a/rust/feature-flags/src/flags/flag_analytics.rs b/rust/feature-flags/src/flags/flag_analytics.rsindex 581c979cd31a..b5f801bbe356 100644--- a/rust/feature-flags/src/flags/flag_analytics.rs+++ b/rust/feature-flags/src/flags/flag_analytics.rs@@ -13,14 +13,20 @@ pub fn is_billable_flag_key(key: &str) -> bool {&& !key.starts_with(PRODUCT_TOUR_TARGETING_FLAG_PREFIX)}-const CACHE_BUCKET_SIZE: u64 = 60 * 2; // duration in seconds+pub const CACHE_BUCKET_SIZE: u64 = 60 * 2; // duration in seconds++/// Current 2-minute bucket, expressed as `unix_seconds / CACHE_BUCKET_SIZE`.+pub fn current_bucket() -> u64 {+ SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .map(|d| d.as_secs() / CACHE_BUCKET_SIZE)+ .unwrap_or(0)+}pub fn get_team_request_key(team_id: i32, request_type: FlagRequestType) -> String {match request_type {FlagRequestType::Decide => format!("posthog:decide_requests:{team_id}"),- FlagRequestType::FlagDefinitions => {- format!("posthog:local_evaluation_requests:{team_id}")- }+ FlagRequestType::FlagDefinitions => format!("posthog:local_evaluation_requests:{team_id}"),}}@@ -30,15 +36,40 @@ pub fn get_team_request_library_key(library: Library,) -> String {match request_type {- FlagRequestType::Decide => {- format!("posthog:decide_requests:sdk:{team_id}:{library}")- }+ FlagRequestType::Decide => format!("posthog:decide_requests:sdk:{team_id}:{library}"),FlagRequestType::FlagDefinitions => {format!("posthog:local_evaluation_requests:sdk:{team_id}:{library}")}}}+/// Suffix appended to a production key to obtain its shadow-keyspace mirror.+/// The `BillingAggregator` writes to shadow keys so its counts can be+/// reconciled against the authoritative synchronous path without affecting+/// billing.+pub const SHADOW_KEY_SUFFIX: &str = ":shadow";++pub fn get_team_request_shadow_key(team_id: i32, request_type: FlagRequestType) -> String {+ format!(+ "{}{SHADOW_KEY_SUFFIX}",+ get_team_request_key(team_id, request_type)+ )+}++pub fn get_team_request_library_shadow_key(+ team_id: i32,+ request_type: FlagRequestType,+ library: Library,+) -> String {+ format!(+ "{}{SHADOW_KEY_SUFFIX}",+ get_team_request_library_key(team_id, request_type, library)+ )+}++/// Synchronous, per-request HINCRBY into the production billing keyspace.+/// This is the authoritative billing write — see `crate::billing` for the+/// dual-write contract.pub async fn increment_request_count(redis_client: Arc<dyn RedisClient + Send + Sync>,team_id: i32,@@ -46,13 +77,7 @@ pub async fn increment_request_count(request_type: FlagRequestType,library: Option<Library>,) -> Result<(), CustomRedisError> {- let time_bucket = SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs()- / CACHE_BUCKET_SIZE;-- let time_bucket_str = time_bucket.to_string();+ let time_bucket_str = current_bucket().to_string();let key_name = get_team_request_key(team_id, request_type);// Build pipeline commands for a single Redis round-trip@@ -128,6 +153,54 @@ mod tests {);}+ #[test]+ fn test_get_team_request_shadow_key() {+ assert_eq!(+ get_team_request_shadow_key(123, FlagRequestType::Decide),+ "posthog:decide_requests:123:shadow"+ );+ assert_eq!(+ get_team_request_shadow_key(456, FlagRequestType::FlagDefinitions),+ "posthog:local_evaluation_requests:456:shadow"+ );+ }++ #[test]+ fn test_get_team_request_library_shadow_key() {+ assert_eq!(+ get_team_request_library_shadow_key(123, FlagRequestType::Decide, Library::PosthogNode),+ "posthog:decide_requests:sdk:123:posthog-node:shadow"+ );+ assert_eq!(+ get_team_request_library_shadow_key(+ 456,+ FlagRequestType::FlagDefinitions,+ Library::PosthogJs+ ),+ "posthog:local_evaluation_requests:sdk:456:posthog-js:shadow"+ );+ }++ #[test]+ fn test_shadow_keys_never_collide_with_production_keys() {+ // The reconciliation strategy depends on the two keyspaces being+ // disjoint — a shared key would corrupt the production count.+ for team_id in [1, 100_000, i32::MAX] {+ for rt in [FlagRequestType::Decide, FlagRequestType::FlagDefinitions] {+ assert_ne!(+ get_team_request_key(team_id, rt),+ get_team_request_shadow_key(team_id, rt)+ );+ for lib in [Library::PosthogNode, Library::PosthogJs, Library::Other] {+ assert_ne!(+ get_team_request_library_key(team_id, rt, lib),+ get_team_request_library_shadow_key(team_id, rt, lib)+ );+ }+ }+ }+ }+#[tokio::test]async fn test_increment_request_count() {let redis_client = setup_redis_client(None).await;diff --git a/rust/feature-flags/src/flags/flag_request.rs b/rust/feature-flags/src/flags/flag_request.rsindex 215d9a55f5f5..afc86e3dcc5a 100644--- a/rust/feature-flags/src/flags/flag_request.rs+++ b/rust/feature-flags/src/flags/flag_request.rs@@ -30,12 +30,21 @@ where}))}-#[derive(Debug, Clone, Copy)]+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub enum FlagRequestType {Decide,FlagDefinitions,}+impl FlagRequestType {+ pub const fn as_str(&self) -> &'static str {+ match self {+ FlagRequestType::Decide => "decide",+ FlagRequestType::FlagDefinitions => "flag_definitions",+ }+ }+}+#[derive(Default, Debug, Deserialize, Serialize)]pub struct FlagRequest {#[serde(diff --git a/rust/feature-flags/src/handler/billing.rs b/rust/feature-flags/src/handler/billing.rsindex 5dac3a19d3dd..f8e60eb58b22 100644--- a/rust/feature-flags/src/handler/billing.rs+++ b/rust/feature-flags/src/handler/billing.rs@@ -1,16 +1,18 @@use crate::{api::{errors::FlagError, types::FlagsResponse},+ billing::{aggregator::classify_redis_error, BillingAggregator},flags::{flag_analytics::{increment_request_count, is_billable_flag_key},flag_models::FeatureFlagList,flag_request::FlagRequestType,},- metrics::consts::FLAG_BILLING_INCREMENT_TIME,+ metrics::consts::{FLAG_BILLING_INCREMENT_TIME, FLAG_REQUEST_REDIS_ERROR},};use common_metrics::{histogram, inc};-use common_redis::CustomRedisError;+use common_redis::{Client as RedisClient, CustomRedisError};use limiters::redis::ServiceName;use std::collections::HashMap;+use std::sync::Arc;use std::time::Instant;use super::canonical_log::with_canonical_log;@@ -54,13 +56,61 @@ pub async fn check_limits(Ok(None)}-/// Records usage metrics for feature flag requests.+/// Issues the authoritative synchronous billing HINCRBY plus, when present,+/// the shadow-keyspace tee via the aggregator.///-/// Only increments billing counters if there are billable flags present.-/// Survey and product tour targeting flags are not billable.+/// Caller is responsible for the predicate (skip_writes, billable flags,+/// etc.) — by the time this is called the request is known to be billable.///-/// The `library` parameter is passed in to avoid duplicate detection - it should-/// be detected once at the start of request processing and reused.+/// The shadow tee only fires when the synchronous write succeeds. Recording+/// a request the production keyspace did not capture would surface as a+/// false "aggregator over-counted" signal during reconciliation — masking+/// any real over-count bug in the aggregator behind whatever Redis-error+/// noise was happening at the same time. Tying the two writes together+/// keeps the reconciliation invariant a strict equality: if shadow > prod+/// and `flag_request_redis_error` is flat, the aggregator is the cause.+pub async fn record_billing_increment(+ redis: Arc<dyn RedisClient + Send + Sync>,+ aggregator: Option<&Arc<BillingAggregator>>,+ team_id: i32,+ request_type: FlagRequestType,+ library: Library,+) {+ let start = Instant::now();+ let result = increment_request_count(redis, team_id, 1, request_type, Some(library)).await;+ let elapsed_ms = start.elapsed().as_millis() as u64;++ record_billing_increment_timing(&result, elapsed_ms);+ // No-op outside a canonical-log scope (e.g. /flags/definitions), so safe+ // to call from every site.+ with_canonical_log(|log| log.billing_duration_ms = Some(elapsed_ms));++ match result {+ Ok(()) => {+ if let Some(aggregator) = aggregator {+ aggregator.record(team_id, request_type, Some(library));+ }+ }+ Err(e) => {+ // Bounded `error_type` label (e.g. "timeout"/"transport") —+ // never the raw error message. The full `CustomRedisError::Display`+ // can include unbounded transport details that would explode+ // metric cardinality if used as a Prometheus label.+ inc(+ FLAG_REQUEST_REDIS_ERROR,+ &[(+ "error_type".to_string(),+ classify_redis_error(&e).to_string(),+ )],+ 1,+ );+ }+ }+}++/// Records usage metrics for feature flag requests. Survey and product tour+/// targeting flags are not billable. The `library` parameter is detected+/// once at the start of request processing and reused.pub async fn record_usage(context: &RequestContext,filtered_flags: &FeatureFlagList,@@ -70,32 +120,18 @@ pub async fn record_usage(if *context.state.config.skip_writes {return;}-- let has_billable_flags = contains_billable_flags(filtered_flags);-- if has_billable_flags {- let start = Instant::now();- let result = increment_request_count(- context.state.redis_client.clone(),- team_id,- 1,- FlagRequestType::Decide,- Some(library),- )- .await;- let elapsed_ms = start.elapsed().as_millis() as u64;-- record_billing_increment_timing(&result, elapsed_ms);- with_canonical_log(|log| log.billing_duration_ms = Some(elapsed_ms));-- if let Err(e) = result {- inc(- "flag_request_redis_error",- &[("error".to_string(), e.to_string())],- 1,- );- }+ if !contains_billable_flags(filtered_flags) {+ return;}++ record_billing_increment(+ context.state.redis_client.clone(),+ context.state.billing_aggregator.as_ref(),+ team_id,+ FlagRequestType::Decide,+ library,+ )+ .await;}/// Checks if the flag list contains any billable flags.@@ -109,12 +145,6 @@ fn contains_billable_flags(filtered_flags: &FeatureFlagList) -> bool {})}-/// Helper function to determine if usage should be recorded-/// This function is extracted for testing purposes-pub fn should_record_usage(filtered_flags: &FeatureFlagList) -> bool {- contains_billable_flags(filtered_flags)-}-#[cfg(test)]mod tests {use super::*;@@ -129,7 +159,7 @@ mod tests {use std::collections::HashSet;#[test]- fn test_should_record_usage_only_survey_flags() {+ fn test_contains_billable_flags_only_survey_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey1")),mock!(FeatureFlag, id: 2, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey2")),@@ -137,11 +167,11 @@ mod tests {.mock_into();// Should NOT record usage when only survey flags are present- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_only_regular_flags() {+ fn test_contains_billable_flags_only_regular_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: "regular_flag_1".mock_into()),mock!(FeatureFlag, id: 2, key: "feature_flag_2".mock_into()),@@ -149,11 +179,11 @@ mod tests {.mock_into();// Should record usage when only regular flags are present- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_mixed_flags() {+ fn test_contains_billable_flags_mixed_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey1")),mock!(FeatureFlag, id: 2, key: "regular_flag".mock_into()),@@ -161,19 +191,19 @@ mod tests {.mock_into();// Should record usage when there's at least one regular flag, even with survey flags- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_empty_flags() {+ fn test_contains_billable_flags_empty_flags() {let flag_list: FeatureFlagList = vec![].mock_into();// Should NOT record usage when there are no flags at all- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_flag_key_edge_cases() {+ fn test_contains_billable_flags_flag_key_edge_cases() {// Test flag that contains the prefix but doesn't start with it// Test flag that starts with prefix but has extra contentlet flag_list: FeatureFlagList = vec![@@ -184,11 +214,11 @@ mod tests {// Should record usage: first flag doesn't START with prefix, second does start with prefix// Since we use any(), and the first flag should return true for "!starts_with()", overall result should be true- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_filtered_out_flags_not_billable() {+ fn test_contains_billable_flags_filtered_out_flags_not_billable() {let disabled_flag = mock!(FeatureFlag, id: 1, key: "regular_flag".mock_into());let flag_list = mock!(FeatureFlagList,@@ -197,11 +227,11 @@ mod tests {);// Should NOT record usage when only filtered-out flags are present- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_mixed_active_and_filtered_out() {+ fn test_contains_billable_flags_mixed_active_and_filtered_out() {let disabled_flag = mock!(FeatureFlag, id: 1, key: "disabled_flag".mock_into());let active_flag = mock!(FeatureFlag, id: 2, key: "active_flag".mock_into());@@ -211,11 +241,11 @@ mod tests {);// Should record usage when at least one non-filtered, non-survey flag is present- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_filtered_out_survey_flag() {+ fn test_contains_billable_flags_filtered_out_survey_flag() {let disabled_survey_flag =mock!(FeatureFlag, id: 1, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey1"));@@ -225,11 +255,11 @@ mod tests {);// Should NOT record usage for filtered-out survey flags- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_only_filtered_out_and_survey_flags() {+ fn test_contains_billable_flags_only_filtered_out_and_survey_flags() {let disabled_flag = mock!(FeatureFlag, id: 1, key: "disabled_flag".mock_into());let survey_flag =mock!(FeatureFlag, id: 2, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey1"));@@ -240,11 +270,11 @@ mod tests {);// Should NOT record usage when only filtered-out and survey flags are present- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_only_product_tour_flags() {+ fn test_contains_billable_flags_only_product_tour_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: format!("{PRODUCT_TOUR_TARGETING_FLAG_PREFIX}tour1")),mock!(FeatureFlag, id: 2, key: format!("{PRODUCT_TOUR_TARGETING_FLAG_PREFIX}tour2")),@@ -252,11 +282,11 @@ mod tests {.mock_into();// Should NOT record usage when only product tour flags are present- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_product_tour_mixed_with_regular_flags() {+ fn test_contains_billable_flags_product_tour_mixed_with_regular_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: format!("{PRODUCT_TOUR_TARGETING_FLAG_PREFIX}tour1")),mock!(FeatureFlag, id: 2, key: "regular_flag".mock_into()),@@ -264,11 +294,11 @@ mod tests {.mock_into();// Should record usage when there's at least one regular flag, even with product tour flags- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_filtered_out_product_tour_flag() {+ fn test_contains_billable_flags_filtered_out_product_tour_flag() {let disabled_tour_flag =mock!(FeatureFlag, id: 1, key: format!("{PRODUCT_TOUR_TARGETING_FLAG_PREFIX}tour1"));@@ -278,11 +308,11 @@ mod tests {);// Should NOT record usage for filtered-out product tour flags- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_only_survey_and_product_tour_flags() {+ fn test_contains_billable_flags_only_survey_and_product_tour_flags() {let flag_list: FeatureFlagList = vec![mock!(FeatureFlag, id: 1, key: format!("{SURVEY_TARGETING_FLAG_PREFIX}survey1")),mock!(FeatureFlag, id: 2, key: format!("{PRODUCT_TOUR_TARGETING_FLAG_PREFIX}tour1")),@@ -290,11 +320,11 @@ mod tests {.mock_into();// Should NOT record usage when only survey and product tour flags are present- assert!(!should_record_usage(&flag_list));+ assert!(!contains_billable_flags(&flag_list));}#[test]- fn test_should_record_usage_product_tour_flag_key_edge_cases() {+ fn test_contains_billable_flags_product_tour_flag_key_edge_cases() {// Test flag that contains the prefix but doesn't start with it// Test flag that starts with prefix but has extra contentlet flag_list: FeatureFlagList = vec![@@ -304,6 +334,6 @@ mod tests {.mock_into();// Should record usage: first flag doesn't START with prefix, second does start with prefix- assert!(should_record_usage(&flag_list));+ assert!(contains_billable_flags(&flag_list));}}diff --git a/rust/feature-flags/src/handler/canonical_log.rs b/rust/feature-flags/src/handler/canonical_log.rsindex fd3ce08849cd..dc7d5024fa09 100644--- a/rust/feature-flags/src/handler/canonical_log.rs+++ b/rust/feature-flags/src/handler/canonical_log.rs@@ -269,11 +269,12 @@ pub struct FlagsCanonicalLogLine {/// phase ships; while None, `emit_timing_metrics` skips emission.pub concurrency_limit_wait_ms: Option<u64>,- /// Duration of the Redis HINCRBY billing increment call in milliseconds.- /// Only populated from endpoints that run inside a canonical log scope- /// (currently `/flags` and `/decide`). `None` when billing was skipped- /// (no billable flags or `skip_writes`), or when called from an endpoint- /// that does not emit a canonical log line (e.g. `/flags/definitions`).+ /// Duration of the synchronous Redis HINCRBY billing increment in+ /// milliseconds. Only populated from endpoints that run inside a canonical+ /// log scope (currently `/flags` and `/decide`). `None` when billing was+ /// skipped (no billable flags or `skip_writes`), or when called from an+ /// endpoint that does not emit a canonical log line (e.g.+ /// `/flags/definitions`).pub billing_duration_ms: Option<u64>,// Cache sources (populated during data fetching)diff --git a/rust/feature-flags/src/lib.rs b/rust/feature-flags/src/lib.rsindex 340a2ee2bd20..68f8d095b037 100644--- a/rust/feature-flags/src/lib.rs+++ b/rust/feature-flags/src/lib.rs@@ -1,5 +1,5 @@pub mod api;-pub mod billing_limiters;+pub mod billing;pub mod cohorts;pub mod config;pub mod config_cache;diff --git a/rust/feature-flags/src/metrics/consts.rs b/rust/feature-flags/src/metrics/consts.rsindex 7100c13bc2ef..db1bc1dbd0ff 100644--- a/rust/feature-flags/src/metrics/consts.rs+++ b/rust/feature-flags/src/metrics/consts.rs@@ -81,12 +81,103 @@ pub const DB_CONNECTION_POOL_IDLE_COUNTER: &str = "flags_db_connection_pool_idlepub const DB_CONNECTION_POOL_MAX_COUNTER: &str = "flags_db_connection_pool_max_total";pub const DB_CONNECTION_POOL_SIZE_GAUGE: &str = "flags_db_connection_pool_size";-// Flag billing timing-// Duration of the Redis HINCRBY billing increment call. Labeled by-// `outcome` ("ok" | "timeout" | "error") to isolate the happy path from-// Redis timeouts — closes a sub-metric blind spot on the billing path.+// Synchronous-path billing increment timing.+// Labeled by `outcome` ("ok" | "timeout" | "error") to isolate the happy+// path from Redis timeouts.pub const FLAG_BILLING_INCREMENT_TIME: &str = "flags_billing_increment_time_ms";+// Counter for Redis errors observed during the synchronous billing+// increment. Labeled by `error_type` ("timeout" | "transport" | "not_found"+// | "parse" | "config") — same classification the billing flusher uses, so+// breakdowns line up across both paths. The raw error message is never used+// as a label (cardinality risk).+pub const FLAG_REQUEST_REDIS_ERROR: &str = "flag_request_redis_error";++// Billing aggregator metrics+// See `src/billing/aggregator.rs`. The accounting identity+// flags_billing_records_total ≈ flags_billing_entries_flushed_total+// + flags_billing_unflushed_requests_total{cause="cap_drop"}+// + flags_billing_unflushed_requests_total{cause="flush_dropped_on_error"}+// + flags_billing_unflushed_requests_total{cause="shutdown_drop"}+// + flags_billing_pending_records (the residual still in `pending`)+// should hold per pod over any window. The `redis_error` cause is omitted+// from this identity because it only fires under `BailOnError` (normal+// ticks): the affected entries are requeued and eventually flush or land+// in `shutdown_drop`, so they aren't lost at the moment the error fires.+// Under `BestEffort` (shutdown) the same chunk failures are recorded only+// as `flush_dropped_on_error`, so summing `redis_error` with the other+// causes for a total-loss figure does not double-count.++// Counter, labeled by `request_type` ("decide" | "flag_definitions").+pub const FLAGS_BILLING_RECORDS: &str = "flags_billing_records_total";++// Counter: sum of `count` args across all successfully written HINCRBYs.+pub const FLAGS_BILLING_ENTRIES_FLUSHED: &str = "flags_billing_entries_flushed_total";++// Gauge of the live `pending` map size. Sustained growth is the leading+// indicator of a wedged flusher; alert when the gauge approaches+// `max_pending_entries`. Pair with `flags_billing_seconds_since_successful_flush`+// to distinguish a hung `execute_pipeline` (gauge growing, staleness rising)+// from steady-state high traffic (gauge oscillating, staleness near zero).+pub const FLAGS_BILLING_PENDING_ENTRIES: &str = "flags_billing_pending_entries";++// Gauge: sum of counts in `pending` — the per-pod count of records that+// would be lost if the process crashed at scrape time (SIGKILL past+// `terminationGracePeriodSeconds`, OOM-kill, node loss, panic). The+// crashed pod cannot emit at the moment of loss, so this gauge is the+// pre-crash signal: scrape it just before the pod is gone, and you have+// an upper bound on the loss. Use `sum(flags_billing_pending_records)`+// for fleet-wide loss exposure, and correlate per-pod spikes with+// `kube_pod_container_status_restarts_total` to attribute crash-loss to+// specific restarts. Differs from `flags_billing_pending_entries`+// (number of distinct keys), which can stay flat while record counts+// per key climb.+pub const FLAGS_BILLING_PENDING_RECORDS: &str = "flags_billing_pending_records";++// Gauge: seconds elapsed since the last successful flush (0 until the+// first successful flush completes).+pub const FLAGS_BILLING_SECONDS_SINCE_SUCCESSFUL_FLUSH: &str =+ "flags_billing_seconds_since_successful_flush";++pub const FLAGS_BILLING_FLUSH_DURATION_MS: &str = "flags_billing_flush_duration_ms";++// Histogram of per-call `record()` latency in microseconds, with no labels+// to keep the hot-path emission allocation-free. The expected uncontended+// p50 is sub-microsecond (one atomic increment + a hash + a HashMap entry+// op). p99 climbing while p50 stays flat is the canonical signature of+// `pending` mutex contention — this metric is the sole signal for it, since+// `record()` would otherwise be invisible to monitoring.+pub const FLAGS_BILLING_RECORD_DURATION_US: &str = "flags_billing_record_duration_us";++// Counter: flusher-side Redis failures, labeled by `error_type`+// ("timeout" | "transport" | "not_found" | "parse" | "config"). Alert on+// `rate(flags_billing_flush_errors_total[1m]) > 0 for: 30s` to catch a+// wedged Redis link without a separate consecutive-failures gauge.+pub const FLAGS_BILLING_FLUSH_ERRORS: &str = "flags_billing_flush_errors_total";++// Counter: billable requests that did not reach Redis, labeled by `cause`.+// `sum(rate(flags_billing_unflushed_requests_total[5m]))` is the+// one-expression billing-leak rate. Cause values:+// - "cap_drop": dropped at the hot path because `pending_entries` was at+// `max_pending_entries` and the incoming key was new. Tripwire — any+// non-zero rate is an incident signal.+// - "flush_dropped_on_error": drained from `pending`, then lost because a+// flush chunk errored AND the flush policy couldn't retry. In practice+// only fires on the shutdown path (`BestEffort`); normal ticks+// (`BailOnError`) re-queue failed entries back into `pending`.+// - "shutdown_drop": entries lost during shutdown because the final flush+// timed out or panicked. SIGKILL past the grace window lands here.+// - "redis_error": affected-request count for chunks that hit a Redis+// error during a normal-tick flush. Only emitted under `BailOnError`:+// includes the failing chunk plus the unattempted remainder so the+// rate reflects all requests blocked by the error, not just the chunk+// that hit it. These records are requeued and retried, so this label+// is an "incident magnitude" signal rather than a terminal-loss+// signal. Suppressed under `BestEffort` (shutdown) where the same+// counts terminally land in `flush_dropped_on_error` — emitting both+// would double-count when summing causes for a total-loss figure.+pub const FLAGS_BILLING_UNFLUSHED_REQUESTS: &str = "flags_billing_unflushed_requests_total";+// Flag evaluation timingpub const FLAG_EVALUATION_TIME: &str = "flags_evaluation_time";pub const FLAG_HASH_KEY_PROCESSING_TIME: &str = "flags_hash_key_processing_time";diff --git a/rust/feature-flags/src/router.rs b/rust/feature-flags/src/router.rsindex 5e2c0fa7247e..ec5455dc8bd7 100644--- a/rust/feature-flags/src/router.rs+++ b/rust/feature-flags/src/router.rs@@ -5,7 +5,7 @@ use std::{time::Duration,};-use crate::billing_limiters::{FeatureFlagsLimiter, SessionReplayLimiter};+use crate::billing::{BillingAggregator, FeatureFlagsLimiter, SessionReplayLimiter};use crate::database_pools::DatabasePools;use axum::{error_handling::HandleErrorLayer,@@ -100,6 +100,9 @@ pub struct State {pub auth_token_cache: Arc<ReadThroughCacheWithMetrics>,/// Provider for realtime/behavioral cohort membership lookupspub cohort_membership_provider: Arc<dyn CohortMembershipProvider>,+ /// Shadow-keyspace billing aggregator. See `crate::billing` for the+ /// dual-write contract.+ pub billing_aggregator: Option<Arc<BillingAggregator>>,}#[allow(clippy::too_many_arguments)]@@ -124,6 +127,7 @@ pub fn router(team_negative_cache: NegativeCache,auth_token_cache: Arc<ReadThroughCacheWithMetrics>,cohort_membership_provider: Arc<dyn CohortMembershipProvider>,+ billing_aggregator: Option<Arc<BillingAggregator>>,config: Config,) -> Router {// Initialize flag definitions rate limiter with default and custom team rates@@ -211,6 +215,7 @@ pub fn router(team_negative_cache,cohort_membership_provider,auth_token_cache,+ billing_aggregator,};// Very permissive CORS policy, as old SDK versionsdiff --git a/rust/feature-flags/src/server.rs b/rust/feature-flags/src/server.rsindex 0877c0fc3353..2ac130107005 100644--- a/rust/feature-flags/src/server.rs+++ b/rust/feature-flags/src/server.rs@@ -2,7 +2,9 @@ use std::net::SocketAddr;use std::sync::Arc;use std::time::Duration;-use crate::billing_limiters::{FeatureFlagsLimiter, SessionReplayLimiter};+use crate::billing::{+ BillingAggregator, BillingAggregatorConfig, FeatureFlagsLimiter, SessionReplayLimiter,+};use crate::cohorts::cohort_cache_manager::CohortCacheManager;use crate::cohorts::membership::{CachedCohortMembershipProvider, CohortMembershipProvider, NoOpCohortMembershipProvider,@@ -498,6 +500,22 @@ pub async fn serve(tokio_monitor.start_monitoring(tokio_monitor_handle).await;});+ let billing_aggregator: Option<Arc<BillingAggregator>> = if *config.billing_aggregator_enabled {+ Some(BillingAggregator::start(+ redis_client.clone(),+ BillingAggregatorConfig {+ flush_interval: Duration::from_millis(config.billing_flush_interval_ms),+ max_pending_entries: config.billing_max_pending_entries,+ per_flush_batch_size: config.billing_per_flush_batch_size,+ shutdown_flush_timeout: Duration::from_millis(+ config.billing_shutdown_flush_timeout_ms,+ ),+ },+ ))+ } else {+ None+ };+let app = router::router(redis_client,dedicated_redis_client,@@ -519,6 +537,7 @@ pub async fn serve(team_negative_cache,auth_token_cache,cohort_membership_provider,+ billing_aggregator.clone(),config,);@@ -537,6 +556,13 @@ pub async fn serve().with_graceful_shutdown(http_handle.shutdown_signal()).await;++ // Must run *after* `axum::serve(...).await` resolves so axum has drained+ // in-flight requests; flushing earlier would miss late-arriving records.+ if let Some(ref aggregator) = billing_aggregator {+ aggregator.shutdown().await;+ }+match serve_result {Ok(()) => http_handle.work_completed(),Err(e) => {diff --git a/rust/feature-flags/tests/common/mod.rs b/rust/feature-flags/tests/common/mod.rsindex 54978de4d3e2..81ed55579999 100644--- a/rust/feature-flags/tests/common/mod.rs+++ b/rust/feature-flags/tests/common/mod.rs@@ -249,24 +249,21 @@ impl ServerHandle {),);- let feature_flags_billing_limiter =- feature_flags::billing_limiters::FeatureFlagsLimiter::new(- Duration::from_secs(5),- redis_reader_client.clone(),- QUOTA_LIMITER_CACHE_KEY.to_string(),- None,- )- .unwrap();-- let session_replay_billing_limiter =- feature_flags::billing_limiters::SessionReplayLimiter::new(- Duration::from_secs(5),- redis_reader_client.clone(),- QUOTA_LIMITER_CACHE_KEY.to_string(),- None,- )- .unwrap();+ let feature_flags_billing_limiter = feature_flags::billing::FeatureFlagsLimiter::new(+ Duration::from_secs(5),+ redis_reader_client.clone(),+ QUOTA_LIMITER_CACHE_KEY.to_string(),+ None,+ )+ .unwrap();+ let session_replay_billing_limiter = feature_flags::billing::SessionReplayLimiter::new(+ Duration::from_secs(5),+ redis_reader_client.clone(),+ QUOTA_LIMITER_CACHE_KEY.to_string(),+ None,+ )+ .unwrap();let cookieless_manager = Arc::new(common_cookieless::CookielessManager::new(config.get_cookieless_config(),redis_reader_client.clone(),@@ -373,6 +370,23 @@ impl ServerHandle {feature_flags::flags::flag_definitions_cache::FlagDefinitionsCache::disabled(),);+ // `for_tests()` skips the background flusher and the harness+ // never exposes a handle, so pending counts populated here never+ // reach mock Redis. The construction is intentional: it exercises+ // the synchronous `record()` half of the dual-write+ // (`handler::billing::record_billing_increment`) so mock-Redis+ // coverage doesn't silently regress that codepath. End-to-end+ // flush behavior lives in the real-Redis tests in `test_flags.rs`+ // (`test_dual_write_*`, `test_shutdown_flush_*`).+ let billing_aggregator = if *config.billing_aggregator_enabled {+ Some(feature_flags::billing::BillingAggregator::for_tests(+ redis_writer_client.clone(),+ feature_flags::billing::BillingAggregatorConfig::default(),+ ))+ } else {+ None+ };+let app = feature_flags::router::router(redis_writer_client.clone(), // Use writer client for both reads and writes in testsNone, // No dedicated flags Redis in tests@@ -407,6 +421,7 @@ impl ServerHandle {&[],)),Arc::new(NoOpCohortMembershipProvider),+ billing_aggregator,config,);@@ -475,3 +490,22 @@ impl Drop for ServerHandle {self.shutdown.cancel();}}++/// 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.+#[allow(dead_code)]+pub async fn poll_for_billing_counter(+ client: &Arc<dyn common_redis::Client + Send + Sync>,+ key: &str,+ field: &str,+) -> String {+ for _ in 0..40 {+ if let Ok(v) = client.hget(key.to_string(), field.to_string()).await {+ return v;+ }+ tokio::time::sleep(Duration::from_millis(25)).await;+ }+ panic!("billing counter at {key}:{field} did not appear within 1s");+}diff --git a/rust/feature-flags/tests/test_flag_definitions.rs b/rust/feature-flags/tests/test_flag_definitions.rsindex fd800147da5d..9906099ad8fa 100644--- a/rust/feature-flags/tests/test_flag_definitions.rs+++ b/rust/feature-flags/tests/test_flag_definitions.rs@@ -2556,3 +2556,88 @@ async fn test_db_rate_limit_allowlist() {.await.unwrap();}++/// Verifies the synchronous billing path writes (or, when `skip_writes=true`,+/// suppresses) the FlagDefinitions counter for `/flags/definitions`. The+/// `/flags` endpoint is covered by tests in `test_flags.rs`; this test is+/// the equivalent for the FlagDefinitions code path so a regression that+/// honored `skip_writes` for one endpoint but not the other can't slip+/// through.+#[rstest::rstest]+#[case(true)]+#[case(false)]+#[tokio::test]+async fn test_flag_definitions_billing_counter(#[case] skip_writes: bool) {+ use feature_flags::config::FlexBool;+ use feature_flags::flags::flag_analytics::{current_bucket, get_team_request_key};+ use feature_flags::flags::flag_request::FlagRequestType;+ use feature_flags::utils::test_utils::{setup_redis_client, TestContext};+ use serde_json::json;++ let mut config = feature_flags::config::Config::default_test_config();+ config.skip_writes = FlexBool(skip_writes);++ let context = TestContext::new(Some(&config)).await;+ let (team, secret_token, _) = context+ .create_team_with_secret_token(None, None, None)+ .await+ .unwrap();++ // Seed the HyperCache with a billable flag (a non-survey, non-product-tour+ // key) so `has_billable_flags` returns true and `record()` actually fires.+ context+ .populate_cache_for_team_with_flags(+ team.id,+ json!({+ "flags": [{"key": "billable-flag", "active": true}],+ "group_type_mapping": {},+ "cohorts": {},+ }),+ )+ .await+ .unwrap();++ // Clean billing key so we can assert on a fresh write.+ let redis = setup_redis_client(Some(config.redis_url.clone())).await;+ let billing_key = get_team_request_key(team.id, FlagRequestType::FlagDefinitions);+ redis.del(billing_key.clone()).await.unwrap();++ 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();++ let response = http+ .get(format!(+ "http://{}/flags/definitions?token={}",+ server.addr, team.api_token+ ))+ .header("Authorization", format!("Bearer {secret_token}"))+ .send()+ .await+ .unwrap();+ assert_eq!(+ response.status(),+ 200,+ "Response body: {}",+ response.text().await.unwrap()+ );++ // Synchronous path writes inline before the response returns, so we can+ // read back without polling.+ let counter = redis.hget(billing_key, bucket_field).await;++ if skip_writes {+ assert!(+ counter.is_err(),+ "FlagDefinitions billing counter should NOT be incremented when skip_writes=true"+ );+ } else {+ assert_eq!(+ counter.unwrap(),+ "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 5baba883e074..6fc7a9cd47e0 100644--- a/rust/feature-flags/tests/test_flags.rs+++ b/rust/feature-flags/tests/test_flags.rs@@ -5624,9 +5624,8 @@ async fn test_skip_writes_suppresses_billing_redis_counter(#[case] skip_writes: bool,) -> Result<()> {use feature_flags::config::FlexBool;- use feature_flags::flags::flag_analytics::get_team_request_key;+ use feature_flags::flags::flag_analytics::{current_bucket, get_team_request_key};use feature_flags::flags::flag_request::FlagRequestType;- use std::time::{SystemTime, UNIX_EPOCH};let mut config = DEFAULT_TEST_CONFIG.clone();config.skip_writes = FlexBool(skip_writes);@@ -5672,19 +5671,17 @@ 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();+let res = server.send_flags_request(payload.to_string(), Some("2"), None).await;assert_eq!(StatusCode::OK, res.status());- // Compute the same time bucket used by increment_request_count- let time_bucket = SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs()- / 120; // CACHE_BUCKET_SIZE = 60 * 2-- let counter = client.hget(billing_key, time_bucket.to_string()).await;+ // Synchronous path writes inline before the response returns, so we can+ // read back without polling.+ let counter = client.hget(billing_key, bucket_field).await;if skip_writes {assert!(@@ -5702,6 +5699,216 @@ async fn test_skip_writes_suppresses_billing_redis_counter(Ok(())}+/// Verifies that an SDK request lands counts in BOTH the team-level and the+/// library-level Redis hashes via the synchronous billing path, AND — when+/// the aggregator is enabled — that the same counts also reach the shadow+/// keyspace via the aggregator's flush path. This is the cross-path+/// reconciliation invariant that the dual-write design depends on.+#[tokio::test]+async fn test_dual_write_lands_counts_in_production_and_shadow_keys() -> Result<()> {+ use feature_flags::config::FlexBool;+ use feature_flags::flags::flag_analytics::{+ current_bucket, get_team_request_key, get_team_request_library_key,+ get_team_request_library_shadow_key, get_team_request_shadow_key,+ };+ use feature_flags::flags::flag_request::FlagRequestType;+ use feature_flags::handler::types::Library;++ let mut config = DEFAULT_TEST_CONFIG.clone();+ config.skip_writes = FlexBool(false);+ // Enable the shadow path so we can assert both keyspaces in one go.+ config.billing_aggregator_enabled = FlexBool(true);+ // Tight flush interval so the aggregator lands its shadow write before+ // the assertion polling window expires.+ config.billing_flush_interval_ms = 100;++ let distinct_id = format!("billing_lib_test_{}", rand::thread_rng().gen::<u32>());++ let client = setup_redis_client(Some(config.redis_url.clone())).await;+ let team = insert_new_team_in_redis(client.clone()).await.unwrap();+ let token = team.api_token.clone();++ let context = TestContext::new(None).await;+ context.insert_new_team(Some(team.id)).await.unwrap();+ context+ .insert_person(team.id, distinct_id.clone(), None)+ .await+ .unwrap();++ let flag_json = json!([{+ "id": 1,+ "key": "billable-flag",+ "name": "Billable Flag",+ "active": true,+ "deleted": false,+ "team_id": team.id,+ "filters": {+ "groups": [{+ "properties": [],+ "rollout_percentage": 100+ }],+ },+ }]);+ insert_flags_for_team_in_redis(client.clone(), team.id, Some(flag_json.to_string())).await?;++ let team_key = get_team_request_key(team.id, FlagRequestType::Decide);+ let library_key =+ get_team_request_library_key(team.id, FlagRequestType::Decide, Library::PosthogNode);+ let team_shadow_key = get_team_request_shadow_key(team.id, FlagRequestType::Decide);+ let library_shadow_key =+ get_team_request_library_shadow_key(team.id, FlagRequestType::Decide, Library::PosthogNode);+ client.del(team_key.clone()).await.unwrap();+ client.del(library_key.clone()).await.unwrap();+ client.del(team_shadow_key.clone()).await.unwrap();+ client.del(library_shadow_key.clone()).await.unwrap();++ let server = ServerHandle::for_config(config).await;++ let payload = json!({+ "token": token,+ "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();++ // Direct reqwest call so we can set the SDK user-agent that drives+ // Library::from_headers → Library::PosthogNode.+ let http = reqwest::Client::new();+ let res = http+ .post(format!("http://{}/flags?v=2", server.addr))+ .header("content-type", "application/json")+ .header("user-agent", "posthog-node/3.1.0")+ .body(payload.to_string())+ .send()+ .await?;+ assert_eq!(StatusCode::OK, res.status());++ // Production keyspace — written synchronously by `record_usage`.+ let team_counter = client.hget(team_key, bucket_field.clone()).await.unwrap();+ assert_eq!(team_counter, "1", "team key should reflect one request");++ let library_counter = client+ .hget(library_key, bucket_field.clone())+ .await+ .unwrap();+ assert_eq!(+ library_counter, "1",+ "library key should reflect one request"+ );++ // Shadow keyspace — written by the aggregator's background flush. Poll+ // because we don't know exactly when the flusher tick lands.+ let team_shadow_counter =+ poll_for_billing_counter(&client, &team_shadow_key, &bucket_field).await;+ assert_eq!(+ team_shadow_counter, "1",+ "shadow team key should reflect one request from the aggregator"+ );++ let library_shadow_counter =+ poll_for_billing_counter(&client, &library_shadow_key, &bucket_field).await;+ assert_eq!(+ library_shadow_counter, "1",+ "shadow library key should reflect one request from the aggregator"+ );++ Ok(())+}++/// End-to-end shutdown-flush path *for the shadow aggregator*. Sets a flush+/// interval much longer than the test's runtime so the periodic flusher+/// cannot fire — the only way the **shadow** counter can land in Redis is via+/// `BillingAggregator::shutdown()` after axum's graceful drain. Catches a+/// regression that drops the `billing_aggregator.shutdown().await` call in+/// `serve()`, swaps in `for_tests()`, or wires up the lifecycle ordering+/// wrong (flushing before axum drains). The synchronous production-keyspace+/// write is unaffected by the aggregator's lifecycle and is verified by+/// `test_skip_writes_suppresses_billing_redis_counter`.+#[tokio::test]+async fn test_shutdown_flush_lands_shadow_counter_in_redis() -> Result<()> {+ use feature_flags::config::FlexBool;+ use feature_flags::flags::flag_analytics::{current_bucket, get_team_request_shadow_key};+ use feature_flags::flags::flag_request::FlagRequestType;++ let mut config = DEFAULT_TEST_CONFIG.clone();+ config.skip_writes = FlexBool(false);+ // The aggregator must be on for this test to mean anything — the gate is+ // off by default, so explicitly enable it.+ config.billing_aggregator_enabled = FlexBool(true);+ // Long enough that no periodic flush can fire during the test. If the+ // counter shows up in Redis, the shutdown path is what put it there.+ config.billing_flush_interval_ms = 60_000;++ let distinct_id = format!("billing_shutdown_test_{}", rand::thread_rng().gen::<u32>());++ let client = setup_redis_client(Some(config.redis_url.clone())).await;+ let team = insert_new_team_in_redis(client.clone()).await.unwrap();+ let token = team.api_token.clone();++ let context = TestContext::new(None).await;+ context.insert_new_team(Some(team.id)).await.unwrap();+ context+ .insert_person(team.id, distinct_id.clone(), None)+ .await+ .unwrap();++ let flag_json = json!([{+ "id": 1,+ "key": "billable-flag",+ "name": "Billable Flag",+ "active": true,+ "deleted": false,+ "team_id": team.id,+ "filters": {+ "groups": [{ "properties": [], "rollout_percentage": 100 }],+ },+ }]);+ insert_flags_for_team_in_redis(client.clone(), team.id, Some(flag_json.to_string())).await?;++ let billing_key = get_team_request_shadow_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 });+ let res = server+ .send_flags_request(payload.to_string(), Some("2"), None)+ .await;+ assert_eq!(StatusCode::OK, res.status());++ // Confirm the periodic flusher has NOT yet landed the SHADOW write —+ // sanity check that we're really exercising the shutdown path. With a+ // 60s flush interval and immediate-after-request lookup, the shadow+ // counter should still be missing. (The synchronous production-keyspace+ // write *has* already happened by now, but that's a different key.)+ let pre_shutdown = client.hget(billing_key.clone(), bucket_field.clone()).await;+ assert!(+ pre_shutdown.is_err(),+ "shadow billing counter 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+ // the final flush to the shadow keyspace.+ server.shutdown_now();++ // 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;+ assert_eq!(+ counter, "1",+ "shadow billing counter must reach Redis via the shutdown flush path"+ );++ Ok(())+}+#[tokio::test]async fn test_api_cohort_flag_integration() -> Result<()> {let config = DEFAULT_TEST_CONFIG.clone();