: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
refactor(capture): GRL throttled tier-gauge scan, finer latency buckets, new metrics (#64450)
Eli Reisman366770e0a636
diff --git a/rust/capture/src/prometheus.rs b/rust/capture/src/prometheus.rsindex c6639bea125b..73afb2daac62 100644--- a/rust/capture/src/prometheus.rs+++ b/rust/capture/src/prometheus.rs@@ -82,19 +82,16 @@ pub fn setup_metrics_recorder(role: String, capture_mode: &'static str) -> Prome];// Blob count per event (2x increments)const BLOB_COUNTS: &[f64] = &[1.0, 2.0, 4.0, 8.0, 16.0, 32.0];- // Global rate limiter pipeline/tick latency (milliseconds)- const GLOBAL_RATE_LIMITER_LATENCY_MS: &[f64] = &[- 0.1, // 100 microseconds- 0.5, // 500 microseconds- 1.0, // 1ms- 2.0, // 2ms- 5.0, // 5ms- 10.0, // 10ms- 100.0, // 100ms- 1000.0, // 1 second- 2000.0, // 2 seconds- 4000.0, // 4 seconds- 10000.0, // 10 seconds+ // Redis read/write pipeline round-trip (milliseconds). Dense around the+ // 100ms read/write timeout so p99 is readable below it.+ const GLOBAL_RATE_LIMITER_PIPELINE_MS: &[f64] = &[+ 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 30.0, 50.0, 75.0, 100.0, 150.0, 250.0, 500.0, 1000.0,+ ];+ // Full background tick (milliseconds). Dense around the 1s tick_interval so+ // we can see how close ticks run to the budget (e.g. reconcile ticks).+ const GLOBAL_RATE_LIMITER_TICK_MS: &[f64] = &[+ 5.0, 10.0, 25.0, 50.0, 100.0, 150.0, 250.0, 400.0, 600.0, 800.0, 1000.0, 1500.0, 2000.0,+ 4000.0,];// Global rate limiter pipeline batch sizes (entity counts)const GLOBAL_RATE_LIMITER_PIPELINE_SIZES: &[f64] =@@ -182,12 +179,12 @@ pub fn setup_metrics_recorder(role: String, capture_mode: &'static str) -> Prome.unwrap().set_buckets_for_metric(Matcher::Full("global_rate_limiter_pipeline_ms".to_string()),- GLOBAL_RATE_LIMITER_LATENCY_MS,+ GLOBAL_RATE_LIMITER_PIPELINE_MS,).unwrap().set_buckets_for_metric(Matcher::Full("global_rate_limiter_tick_ms".to_string()),- GLOBAL_RATE_LIMITER_LATENCY_MS,+ GLOBAL_RATE_LIMITER_TICK_MS,).unwrap().set_buckets_for_metric(diff --git a/rust/common/limiters/GLOBAL_RATE_LIMITER.md b/rust/common/limiters/GLOBAL_RATE_LIMITER.mdindex 0cbed26d3dac..11247f8fb7dc 100644--- a/rust/common/limiters/GLOBAL_RATE_LIMITER.md+++ b/rust/common/limiters/GLOBAL_RATE_LIMITER.md@@ -49,7 +49,7 @@ and **pressure-tiered adaptive sync** to minimize read volume for low-utilizatio│ │ - Measure drift vs local estimate │ ││ │ - Update CacheEntry (estimated_count, pressure, synced_at)│ ││ │ - Reset local_pending to 0 (Redis now includes our writes)│ │-│ │ - Track tier transitions │ │+│ │ - Record tier transitions for observability │ ││ └───────────────────────────┬────────────────────────────────────┘ ││ │ │└──────────────────────────────┼───────────────────────────────────────┘@@ -201,6 +201,8 @@ it's promoted to Low on the very next request — no waiting for a stale sync inTier boundaries are pressure-based (level / threshold), so they apply correctly to custom keys regardless of threshold magnitude. A custom key with a 100× higher limit than the default will sync at the same relative cadence when at equivalent pressure.+**Tier-distribution gauges come from a throttled full scan.** `cache_size` is cheap (`entry_count`) and emits every tick. The per-tier `sync_tier_gauge` needs a full `cache.iter()` scan, so it runs only every `TIER_SCAN_INTERVAL_TICKS` (default 30): the distribution moves slowly and prod metrics dedup to 60s, so scanning every tick would be wasted work under load. The scan runs in the background tick task, off the per-event hot path.+## Data Model### CacheEntry (local moka LRU)@@ -372,8 +374,10 @@ When multiple Redis instances are configured, work is partitioned by consistent| `global_rate_limiter_tick_ms` | Histogram | Full tick duration || `global_rate_limiter_pipeline_size` | Histogram | Entities per pipeline (read/write) || `global_rate_limiter_pending_sync_size` | Gauge | Backpressure signal |-| `global_rate_limiter_sync_tier_gauge` | Gauge | Entity distribution across tiers |+| `global_rate_limiter_sync_tier_gauge` | Gauge | Entity distribution across tiers (scanned every `TIER_SCAN_INTERVAL_TICKS`) || `global_rate_limiter_tier_transitions_total` | Counter | Tier promotion/demotion events |+| `global_rate_limiter_cache_size` | Gauge | Live local cache entry count vs cap |+| `global_rate_limiter_eviction_total` | Counter | Cache evictions by cause (size/expired/explicit) || `global_rate_limiter_estimate_drift` | Histogram | Local vs Redis accuracy || `global_rate_limiter_sync_staleness_ms` | Histogram | Real staleness at access time || `global_rate_limiter_error_total` | Counter | Pipeline errors and timeouts |diff --git a/rust/common/limiters/src/global_rate_limiter.rs b/rust/common/limiters/src/global_rate_limiter.rsindex fc3e1e1ec08b..f13487bdf65b 100644--- a/rust/common/limiters/src/global_rate_limiter.rs+++ b/rust/common/limiters/src/global_rate_limiter.rs@@ -26,6 +26,15 @@ const GLOBAL_RATE_LIMITER_TIER_TRANSITIONS_COUNTER: &str ="global_rate_limiter_tier_transitions_total";const GLOBAL_RATE_LIMITER_ESTIMATE_DRIFT_HISTOGRAM: &str = "global_rate_limiter_estimate_drift";const GLOBAL_RATE_LIMITER_SYNC_STALENESS_HISTOGRAM: &str = "global_rate_limiter_sync_staleness_ms";+const GLOBAL_RATE_LIMITER_CACHE_SIZE_GAUGE: &str = "global_rate_limiter_cache_size";+const GLOBAL_RATE_LIMITER_EVICTION_COUNTER: &str = "global_rate_limiter_eviction_total";++/// Full-cache scan cadence (ticks) for the per-tier distribution gauges. The+/// distribution moves slowly and prod metrics dedup to 60s, so a periodic scan+/// keeps these gauges fresh enough without scanning the cache every tick.+const TIER_SCAN_INTERVAL_TICKS: u64 = 30;+/// Tier label order, indexed by `PressureTier::index()`.+const TIER_LABELS: [&str; 4] = ["idle", "low", "normal", "hot"];/// Pressure tiers for adaptive sync scheduling#[derive(Debug, Clone, Copy, PartialEq, Eq)]@@ -61,6 +70,16 @@ impl PressureTier {Self::Hot => "hot",}}++ /// Index into the per-tier scan tally array (see `TIER_LABELS`).+ pub fn index(&self) -> usize {+ match self {+ Self::Idle => 0,+ Self::Low => 1,+ Self::Normal => 2,+ Self::Hot => 3,+ }+ }}/// Compute the effective sync interval for a given pressure tier@@ -379,15 +398,28 @@ impl GlobalRateLimiterImpl {));}+ let scope: &'static str = Box::leak(config.metrics_scope.clone().into_boxed_str());+let cache = Cache::builder().max_capacity(config.local_cache_max_entries).time_to_live(config.local_cache_ttl).time_to_idle(config.local_cache_idle_timeout)+ .eviction_listener(move |_key, _entry: CacheEntry, cause| {+ // MUST stay panic-free: moka permanently disables a listener that+ // panics. Replaced is an in-place update, not a removal.+ if cause != moka::notification::RemovalCause::Replaced {+ metrics::counter!(+ GLOBAL_RATE_LIMITER_EVICTION_COUNTER,+ "scope" => scope,+ "cause" => removal_cause_str(cause),+ )+ .increment(1);+ }+ }).build();let (update_tx, update_rx) = mpsc::channel(config.channel_capacity);let pending_sync = Arc::new(DashSet::new());- let scope: &'static str = Box::leak(config.metrics_scope.clone().into_boxed_str());let limiter = Self {config: config.clone(),@@ -559,6 +591,7 @@ impl GlobalRateLimiterImpl {let mut tick = tokio::time::interval(config.tick_interval);// Pre-aggregate writes by (key, epoch)let mut write_batch: HashMap<(String, i64), u64> = HashMap::new();+ let mut tick_n: u64 = 0;loop {tokio::select! {@@ -573,7 +606,7 @@ impl GlobalRateLimiterImpl {if !write_batch.is_empty() {Self::tick(&config, &redis_instances, &cache,- &pending_sync, &mut write_batch, scope,+ &pending_sync, &mut write_batch, scope, tick_n,).await;}break;@@ -581,9 +614,10 @@ impl GlobalRateLimiterImpl {}}_ = tick.tick() => {+ tick_n = tick_n.wrapping_add(1);Self::tick(&config, &redis_instances, &cache,- &pending_sync, &mut write_batch, scope,+ &pending_sync, &mut write_batch, scope, tick_n,).await;}}@@ -602,9 +636,14 @@ impl GlobalRateLimiterImpl {pending_sync: &Arc<DashSet<String>>,write_batch: &mut HashMap<(String, i64), u64>,scope: &'static str,+ tick_n: u64,) {let tick_start = Instant::now();+ // Cache-size gauge every tick (O(1)); per-tier distribution via a+ // throttled full scan (slow-moving, see TIER_SCAN_INTERVAL_TICKS).+ Self::emit_cache_gauges(cache, scope, tick_n);+// Drain pending sync set (lock-free: iterate then clear)let sync_keys: Vec<String> = pending_sync.iter().map(|r| r.key().clone()).collect();pending_sync.clear();@@ -649,6 +688,7 @@ impl GlobalRateLimiterImpl {}/// Execute a tick against a single Redis instance (the common case).+ #[allow(clippy::too_many_arguments)]async fn tick_single_instance(config: &GlobalRateLimiterConfig,redis: &Arc<dyn Client + Send + Sync>,@@ -871,20 +911,18 @@ impl GlobalRateLimiterImpl {.copied().unwrap_or(config.global_threshold);- // Compute drift before updating (for observability)+ let pressure = estimated / threshold as f64;+ let new_tier = PressureTier::from_pressure(pressure);++ // Single lookup: emit estimate drift + tier transition for the prior entry.if let Some(old_entry) = cache.get(key) {let leak_rate = config.leak_rate_for(threshold);let local_estimate = effective_level(&old_entry, leak_rate, now_instant);let drift = (local_estimate - estimated).abs() / threshold as f64;metrics::histogram!(GLOBAL_RATE_LIMITER_ESTIMATE_DRIFT_HISTOGRAM, "scope" => scope).record(drift);- }- let pressure = estimated / threshold as f64;- // Track tier transitions- if let Some(old_entry) = cache.get(key) {let old_tier = PressureTier::from_pressure(old_entry.pressure);- let new_tier = PressureTier::from_pressure(pressure);if old_tier != new_tier {metrics::counter!(GLOBAL_RATE_LIMITER_TIER_TRANSITIONS_COUNTER,@@ -910,26 +948,47 @@ impl GlobalRateLimiterImpl {},);}+ }++ /// Emit cache observability gauges from the background task (off the hot path).+ ///+ /// `cache_size` is cheap (`entry_count`) so it emits every tick. The per-tier+ /// distribution needs a full `cache.iter()` scan, so it runs only every+ /// `TIER_SCAN_INTERVAL_TICKS`: the distribution moves slowly and prod metrics+ /// dedup to 60s, so scanning every tick would be wasted work under load.+ fn emit_cache_gauges(cache: &Cache<String, CacheEntry>, scope: &'static str, tick_n: u64) {+ metrics::gauge!(GLOBAL_RATE_LIMITER_CACHE_SIZE_GAUGE, "scope" => scope)+ .set(cache.entry_count() as f64);++ if tick_n.is_multiple_of(TIER_SCAN_INTERVAL_TICKS) {+ let tier_counts = Self::scan_tier_counts(cache);+ for i in 0..4 {+ metrics::gauge!(GLOBAL_RATE_LIMITER_SYNC_TIER_GAUGE, "scope" => scope, "tier" => TIER_LABELS[i])+ .set(tier_counts[i] as f64);+ }+ }+ }- // Update tier gauge counts+ /// Full O(n) scan tallying live entries per pressure tier (indexed by+ /// `PressureTier::index()`). Off the hot path; see `TIER_SCAN_INTERVAL_TICKS`.+ fn scan_tier_counts(cache: &Cache<String, CacheEntry>) -> [u64; 4] {let mut tier_counts = [0u64; 4];for (_, entry) in cache.iter() {- let tier = PressureTier::from_pressure(entry.pressure);- match tier {- PressureTier::Idle => tier_counts[0] += 1,- PressureTier::Low => tier_counts[1] += 1,- PressureTier::Normal => tier_counts[2] += 1,- PressureTier::Hot => tier_counts[3] += 1,- }+ tier_counts[PressureTier::from_pressure(entry.pressure).index()] += 1;}- metrics::gauge!(GLOBAL_RATE_LIMITER_SYNC_TIER_GAUGE, "scope" => scope, "tier" => "idle")- .set(tier_counts[0] as f64);- metrics::gauge!(GLOBAL_RATE_LIMITER_SYNC_TIER_GAUGE, "scope" => scope, "tier" => "low")- .set(tier_counts[1] as f64);- metrics::gauge!(GLOBAL_RATE_LIMITER_SYNC_TIER_GAUGE, "scope" => scope, "tier" => "normal")- .set(tier_counts[2] as f64);- metrics::gauge!(GLOBAL_RATE_LIMITER_SYNC_TIER_GAUGE, "scope" => scope, "tier" => "hot")- .set(tier_counts[3] as f64);+ tier_counts+ }+}++/// Map a Moka removal cause to a stable metric label (Replaced is filtered out+/// before this is called).+fn removal_cause_str(cause: moka::notification::RemovalCause) -> &'static str {+ use moka::notification::RemovalCause;+ match cause {+ RemovalCause::Expired => "expired",+ RemovalCause::Explicit => "explicit",+ RemovalCause::Size => "size",+ RemovalCause::Replaced => "replaced",}}@@ -1722,6 +1781,63 @@ mod tests {}}+ // --- Tier distribution scan tests ---++ fn seed_entry(pressure: f64) -> CacheEntry {+ CacheEntry {+ estimated_count: 0.0,+ synced_at: Instant::now() - Duration::from_secs(30),+ local_pending: 0,+ pressure,+ }+ }++ #[test]+ fn test_scan_tier_counts_tallies_distribution() {+ let cache = Cache::builder().max_capacity(100).build();+ cache.insert("a".to_string(), seed_entry(0.0)); // idle+ cache.insert("b".to_string(), seed_entry(0.05)); // idle+ cache.insert("c".to_string(), seed_entry(0.3)); // low+ cache.insert("d".to_string(), seed_entry(0.6)); // normal+ cache.insert("e".to_string(), seed_entry(0.9)); // hot+ cache.insert("f".to_string(), seed_entry(0.95)); // hot+ cache.run_pending_tasks();++ let counts = GlobalRateLimiterImpl::scan_tier_counts(&cache);++ assert_eq!(counts[PressureTier::Idle.index()], 2);+ assert_eq!(counts[PressureTier::Low.index()], 1);+ assert_eq!(counts[PressureTier::Normal.index()], 1);+ assert_eq!(counts[PressureTier::Hot.index()], 2);+ assert_eq!(counts.iter().sum::<u64>(), cache.entry_count());+ }++ #[test]+ fn test_scan_tier_counts_excludes_evicted_entries() {+ // Size-based eviction: only the surviving entries should be tallied,+ // and the total must match entry_count after maintenance.+ let cache = Cache::builder().max_capacity(3).build();+ for i in 0..10 {+ cache.insert(format!("k{i}"), seed_entry(0.9)); // all hot+ }+ cache.run_pending_tasks();++ let counts = GlobalRateLimiterImpl::scan_tier_counts(&cache);++ assert_eq!(counts.iter().sum::<u64>(), cache.entry_count());+ assert!(cache.entry_count() <= 3);+ }++ #[test]+ fn test_scan_tier_counts_empty_cache() {+ let cache: Cache<String, CacheEntry> = Cache::builder().max_capacity(100).build();+ cache.run_pending_tasks();++ let counts = GlobalRateLimiterImpl::scan_tier_counts(&cache);++ assert_eq!(counts, [0, 0, 0, 0]);+ }+#[test]fn test_parse_redis_count() {assert_eq!(parse_redis_count(&Some(b"42".to_vec())), 42);